E
Enda
Hi,
I would appreciate help with the following.
I have a hidden bookmark in a document. I wish to delete all content
in the document up that bookmark.
The code I started with is below. It throws an error '5904 Cannot edit
range'. This might be when it reaches a table or a field. Cannot
anyone suggest improvements?
Thanks,
Enda
'******************************************************************
' Removes all text up to a given bookmark. Does not save the
' altered document.
'
' bmark - the bookmark up to which everything is deleted
' doc - the document to operate on
'******************************************************************
Public Sub removeBeforeBookmark(bmark As Bookmark, doc As Document)
On Error GoTo ErrHandler
Dim para As Paragraph
Dim nextPar As Paragraph
Set para = doc.Paragraphs.First
Do Until para.Range.Bookmarks.Exists(bmark.Name) = True
Set nextPar = para.Next
para.Range.Delete
Set para = nextPar
Loop
Exit Sub
ErrHandler:
Dim routine As String
routine = "removeBeforeBookmark "
Select Case Err.Number
'Cannot Edit range
'Case 5904
Case Else
MsgBox routine _
& vbNewLine & Error(Err) _
& vbNewLine & Err.Number _
& vbNewLine & Err.Description _
& vbNewLine & Err.Source
End Select
End Sub
I would appreciate help with the following.
I have a hidden bookmark in a document. I wish to delete all content
in the document up that bookmark.
The code I started with is below. It throws an error '5904 Cannot edit
range'. This might be when it reaches a table or a field. Cannot
anyone suggest improvements?
Thanks,
Enda
'******************************************************************
' Removes all text up to a given bookmark. Does not save the
' altered document.
'
' bmark - the bookmark up to which everything is deleted
' doc - the document to operate on
'******************************************************************
Public Sub removeBeforeBookmark(bmark As Bookmark, doc As Document)
On Error GoTo ErrHandler
Dim para As Paragraph
Dim nextPar As Paragraph
Set para = doc.Paragraphs.First
Do Until para.Range.Bookmarks.Exists(bmark.Name) = True
Set nextPar = para.Next
para.Range.Delete
Set para = nextPar
Loop
Exit Sub
ErrHandler:
Dim routine As String
routine = "removeBeforeBookmark "
Select Case Err.Number
'Cannot Edit range
'Case 5904
Case Else
MsgBox routine _
& vbNewLine & Error(Err) _
& vbNewLine & Err.Number _
& vbNewLine & Err.Description _
& vbNewLine & Err.Source
End Select
End Sub