Set Bookmark Range results in Type Mismatch

  • Follow


I'm writing a VBA program which copies information from Excel into bookmarks in Word. I originally wrote it to be run from the Word document, but it makes more sense to have it in Excel so I'm migrating it over.

The original code worked, and used ActiveDocument throughout, which I've replaced with myDoc.

However, when run it gives the Type Mismatch error when I try to set the bookmark range and I can't figure out why.


myDoc is defined and works well in other places (for instance, if you add the line:

msgbox myDoc.Bookmarks(BookmarkToUse) 

into this sub it correctly returns to bookmark name.

The code for the sub is:

Private Sub UpdateBookmark(BookmarkToUse As String, TextToUse As String, FontToUse As String, SizeToUse As String)

    Dim BMRange As Range
    BMRange.SetRange = myDoc.Bookmarks(BookmarkToUse).Range
    BMRange.Text = TextToUse
    BMRange.Font.Name = FontToUse
    BMRange.Font.Size = SizeToUse
    myDoc.Bookmarks.Add BookmarkToUse, BMRange
End Sub

Any help would be much appreciated.





Submitted via EggHeadCafe - Software Developer Portal of Choice 
IIS - Create App Pools, Virtual Directories and Web Sites C# .NET
http://www.eggheadcafe.com/tutorials/aspnet/d4bba585-b517-4834-8476-ff05b085d86e/iis--create-app-pools-v.aspx
0
Reply Stephen 1/2/2010 8:25:33 PM

Excel has its own Range object.
MSWord has its own Range object.

I'd try:
Dim BMRange As Word.Range 'if you've got a reference to MSWord.
or
dim BMRange as Object     'if you're using late binding (no reference)


Stephen, Jackson wrote:
> 
> I'm writing a VBA program which copies information from Excel into bookmarks in Word. I originally wrote it to be run from the Word document, but it makes more sense to have it in Excel so I'm migrating it over.
> 
> The original code worked, and used ActiveDocument throughout, which I've replaced with myDoc.
> 
> However, when run it gives the Type Mismatch error when I try to set the bookmark range and I can't figure out why.
> 
> myDoc is defined and works well in other places (for instance, if you add the line:
> 
> msgbox myDoc.Bookmarks(BookmarkToUse)
> 
> into this sub it correctly returns to bookmark name.
> 
> The code for the sub is:
> 
> Private Sub UpdateBookmark(BookmarkToUse As String, TextToUse As String, FontToUse As String, SizeToUse As String)
> 
>     Dim BMRange As Range
>     BMRange.SetRange = myDoc.Bookmarks(BookmarkToUse).Range
>     BMRange.Text = TextToUse
>     BMRange.Font.Name = FontToUse
>     BMRange.Font.Size = SizeToUse
>     myDoc.Bookmarks.Add BookmarkToUse, BMRange
> End Sub
> 
> Any help would be much appreciated.
> 
> Submitted via EggHeadCafe - Software Developer Portal of Choice
> IIS - Create App Pools, Virtual Directories and Web Sites C# .NET
> http://www.eggheadcafe.com/tutorials/aspnet/d4bba585-b517-4834-8476-ff05b085d86e/iis--create-app-pools-v.aspx

-- 

Dave Peterson
0
Reply Dave 1/2/2010 8:39:28 PM


1 Replies
667 Views

(page loaded in 0.081 seconds)


Reply: