VBA (from Excel) trying to manipulate bookmarks- want to retain empty bookmark(s)

K

Keith

I'm using code (below) to access a Word file from Excel. My goal is to use
information from Excel to delete unnecessary sections of a word document
(via bookmarks), then save that Word document under a new name.

The problem is somehow related to the section where I am deleting/clearing
bookmarks- I've narrowed it down to two main possibilities, but I'm not sure
which (and crashing the code from Excel forces me to reboot, which takes
forever and is frustrating). Any advice or suggestions would be greatly
appreciated!
Keith
Office2003

Possibility 1: I'm still somehow deleting the bookmark, so when I delete
enough of them, I eventually try to delete one with an index that is now out
of range

Possibility 2: despite my attempts to always set everything to option base 1
(easier for me to understand as a non-programmer) maybe the array of
bookmarks has a base of zero, which would cause problems only for documents
where I try to clear/delete the last bookmark in the document

(or both)

code snippet:
------------------------------
Sub MakeGuideA()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")

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

'incoming array of bookmarks to delete are not in order
'find the largest one
thisVal = Application.WorksheetFunction.Max(WordArray)
'figure out where in the array it is
thisMatch = Application.Match(thisVal, WordArray, 0)
'delete the bookmark
If thisVal > 0 Then
wrdDoc.Bookmarks(thisMatch).Range.Text = "" 'Range.Delete
End If
'remove that value from the array so it isn't found again
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
 
H

Helmut Weber

Hi Keith
Possibility 1: I'm still somehow deleting the bookmark, so when I delete
enough of them, I eventually try to delete one with an index that is now out
of range

yes,

use a loop like this:
For deleteComp = 18 To 1 step -1

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
K

Keith

Helmut- thank you for your reply.

I've done additional testing to confirm that the bookmarks are deleted even
when I just set the bookmark.range.text to "" or " ".

Is there any way to clear the text of a bookmark's range, without removing
the bookmark itself? Part of the problem is that I won't necessarily be
deleting the bookmarks in order, so if I can leave the bookmark as part of
[bookmarks.count] then I won't have to maintain one or more additional
arrays to keep track of which bookmarks have been deleted and calculate the
new number for the next bookmark to be deleted- it would just be a more
elegant solution.

So how can I delete the text within a bookmark without deleting the bookmark
itself, since a blank space isn't even keeping it?

Thanks!
Keith
 
K

Keith

I've tried to further adapt the code to incorporate a snippet from the MVP
site, but I get a runtime error 13 type mismatch on the line marked below.
Still struggling and would appreciate any help at all-
Thanks!
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 'Step -1

Bkmksize = wrdDoc.Bookmarks.Count
thisVal = Application.WorksheetFunction.Max(WordArray)
thisMatch = Application.Match(thisVal, WordArray, 0)

If thisVal > 0 Then
Set BMRange = wrdDoc.Bookmarks(thisMatch).Range 'TYPE
MISMATCH
BMRange.Text = ""
wrdDoc.Bookmarks.Add thisMatch, BMRange

'wrdDoc.Bookmarks(thisMatch).Range.Text = " " 'Range.Delete
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
 
B

Bear

Keith:

Don't know if you're still working on this, but I was curious about how to
delete a bookmarks range without deleting the bookmark itself. So far, it
doesn't seem possible. Everything I've see uses the strategy of saving the
bookmark name, dinking with the range, then recreating the bookmark right
away.

Here's a lashed-together example just to illustrate the kind of code I'm
seeing. This particular code goes into a loop, as the next bookmark is always
the one I just added.

Dim objBkmk As Bookmark
Dim strName As String

For Each objBkmk In ActiveDocument.Bookmarks
If objBkmk.Name = "BearTest" Then
strName = objBkmk.Name
objBkmk.Select
objBkmk.Range.Text = ""
ActiveDocument.Bookmarks.Add Name:=strName
End If
Next objBkmk

Bear
 

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