Accessing and changing Word bookmarks from Excel

K

Keith

I'm using Excel to sort information that needs to be used to update a Word
document. I've gotten to the point where I have an (unsorted) array of
bookmark /numbers/ that need to be removed/cleared from the document. What
I'm trying to do is replace the bookmark text with "" or " " and assign a
bookmark with the same name to it, so that my bookmark count doesn't change.

The problem is that my attempts to adapt the code from the MVP website have
not been successful. I've pasted my code below; the problem *appears* to be
the selection of the bookmark range, although I'm not familiar enough with
the Word object model to know for sure. I tried using both the bookmark
index and the bookmark name on the offending line, and both give a runtime
error 13 type mismatch. I'm trying to set one range equal to another range,
so I'm stumped!

Any help would be greatly appreciated. Using Office2003.
keith


Sub MakeGuideA()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
Dim BMRange As Range

wrdApp.Visible = True
wrdApp.DisplayAlerts = wdAlertsNone

Set wrdDoc = wrdApp.Documents.Open(CurrentPath & "\Interview_GuideA" &
".doc", , True)

'word operations
With wrdDoc
For deleteComp = 1 To 18
Bkmksize = .Bookmarks.Count
thisVal = Application.WorksheetFunction.Max(WordArray) 'numeric
index of target bookmark
thisMatch = Application.Match(thisVal, WordArray, 0) 'where in
my array it is located
BookMarkName = .Bookmarks(thisMatch).Name 'get the name in case
I need it

If thisVal > 0 Then
Set BMRange = wrdDoc.Bookmarks(BookMarkName).Range '***
runtime error 13 type mismatch ***
BMRange.Text = ""
.Bookmarks.Add BookMarkName, BMRange
End If

WordArray(thisMatch) = ""

Next
wrdDoc.SaveAs (CurrentPath & ApplicantName & ".doc")
.Close ' close the document
End With

wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
ActiveWorkbook.Saved = True
End Sub
 
C

Cindy M.

Hi Keith,

I think the problem is with the declaration of BMRange. You use
Dim BMRange as Range

But both Excel and Word have a Range object, and Excel's will take precedence.
So you need to specifically say you want to use a Word Range:
Dim BMRange as Word.Range
I'm using Excel to sort information that needs to be used to update a Word
document. I've gotten to the point where I have an (unsorted) array of
bookmark /numbers/ that need to be removed/cleared from the document. What
I'm trying to do is replace the bookmark text with "" or " " and assign a
bookmark with the same name to it, so that my bookmark count doesn't change.

The problem is that my attempts to adapt the code from the MVP website have
not been successful. I've pasted my code below; the problem *appears* to be
the selection of the bookmark range, although I'm not familiar enough with
the Word object model to know for sure. I tried using both the bookmark
index and the bookmark name on the offending line, and both give a runtime
error 13 type mismatch. I'm trying to set one range equal to another range,
so I'm stumped!

Any help would be greatly appreciated. Using Office2003.
keith


Sub MakeGuideA()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
Dim BMRange As Range

wrdApp.Visible = True
wrdApp.DisplayAlerts = wdAlertsNone

Set wrdDoc = wrdApp.Documents.Open(CurrentPath & "\Interview_GuideA" &
".doc", , True)

'word operations
With wrdDoc
For deleteComp = 1 To 18
Bkmksize = .Bookmarks.Count
thisVal = Application.WorksheetFunction.Max(WordArray) 'numeric
index of target bookmark
thisMatch = Application.Match(thisVal, WordArray, 0) 'where in
my array it is located
BookMarkName = .Bookmarks(thisMatch).Name 'get the name in case
I need it

If thisVal > 0 Then
Set BMRange = wrdDoc.Bookmarks(BookMarkName).Range '***
runtime error 13 type mismatch ***
BMRange.Text = ""
.Bookmarks.Add BookMarkName, BMRange
End If

WordArray(thisMatch) = ""

Next
wrdDoc.SaveAs (CurrentPath & ApplicantName & ".doc")
.Close ' close the document
End With

wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
ActiveWorkbook.Saved = True
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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