Private
Sub
cmdtest_Click()
Dim
wrdApp
As
Word.Application
Dim
wrdDoc
As
Word.Document
Dim
ctrlText1
As
Word.ContentControl
Dim
Pfad
As
String
Pfad = ThisWorkbook.Path &
"\Vorlage2.docx"
On
Error
GoTo
Err_Handler
Set
wrdApp = CreateObject(
"Word.Application"
)
Set
wrdDoc = wrdApp.Documents.Open(Pfad)
wrdApp.Visible =
False
Set
ctrlText1 = SearchContentControl(wrdDoc,
"Text1"
)
If
Not
ctrlText1
Is
Nothing
Then
ctrlText1.Range.Text =
"Test erfolgreich"
End
If
Pfad = ThisWorkbook.Path &
"\Testpfad\" & "
Fertig
" & "
.pdf"
wrdDoc.Save
wrdDoc.Close
Err_Exit:
If
Not
wrdAp
Is
Nothing
Then
wrdApp.Quit
True
End
If
Set
wrdDoc =
Nothing
Set
wrdApp =
Nothing
Exit
Sub
Err_Handler:
MsgBox
"Es ist ein Fehler aufgetreten."
, vbInformation
Err.Clear
Resume
Err_Exit
End
Sub
Function
SearchContentControl(Doc
As
Word.Document, Tag
As
String
)
As
Word.ContentControl
Dim
ctrl
As
Word.ContentControl
For
Each
ctrl
In
Doc.ContentControls
If
ctrl.Tag = Tag
Then
Set
SearchContentControl = ctrl
Exit
For
End
If
Next
End
Function