Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Controlling MS Word from VBA Excel danp_db Excel Programming 2 October 31st 05 04:04 PM
controlling ms word from vba excel danp_db Excel Programming 0 October 29th 05 08:56 PM
Controlling Word from Excel Paul Haywood Excel Programming 3 June 30th 04 11:13 AM
Controlling Word from Excel tonesmcbutt Excel Programming 1 January 7th 04 09:21 PM


All times are GMT +1. The time now is 06:16 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"