Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can't Dim Range when Controlling Word from Excel
How do you distinguish between "ranges" in Excel vs. "ranges" in Word when
controlling Word using Excel VBA? Or is this necessary? I ask because I'm getting a "Type Mismatch" error when trying to run VBA code in Excel that controls a Word document. I'm filling in a Word document form using Excel VBA and I'm trying to fill in a field without deleting the bookmark. The code for how to do this in Word VBA is provided at the following website http://www.word.mvps.org/FAQs/Macros...AtBookmark.htm but the problem I'm having is that the same code does not work in my Excel VBA routine. Below is a portion of my Excel VBA routine. I notice that when I type in my "Dim BMRange as R" that the editor comes up with TWO "Range"s, and TWO of everything to chose from...leading me to believe that the first one is an Excel Range and the second one a Word Range, etc... Dim Data As Range, cell As Range .... Set wrdDoc = wrdApp.Documents.Open(WordTemplatesPath & FormType & ".dot") With ActiveSheet Set Data = ActiveSheet.AutoFilter.Range.Columns(5) 'bookmarks column Set Data = Data.Offset(1, 0).Resize(Data.Rows.Count - 1, 1) Set Data = Data.SpecialCells(xlVisible) For Each cell In Data UpdateBookmark cell.Value, Range(cell.Value) Next .... end sub Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String) Dim BMRange As Range Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range BMRange.Text = TextToUse ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange End Sub Any help would be appreciated. Tony |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can't Dim Range when Controlling Word from Excel
Maybe you should qualify your ranges:
Dim r1 As Word.Range Dim r2 As Excel.Range T_o_n_y wrote: How do you distinguish between "ranges" in Excel vs. "ranges" in Word when controlling Word using Excel VBA? Or is this necessary? I ask because I'm getting a "Type Mismatch" error when trying to run VBA code in Excel that controls a Word document. I'm filling in a Word document form using Excel VBA and I'm trying to fill in a field without deleting the bookmark. The code for how to do this in Word VBA is provided at the following website http://www.word.mvps.org/FAQs/Macros...AtBookmark.htm but the problem I'm having is that the same code does not work in my Excel VBA routine. Below is a portion of my Excel VBA routine. I notice that when I type in my "Dim BMRange as R" that the editor comes up with TWO "Range"s, and TWO of everything to chose from...leading me to believe that the first one is an Excel Range and the second one a Word Range, etc... Dim Data As Range, cell As Range ... Set wrdDoc = wrdApp.Documents.Open(WordTemplatesPath & FormType & ".dot") With ActiveSheet Set Data = ActiveSheet.AutoFilter.Range.Columns(5) 'bookmarks column Set Data = Data.Offset(1, 0).Resize(Data.Rows.Count - 1, 1) Set Data = Data.SpecialCells(xlVisible) For Each cell In Data UpdateBookmark cell.Value, Range(cell.Value) Next ... end sub Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String) Dim BMRange As Range Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range BMRange.Text = TextToUse ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange End Sub Any help would be appreciated. Tony -- Dave Peterson |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Figured it out
Dave,
Thank you for your response. I didn't realize you could specify different range types that way. But in the meantime I've found another way to do what I want and it's much simpler. I'm using FormFields rather than simple bookmarks, so it was really much simpler, and I didn't even need the separate sub involving the range statement. Here's the code that worked for me: Dim Data As Range, cell As Range ... Set wrdDoc = wrdApp.Documents.Open(WordTemplatesPath & FormType & ".dot") With ActiveSheet Set Data = ActiveSheet.AutoFilter.Range.Columns(5) 'bookmarks column Set Data = Data.Offset(1, 0).Resize(Data.Rows.Count - 1, 1) Set Data = Data.SpecialCells(xlVisible) For Each cell In Data wrdDoc.FormFields(cell.Value).Result = Range(cell.Value) Next .... end sub Thanks again, -Tony "Dave Peterson" wrote: Maybe you should qualify your ranges: Dim r1 As Word.Range Dim r2 As Excel.Range T_o_n_y wrote: How do you distinguish between "ranges" in Excel vs. "ranges" in Word when controlling Word using Excel VBA? Or is this necessary? I ask because I'm getting a "Type Mismatch" error when trying to run VBA code in Excel that controls a Word document. I'm filling in a Word document form using Excel VBA and I'm trying to fill in a field without deleting the bookmark. The code for how to do this in Word VBA is provided at the following website http://www.word.mvps.org/FAQs/Macros...AtBookmark.htm but the problem I'm having is that the same code does not work in my Excel VBA routine. Below is a portion of my Excel VBA routine. I notice that when I type in my "Dim BMRange as R" that the editor comes up with TWO "Range"s, and TWO of everything to chose from...leading me to believe that the first one is an Excel Range and the second one a Word Range, etc... Dim Data As Range, cell As Range ... Set wrdDoc = wrdApp.Documents.Open(WordTemplatesPath & FormType & ".dot") With ActiveSheet Set Data = ActiveSheet.AutoFilter.Range.Columns(5) 'bookmarks column Set Data = Data.Offset(1, 0).Resize(Data.Rows.Count - 1, 1) Set Data = Data.SpecialCells(xlVisible) For Each cell In Data UpdateBookmark cell.Value, Range(cell.Value) Next ... end sub Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String) Dim BMRange As Range Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range BMRange.Text = TextToUse ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange End Sub Any help would be appreciated. Tony -- Dave Peterson |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Controlling MS Word from VBA Excel | Excel Programming | |||
controlling ms word from vba excel | Excel Programming | |||
Controlling Word from Excel | Excel Programming | |||
Controlling Word from Excel | Excel Programming |