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
Helmut Weber - 16 Jan 2007 15:59 GMT
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

Signature
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
Keith - 16 Jan 2007 20:10 GMT
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
> Hi Keith
>
[quoted text clipped - 10 lines]
>
> HTH
Keith - 16 Jan 2007 20:37 GMT
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
Ed - 16 Jan 2007 21:37 GMT
Try
Dim BMRange As Word.Range
> 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.
[quoted text clipped - 43 lines]
> ActiveWorkbook.Saved = True
> End Sub
Keith - 17 Jan 2007 18:36 GMT
Thanks a million Ed! I never would have caught that :-)
> Try
> Dim BMRange As Word.Range
[quoted text clipped - 48 lines]
>> ActiveWorkbook.Saved = True
>> End Sub
Bear - 09 Mar 2007 22:00 GMT
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