Beim hantieren mit Bookmarks sollte man mit einer Hilfsfunktion arbeiten. Das erleichtert den Umgang ungemein - das Logging ist nur beispielhaft.
Beispiel:
Option Explicit
Public Sub Test()
Dim wdApp As Object 'Word.Application
Dim wdDoc As Object 'Word.Document
Set wdApp = CreateObject("Word.Application")
Set wdDoc = ...
' verdeutlicht das Verhalten bei mehrfachem Setzen der selben Textmarke
Call SetBookmark(wdDoc, "Anrede", "neuerText_A", True)
Call SetBookmark(wdDoc, "Anrede", "neuerText_B")
Call SetBookmark(wdDoc, "Anrede", "neuerText_C")
Call SetBookmark(wdDoc, "Geburtsdatum", Range("Geburtsdatum").Text)
Call SetBookmark(wdDoc, "Lehrgangsbeginn", Range("Geburtsdatum").Text)
...
End Sub
Public Function SetBookmark(WordDocument As Object, BookmarkName As String, NewText As String, Optional Preserve_ As Boolean = False) As Boolean
If WordDocument Is Nothing Then
Debug.Print Time$, "SetBookmark(BookmarkName:='" & BookmarkName & "') failed because argument 'WordDocument' is Nothing"
Exit Function
End If
If Not WordDocument.Bookmarks.Exists(BookmarkName) Then
Debug.Print Time$, "SetBookmark(BookmarkName:='" & BookmarkName & "') failed because it does not exist"
Exit Function
End If
Debug.Print Time$, "SetBookmark(BookmarkName:='" & BookmarkName & "') ... ";
Dim rng As Object 'Word.Range
Set rng = WordDocument.Bookmarks(BookmarkName).Range
rng.Text = NewText
If Preserve_ Then
Call WordDocument.Bookmarks.Add(BookmarkName, rng)
End If
Debug.Print "successful"
SetBookmark = True
End Function
|