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
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