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

Wow you are amazing thanks
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Re: Transposing Input on excel userform to word template via bookmarks

Hi Macropod, i know this is late on but i would like to try and progress this a little further to include data a from a range in a spreadsheet and insert that into a bookmark, have been playing a bit with the code you provided me with but cannot seem to crack this part. The range is created on a submit of userForm and is dynamic in that each submission can be in varying length but always column A:C in the same workbook in a sheet called "Store". The bookmark is called "Breakdown", no matter what i try it does not work and no error.

Code:
Sub uc371()

'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 answer As Variant
    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
    Dim wb As Workbook ' Latest
    Dim ws As Worksheet 'Latest
    Dim rng As Range 'Latest
    
        


'Get Surname from control
    strSurname = Me.Surname
    ' Get nino  from from control
    strNino = Me.Nino


    strFolderName = strSurname & " " & strNino
    
    strFilePath = Application.ThisWorkbook.Path & "\" & "Archive" & "\" & strFolderName & "\"
    CreateObject("WScript.Shell").PopUp "Creating your UC371!", 1
           


    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" & "\" & "UC371.dotx"


'End If


'Create the Word document from the template.
Set wdDoc = wdApp.Documents.Add(strDocName)
Set wb = ThisWorkbook
Set ws = ThisWorkbook("Store")
Set rng = ws("UsedRange")




    
    'Variable values to determine if couple or not for decision completion


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




With wdDoc
    
    Call Module1.UpdateBookmark(wdDoc, "Title", ComboBox1.Value)
    Call Module1.UpdateBookmark(wdDoc, "ForeName", Forename.Value)
    Call Module1.UpdateBookmark(wdDoc, "SurName", Surname.Value)
    Call Module1.UpdateBookmark(wdDoc, "Nino", Nino.Value)
    Call Module1.UpdateBookmark(wdDoc, "AddressL1", AddressL1.Value)
    Call Module1.UpdateBookmark(wdDoc, "AddressL2", AddressL2.Value)
    Call Module1.UpdateBookmark(wdDoc, "AddressL3", AddressL3.Value)
    Call Module1.UpdateBookmark(wdDoc, "PCode", Pcode.Value)
    Call Module1.UpdateBookmark(wdDoc, "OPAmount", OPAmount.Value)
    Call Module1.UpdateBookmark(wdDoc, "iDate", Format(iDate.Value, "dd mmmm yyyy"))
    Call Module1.UpdateBookmark(wdDoc, "opStartDate", opStartDate.Value)
    Call Module1.UpdateBookmark(wdDoc, "opEndDate", opEndDate.Value)
    Call Module1.UpdateBookmark(wdDoc, "Reason", Reason.Value)
    Call Module1.UpdateBookmark(wdDoc, "RevisedAmt", Revise****t.Value)
    Call Module1.UpdateBookmark(wdDoc, "Breakdown", Sheets("Store").UsedRange)
        'Update the Word document's cross-references.
    wdDoc.Fields.Update
       
          
End With
    answer = MsgBox("Do you wish to Print the UC371 before saving?", vbYesNo + vbQuestion, "Print")


    If answer = vbYes Then
    wdDoc.PrintOut
    Else
    'do nothing
     End If
         
     wdDoc.SaveAs strFilePath & "UC371.doc"
     wdDoc.Close True
     
  


'Release system memory that was reserved for the two Object variables.
    Set wdDoc = Nothing
    Set wdApp = Nothing
'Close Word Doc
    Call CloseWordDocuments
'Open created Folder


    'Call Module1.print_preview
      
    


    Shell "explorer.exe " & strFilePath, vbNormalFocus


 


End Sub

I hope you can help me, here is the code i have tried but nothing happening. Would really appreciate it as would let me finish this project.
 
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

If your range is to be inserted as a table, you'll need to use either copy/paste or, if you start of with a basic table in-situ in the Word document, populate it,one Excel cell at a time, and add rows on the fly. If your range is not to be inserted as a table, you'll need to either copy/paste as a table, then convert the table to text, or write it out one Excel cell at a time.
 
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

If your range is to be inserted as a table, you'll need to use either copy/paste or, if you start of with a basic table in-situ in the Word document, populate it,one Excel cell at a time, and add rows on the fly. If your range is not to be inserted as a table, you'll need to either copy/paste as a table, then convert the table to text, or write it out one Excel cell at a time.

Thanks for the speedy reply the data is in a table and I want to copy the full thing over. I managed to copy it paste it to a blank word doc. But need to insert it at a specific bookmark. Thanks for the pointers I will give it a go
 
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

Try something like:
Code:
Sub PasteBookMark(wdDoc As Object, BkMkNm As String)
Dim BmkRng As Range
With wdDoc
  If .Bookmarks.Exists(BkMkNm) Then
    Set BmkRng = .Bookmarks(BkMkNm).Range
    With BmkRng
        .Delete
        .Paste
    End With
    .Bookmarks.Add BkMkNm, BmkRng
  End If
End With
Set BmkRng = Nothing
End Sub
which you'd call with code like:
Call PasteBookMark(wdDoc, "BkMk")
after copying the source range to the clipboard.
 
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

Been trying this all day but still no joy, i sort of understand what you are doing. But getting compile error, wrong number of arguments or invalid property assignment. Not sure why

Code:
''''' Paste excel tablesSub PasteBookMark(wdDoc As Object, BkMkNm As String)
Dim BmkRng As Range
With wdDoc
  If .Bookmarks.Exists(BkMkNm) Then
    Set BmkRng = .Bookmarks(BkMkNm).Range
    With BmkRng
        .Delete
        .Paste
    End With
    .Bookmarks.Add BkMkNm, BmkRng
  End If
End With
Set BmkRng = Nothing
End Sub

And the rest of the code

Code:
b uc371()

'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 answer As Variant
    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
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim tbl As Excel.Range
                   
    
        


'Get Surname from control
    strSurname = Me.Surname
    ' Get nino  from from control
    strNino = Me.Nino


    strFolderName = strSurname & " " & strNino
    
    strFilePath = Application.ThisWorkbook.Path & "\" & "Archive" & "\" & strFolderName & "\"
    CreateObject("WScript.Shell").PopUp "Creating your UC371!", 1
           


    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" & "\" & "UC371.dotx"


'End If


    
    'Variable values to determine if couple or not for decision completion


    Clmt = ComboBox1.Value & " " & Surname.Value
    Partner = ComboBox2.Value & " " & PSurname.Value
    Couple = Clmt & " and " & Partner
    
    Set wb = ThisWorkbook
    Set ws = Sheets("Store")
    Set tbl = ws.Range("A1:C1", Range("A" & Rows.Count).End(xlUp))
    tbl.Copy
        
                




With wdDoc
    
    Call Module1.UpdateBookmark(wdDoc, "Title", ComboBox1.Value)
    Call Module1.UpdateBookmark(wdDoc, "ForeName", Forename.Value)
    Call Module1.UpdateBookmark(wdDoc, "SurName", Surname.Value)
    Call Module1.UpdateBookmark(wdDoc, "Nino", Nino.Value)
    Call Module1.UpdateBookmark(wdDoc, "AddressL1", AddressL1.Value)
    Call Module1.UpdateBookmark(wdDoc, "AddressL2", AddressL2.Value)
    Call Module1.UpdateBookmark(wdDoc, "AddressL3", AddressL3.Value)
    Call Module1.UpdateBookmark(wdDoc, "PCode", Pcode.Value)
    Call Module1.UpdateBookmark(wdDoc, "OPAmount", OPAmount.Value)
    Call Module1.UpdateBookmark(wdDoc, "iDate", Format(iDate.Value, "dd mmmm yyyy"))
    Call Module1.UpdateBookmark(wdDoc, "opStartDate", opStartDate.Value)
    Call Module1.UpdateBookmark(wdDoc, "opEndDate", opEndDate.Value)
    Call Module1.UpdateBookmark(wdDoc, "Reason", Reason.Value)
    Call Module1.UpdateBookmark(wdDoc, "RevisedAmt", Revise****t.Value)
    Call Module1.UpdateBookmark(wdDoc, "EarningsTotal", APCalculator.EarningsTotal.Value)
    Call Module1.PasteBookMark(wdDoc, "BreakDown", tbl.PasteSpecial)
     
         
        
        'Update the Word document's cross-references.
    wdDoc.Fields.Update
       
          
End With


     wdDoc.SaveAs strFilePath & "UC371.doc"
     wdDoc.Close True
     
  


'Release system memory that was reserved for the two Object variables.
    Set wdDoc = Nothing
    Set wdApp = Nothing
'Close Word Doc
    Call CloseWordDocuments
 
Last edited:
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

It's hardly surprising it doesn't work - you're not passing the required parameters. As per post #25 , there are only 2, but you're trying to pass 3...
 
Upvote 0
Do you mean like this without the wdObject as that already referenced in the the with wdDoc statement?

Call Module1.PasteBookMark("BreakDown", tbl.PasteSpecial) if so its a type mismatch?
 
Upvote 0
Re: Transposing Input on excel userform to word template via bookmarks

It goes through without error but doesn't produce the documents. Sort of just flashes and then disappears.

Hold on its something else unrelated to your code
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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