Can't Dim Range when Controlling Word from Excel

T

T_o_n_y

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/MacrosVBA/InsertingTextAtBookmark.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
 
D

Dave Peterson

Maybe you should qualify your ranges:

Dim r1 As Word.Range
Dim r2 As Excel.Range
 
T

T_o_n_y

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top