Transferring Input on excel userform to word template via bookmarks

Desmondo

Board Regular
Joined
Feb 27, 2013
Messages
70
I have set up a nice userform for some work related stuff, but for the last few weeks i have been losing hair rapidly trying to figure how to transfer data between the excel userform and my word templates. Most articles on the web suggest using word bookmarks which i have attempted but always get errors. I have all the ms office libraries references etc checked.

I have labelled all my word templates at the appropriate area with an appropriate bookmark. I have labelled all form controls in excel userform with the same and assigned the code to a command button.

I am able to open the documents just fine from excel but nothing transfer moves, i have had various errors in all my attempts. The end goal is that i open the correct template and populate the bookmarks, make a copy and save to another directory in windows. I have two userform one is a menu that launches other userforms depending on selection all userform share some of the same information like name, address, ref, etc etc with the exception of form 1 which is just a menu.

Can anyone provide me an easy to follow example this would be very much appreciated.

The code i have so far which opens the docs

Code:
Private Sub PSave_Click()
'Declare obj variables for the word application and document.
      Dim WdApp As Object, wddoc As Object
'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\desmo\Desktop\New folder (5)\Work docs\UC372.dotx"
'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 & _
    "C:\Users\desmo\Desktop\New folder (5)\Work docs\UC372.dotx", _
    vbExclamation, _
   "Sorry, that document name does not exist."
Exit Sub
End If
'Activate the Word application.
    WdApp.Activate
'Set the Object variable for the Word document's full name and folder path.
    Set wddoc = WdApp.Documents(strDocName)
'If the Word document is not already open, then open it.
   If wddoc Is Nothing Then Set wddoc = WdApp.Documents.Open(strDocName)
'The document is open, so activate it.
   wddoc.Activate
'Release system memory that was reserved for the two Object variables.
  Set wddoc = Nothing
  Set WdApp = Nothing
  Run "OP"
End Sub
 
Last edited by a moderator:
Re: Transposing Input on excel userform to word template via bookmarks

Ah got it now, didn't think it was doing anything but alt f9 did the trick. Thanks again
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Re: Transposing Input on excel userform to word template via bookmarks

With the code you're using, there's no reason for the destination bookmark to be empty from the outset; indeed, having something there makes it easier to locate both the bookmark and the cross-reference.

Regardless, you can insert an empty bookmark, via Insert|Bookmark, then a cross-reference to it via Insert|Cross-reference. Toggling the field code display on/off via Alt-F9 will show that your cross-references are being inserted.

Hi Again and sorry for being a pest. I am trying this code but its not updating the bookmarks nor the ref's. I shifted the update bookmarks() to another module and i am calling it from there, i believe correctly. The docs is opening and saving no problem to the correct directory etc. But bookmarks etc remain unchanged.

Am i missing something??

Code:
'Declare obj variables for the word application and document.Dim wdApp As Object, wdDoc As Object
'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.


    Dim strFolderName As String
    Dim strSurname As String
    Dim strNino As String
    Dim strFilePath As String
    Dim Partner As String
    Dim Clmt As String
    Dim Couple As String
    Dim aPPPath As String
    
  
'Get Surname from control
    strSurname = Me.Surname
    ' Get nino  from from control
    strNino = Me.Nino


    strFolderName = strSurname & " " & strNino
    MsgBox "Folder created in Archive"
    'strFilePath = strParent & strFolderName & "\"
    aPPPath = Application.ThisWorkbook.Path & "\" & "Archive" & "\" & strFolderName & "\"


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 = Application.ThisWorkbook.Path & "\" & "Templates" & "\" & "DupC.dotx"
'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 Folder Was Created successfully in Directory " & vbCrLf & _
    "was not found in the folder path" & vbCrLf & _
    "\\DFZ70406.link2.gpn.gov.uk\70805003$\OPTool 191017\op.", _
    vbExclamation, _
    "Sorry, that document name does not exist."
End If


'Create the Word document from the template.
    Set wdDoc = wdApp.Documents.Add(strDocName)
    
    'Variable values to determine if couple or not for decision completion
    
    Clmt = ComboBox1.Value & " " & Surname.Value
    Partner = ComboBox2.Value & " " & PSurname.Value
    Couple = Clmt & " & " & Partner
    
    'Populate the Word Documents Bookmarks
'


With wdDoc


    Call Module1.UpdateBookmark("OPAmount", OPAmount.Value)
    Call Module1.UpdateBookmark("ClaimDate", ClaimDate.Value)
    Call Module1.UpdateBookmark("Clmts", Clmts.Value)
    Call Module1.UpdateBookmark("CorrectAmount", CorrectAmount.Value)
    Call Module1.UpdateBookmark("CorrectDate", CorrectDate.Value)
    Call Module1.UpdateBookmark("iDate", iDate.Value)
    Call Module1.UpdateBookmark("iPayt", iPyt.Value)
    Call Module1.UpdateBookmark("opStartDate", opStartDate.Value)
    Call Module1.UpdateBookmark("opEndDate", opEndDate.Value)
  
    'Update the Word document's cross-references.
    wdDoc.Fields.Update


'    .BookMarks("Title").Range.Text = ComboBox1.Value
'    .BookMarks("Forename").Range.Text = Forename.Value
'    .BookMarks("Surname").Range.Text = Surname.Value
'    .BookMarks("Nino").Range.Text = Nino.Value
'    .BookMarks("doc").Range.Text = ClaimDate.Value
'    .BookMarks("apStart").Range.Text = apStart.Value
'    .BookMarks("apEnd").Range.Text = apEnd.Value
'    .BookMarks("iPyt").Range.Text = iPyt.Value
'    .BookMarks("opStartDate").Range.Text = opStartDate.Value
'    .BookMarks("opEndDate").Range.Text = opEndDate.Value
'    .BookMarks("Title1").Range.Text = ComboBox1.Value
'    .BookMarks("iDate").Range.Text = iDate.Value
'    .BookMarks("CorrectAmount").Range.Text = CorrectAmount.Value
'    .BookMarks("CorrectDate").Range.Text = CorrectDate.Value
'    .BookMarks("iDate1").Range.Text = iDate.Value
'    .BookMarks("iPyt1").Range.Text = iPyt.Value
'    .BookMarks("iDate2").Range.Text = iDate.Value
'    .BookMarks("Title2").Range.Text = ComboBox1.Value
'    .BookMarks("Surname1").Range.Text = Surname.Value
'    .BookMarks("Surname2").Range.Text = Surname.Value
'    .BookMarks("apStart1").Range.Text = apStart.Value
'    .BookMarks("apEnd1").Range.Text = apEnd.Value
'    .BookMarks("iPyt2").Range.Text = iPyt.Value
'    .BookMarks("Title3").Range.Text = ComboBox1.Value
'    .BookMarks("Surname3").Range.Text = Surname.Value
'    .BookMarks("AddressL1").Range.Text = AddressL1.Value
'    .BookMarks("AddressL2").Range.Text = AddressL2.Value
'    .BookMarks("AL3").Range.Text = TextBox38.Value
'    .BookMarks("PCode").Range.Text = Pcode.Value
'    .BookMarks("PTitle").Range.Text = ComboBox2.Value
'    .BookMarks("PForename").Range.Text = PForename.Value
'    .BookMarks("PSurname").Range.Text = PSurname.Value
'    .BookMarks("PNino").Range.Text = PNino.Value
'    .BookMarks("PTitle1").Range.Text = ComboBox2.Value
'    .BookMarks("PForename1").Range.Text = PForename.Value
'    .BookMarks("PSurname1").Range.Text = PSurname.Value
'
'
'    If CheckBox11.Value = True Then
'    .BookMarks("Clmts").Range.Text = Couple
'    .BookMarks("Clmts1").Range.Text = Couple
'    .BookMarks("Clmts2").Range.Text = Couple
'    .BookMarks("Clmts3").Range.Text = Couple
'
'    Else
'    .BookMarks("Clmts").Range.Text = Clmt
'    .BookMarks("Clmts1").Range.Text = Clmt
'    .BookMarks("Clmts2").Range.Text = Clmt
'    .BookMarks("Clmts3").Range.Text = Clmt
  
        
    'End If
           
         
End With
    
    
     wdDoc.SaveAs aPPPath & "OP Decision.doc"
     wdDoc.Close True
    


'Release system memory that was reserved for the two Object variables.
    Set wdRng = Nothing: Set wdDoc = Nothing:  Set wdApp = Nothing
'Close Word Doc
    Call CloseWordDocuments
'Open created Folder
'Shell "explorer.exe " & strFilePath, vbNormalFocus


Call UC372




End Sub
 
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

1. You appear to have commented out:
Dim wdApp As Object, wdDoc As Object
with the result they're now undefined. Likewise, nowhere is wdRng defined.
2. With the UpdateBookmark sub in another module, you'll need to pass wdDoc to that as part of the call, e.g.:
Call Module1.UpdateBookmark(wdDoc, "OPAmount", OPAmount.Value)
3. The UpdateBookmark likewise needs to be modified to accept the wdDoc parameter (and have wdRng defined):
Code:
Sub UpdateBookmark(wdDoc As Object, strBkMk As String, strTxt As String)
Dim wdRng As Object
With wdDoc
  If .Bookmarks.Exists(strBkMk) Then
    Set wdRng = .Bookmarks(strBkMk).Range
    wdRng.Text = strTxt
    .Bookmarks.Add strBkMk, wdRng
  End If
End With
Set wdRng = Nothing
End Sub
4. Since wdRng isn't used or defined in your useform's sub, you should delete:
Set wdRng = Nothing
from there.
 
Last edited:
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

Your the man as always all working now except one little bit i had working the other way.

Sometime the case i deal with is a couple and in that case a checkbox is ticked to add their details. I declared a bookmark clmts in my word template. And three variables in my macro like so

Code:
Clmt = ComboBox1.Value & " " & Surname.Value    Partner = ComboBox2.Value & " " & PSurname.Value
    Couple = Clmt & " & " & Partner

And had an if else statement like so

Code:
If CheckBox11.Value = True Then'    .BookMarks("Clmts").Range.Text = Couple
'    .BookMarks("Clmts1").Range.Text = Couple
'    .BookMarks("Clmts2").Range.Text = Couple
'    .BookMarks("Clmts3").Range.Text = Couple
'
'    Else
'    .BookMarks("Clmts").Range.Text = Clmt
'    .BookMarks("Clmts1").Range.Text = Clmt
'    .BookMarks("Clmts2").Range.Text = Clmt
'    .BookMarks("Clmts3").Range.Text = Clmt
  
        
    'End If

But i cannot fathom how i would place this in the new code, any help greatly appreciated.
 
Last edited by a moderator:
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

Its sill works under old way for that section/

Thanks
 
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

Infact doesn't only does the bookmark not the refs
 
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

All you need in the document is a single 'Clmts' bookmark, with cross-references to it from where you now have the Clmts1, Clmts2, & Clmts3 bookmarks.
For the code, all you then need is:
Code:
Clmt = ComboBox1.Value & " " & Surname.Value
If CheckBox11.Value = True Then Clmt = Clmt & " & " & ComboBox2.Value & " " & PSurname.Value 
Call Module1.UpdateBookmark(wdDoc, "Clmts", Clmt)
 
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

Top man, all working great.
 
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

Macropod I tell you what you are **** good at this stuff. Don't know what i would do without you I have read a few books on this and in the middle of an online course. But feel like I only learn when putting it into practice. Thanks for all your patience.

I hope you don't mind me asking you one more thing, something i thought would have been simple but turns out to be far from it. Scouted every where to no avail.

Some of my bookmarks are in a letter and are filled with the user form text boxes. I wanted to present some of them in the format dd/mmm/yyyy but do not want to use date pickers as think this slows down input. Is there away you know off for me to update the bookmarks that are dates to this format in word or from excel vba.
 
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

Simply include the required format as part of the call to UpdateBookmark. For example:
Call Module1.UpdateBookmark(wdDoc, "OPDate", Format(OPDate.Value, "dd/mmm/yyyy"))
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,206
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top