How to make multiple copies from a word template

I have created a word template. I want to fill a table in the template with the details i retrieve from ms Access database. The code I have written is to fill only one document which i later want to change to suit multiple records 

Dim oWordDoc As Word.Document    

Set oWordDoc = GetObject("C:\Users\user\Desktop\Access2Word.dotx")

With oWordDoc

     .Bookmarks("TName").Range.Text = "L.Venkatarao"
    .Bookmarks("Toffice").Range.Text = "ZPHS, Palasapuram"
    '.Bookmarks("EmpId").Range.Text = "0125183"
    .Bookmarks("TdateofBirth").Range.Text = "03-07-1981"
    .Bookmarks("TPolicyNumber").Range.Text = "L-420421422"
    .Bookmarks("TPremiumEnhancement").Range.Text = "150"
    .SaveAs "C:\Users\user\Desktop\Sivasri.docx"
    .Close
  End With 

Set oWordDoc = Nothing

when i save it with SaveAs, the new document is not opening. when i save it with .save, the changes are saved in the original template. but i can't get a copy.

Please give ideas

By the way, i am totally new to ms word vba


Was this discussion helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this discussion?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this discussion?

Thanks for your feedback.

Don't know why you need vba.

Mail Merge

Volunteering to "pay forward" the help I've received in the Microsoft user community.


Charles Kenyon
Sun Prairie, Wisconsin
wordfaq[at]addbalance[dot]com

Legal site: https://addbalance.com

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I am automating the process from Ms Access. So, i am using vba to write the macro. if there is any other easier way, Please inform.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I don' use Access, sorry.

Look into Mail Merge anyway. You can run a mail merge via vba but need to know how it works first.

Volunteering to "pay forward" the help I've received in the Microsoft user community.


Charles Kenyon
Sun Prairie, Wisconsin
wordfaq[at]addbalance[dot]com

Legal site: https://addbalance.com

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Nothing looks right about your code.  For the purpose that you are talking about, it would be better to insert { DOCVARIABLE fieldname } fields in the document where you want the information to appear and use code similar to:

Dim objDoc As Object

Dim objWord As Object

Dim bolOpenedWord As Boolean

Dim i As Long

Dim rs As DAO.Recordset

Set rs = CurrentDb.OpenRecordset("[name of Access table or query]")

If rs.RecordCount = 0 Then

    Exit Sub

End If

On Error Resume Next

Set objWord = GetObject(, "Word.Application")

If Err.Number = 429 Then

    Set objWord = CreateObject("Word.Application")

    bolOpenedWord = True

End If

On Error GoTo 0

rs.MoveFirst

Do Until rs.EOF

    Set objDoc = objWord.Documents.Add("C:\Users\user\Desktop\Access2Word.dotx"), False)

    With objDoc

        For i = 0 To rs.Fields.Count - 1

            If rs.Fields(i).Value <> "" Then

                .variables(rs.Fields(i).Name).Value = rs.Fields(i).Value

            Else

                .variables(rs.Fields(i).Name).Value = " "

            End If

        Next i

        .Range.Fields.Update

        .SaveAs2 "C:\Users\user\Desktop\" & rs.Fields(0).Value & ".docx" 

' Field number starting from 0 of the field that contains the data to be used as the filename

' I WOULD MOST STRONGLY SUGGEST THAT YOU DO NOT SAVE THE DOCUMENTS TO THE DESKTOP!

        .Close

    End With

    rs.MoveNext

Loop

Set objDoc = Nothing

If bolOpenedWord = True Then

    objWord.Quit

End If

Set objWord = Nothing

Set objDoc = Nothing

rs.Close

Set rs = Nothing

Different code will be required if you want to merge the data from multiple records into a table in the document

There is a utility for doing that (Many to One Merge) on the MergeTools – 20150309.dotm Add-in that you can download from the following page of my One Drive:

http://bit.ly/1hduSCB 


The MergeTools – 20150309.dotm file needs to be saved in the Word Startup folder.  In Windows Vista and Windows 7, 8 or 8.1, the default location for that folder is

C:\Users\[User Name]\AppData\Roaming\Microsoft\Word\STARTUP

If you do not see the AppData folder: -

In Windows 7, - In Windows Explorer, click on the Organize drop down and then on Folder and search options and in the Folder Options dialog, go to the View tab and select the item "Show hidden files, folders, and drives".  While there, it is a good idea to uncheck the box of "Hide extensions for known file types".

In Windows 8 and 8.1, in the File Explorer, click on Options on the View tab of the ribbon and then on the View tab in the dialog that appears and select the item "Show hidden files, folders, and drives".  While there, it is a good idea to uncheck the box of "Hide extensions for known file types".

When that has been done and Word is started\re-started, the tab shown below will be added to the Ribbon:



The requirements for using the system are:

  1. The mail merge main document must be of the Letter type, though that does not mean that the output cannot be sent as an e-mail message. 

  2. For the Many To One, Merge with Attachments and Merge to Individual Docs utilities, the data source may be either a table or query in an Access database, or in the form of an Excel worksheet and that worksheet must be the first sheet in the Excel workbook. If the data is on some other sheet, you can easily move that sheet so that it is the first sheet in the workbook by clicking on the sheet tab and dragging it to the left.  For the Chart Merge utility, download the Mail Merging with Charts document that is also on that page of my OneDrive for additional requirements of the data source for use with that utility

  3. For a data source in the form of an Excel worksheet, the field names must be in the first row of the worksheet and there must be a field name in all of the cells in that row that are within the range of columns that contain the data.

  4. For both types of data source, the field names must contain only alphanumeric characters (No @,#,$,%,&,(,), etc) and the field names must not start with a numeric character (0-9).

  5. For a data source in the form of an Excel worksheet, the data must start in the second row of the worksheet and there should be no empty rows within the range of data that is to be processed.

You may also want to download:

  1. the Merging with Attachments document that is also on that page which explains how the system is used.  It is not actually necessary to have separate attachments as the facility can be used to send just the documents created by the merge itself as attachments, either as the body of the message itself or in the form of Word files or .pdf files.

  2. the Mail Merging with Charts document that is also on that page.  That document explains how you must set up the Excel Data Source and the Mail Merge Main document to be able to execute a merge with a Chart that is unique to each record in the data source.

  3. the Using the Many to One Facility document that describes how to use that facility.

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

So nice of you to give such a detailed answer. Actually i am studying now about { DOCVARIABLE fieldname } to fully appreciate and derive benefit from this code. So, i am late in thanking you. My apologies for that.

Once again thank you for this answer. I am lost and you showed the way now

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Just now i learnt about { DOCVARIABLE fieldname } . And the code worked like a charm. Thanks a lot for that.

But Run-time error:438 (Object does not support this property or method) is occuring on the line .saveas2 method.

Whereas when i replaced it with .saveas method, it is working fine. (I am using office 2007)

Please explain why you chose .saveas2 over .saveas method. I wish to know the difference

And also why are you so over-cautious in the following code

 If rs.Fields(i).Value <> "" Then

        .variables(rs.Fields(i).Name).Value = rs.Fields(i).Value

  Else

          .variables(rs.Fields(i).Name).Value = " "

  End If

why not a direct statement like the one in the if-block without checking for any condition. What kind of errors it is likely to run into without such condition?

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

SaveAs2 was introduced in Office 2010.

The reason for using the

 If rs.Fields(i).Value <> "" Then

        .variables(rs.Fields(i).Name).Value = rs.Fields(i).Value

  Else

          .variables(rs.Fields(i).Name).Value = " "

  End If

construction is to prevent "Error! No Document variable supplied" appearing in any DocVariable fields for which the corresponding data in the data source is Null or empty.

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Discussion Info


Last updated September 30, 2021 Views 496 Applies to: