For my work I need to register complaints in an excel database and then I need to enter the same data in a complaint form in words. I am new at VBA and followed a youtube tutorial to build a userform to transfer entered data to the database, but now I want to add a cmd button to transfer the same data to the word complaint form. How can I do this, I am stuck? I found this but it does not transfer the data to the bookmark.
'Declare obj variables for the word application and document.
Dim WdApp As Object, wdDoc As Object, wdRng As Object
Private Sub PSave_Click()
'Declare a String variable for the example document's name and folder path.
Dim strDocName As String
'On Error statement if Word is not already open.
On Error Resume Next
'Activate Word if it is already open.
Set WdApp = GetObject(, "Word.Application")
If Err.Number = 429 Then
Err.Clear
'Create a Word application if Word is not already open.
Set WdApp = CreateObject("Word.Application")
End If
'Make sure the Word application is visible.
WdApp.Visible = True
'Define the strDocName String variable.
strDocName = "C:\Users\Jasmijn\Desktop\FOR-3711 afwijkingenformulier blanco 190520-2021.docx"
'Check the directory for the presence of the document name in the folder path.
'If it is not recognized, inform the user and exit the macro.
If Dir(strDocName) = "" Then
MsgBox "The file UC372" & vbCrLf & _
"was not found in the folder path" & vbCrLf & strDocName, _
vbExclamation, _
"Sorry, that document name does not exist."
Exit Sub
End If
'Create the Word document from the template.
Set wdDoc = WdApp.Documents.Add(strDocName)
'Populate the Word document's bookmarks.
Call UpdateBookmark("Klachtcode", frmForm.txtKlachtcode.Value)
Call UpdateBookmark("Datum", frmForm.txtIndienerklacht.Value)
'Update the Word document's cross-references.
wdDoc.Fields.Update
'Release system memory that was reserved for the Object variables.
Set wdRng = Nothing: Set wdDoc = Nothing: Set WdApp = Nothing
End Sub
Sub UpdateBookmark(strBkMk As String, strTxt As String)
With wdDoc
If .Bookmarks.Exists(strBkMk) Then
Set wdRng = .Bookmarks(strBkMk).Range
wdRng.Text = strTxt
.Bookmarks.Add strBkMk, wdRng
End If
End With
End Sub
Bookmarks