Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Send current record to Word template

$
0
0
Hi all,

I'm trying to send information from access 2010 form to a 2010 word template/document (.docx)

I have followed this tutorial.
How to send the current record to Word 2000 with automation

Problem:
However, when I click on "Sent to word" button, something happens.
I realize it was written for MS Word 97 but I didn't see anything obvious in the code that would cause a problem with word 2010.

I only used "Name" and "Number" as bookmarks in the word document simply as a starting point to test.

I also commented out all the photo stuff as I will never need that.

Here's what I have as it stands. Doesn't throw any errors but nothing happens either.

Code:

Private Sub MergeButton_Click()
    On Error GoTo MergeButton_Err
    Dim objWord As Word.Application
   
    'Copy the Photo control on the Employees form.
  ' DoCmd.GoToControl "Photo"
    DoCmd****nCommand acCmdCopy

    'Start Microsoft Word 97.
    Set objWord = CreateObject("Word.Application")

    With objWord
        'Make the application visible.
        .Visible = True

        'Open the document.
        .Documents.Open ("C:\Users\Documents\myMerge.docx")

        'Move to each bookmark and insert text from the form.
        .ActiveDocument.Bookmarks("Name").Select
        .Selection.Text = (CStr(Me.Name))
        .ActiveDocument.Bookmarks("Number").Select
        .Selection.Text = (CStr(Me.Number))
'        .ActiveDocument.Bookmarks("Address").Select
'        .Selection.Text = (CStr(Forms!Employees!Address))
'        .ActiveDocument.Bookmarks("City").Select
'        .Selection.Text = (CStr(Forms!Employees!City))
'        .ActiveDocument.Bookmarks("Region").Select
'        .Selection.Text = (CStr(Forms!Employees!Region))
'        .ActiveDocument.Bookmarks("PostalCode").Select
'        .Selection.Text = (CStr(Forms!Employees!PostalCode))
'        .ActiveDocument.Bookmarks("Greeting").Select
'        .Selection.Text = (CStr(Forms!Employees!FirstName))

        'Paste the photo.
'        .ActiveDocument.Bookmarks("Photo").Select
'        .Selection.Paste
    End With

    'Print the document in the foreground so Microsoft Word will not close
    'until the document finishes printing.
    objWord.ActiveDocument.PrintOut Background:=False

    'Close the document without saving changes.
    objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

    'Quit Microsoft Word and release the object variable.
    objWord.Quit
    Set objWord = Nothing
    Exit Sub

MergeButton_Err:
    'If a field on the form is empty, remove the bookmark text, and
    'continue.
    If Err.Number = 94 Then
        objWord.Selection.Text = ""
        Resume Next

    'If the Photo field is empty.
'    ElseIf Err.Number = 2046 Then
'      MsgBox "Please add a photo to this record and try again."
'    Else
'        MsgBox Err.Number & vbCr & Err.Description
    End If

    Exit Sub
End Sub

Any help is greatly appreciated. Thanks! :beer:


edit: I think i got it figured out. Let me do some testing. Be back with results!

Viewing all articles
Browse latest Browse all 13329

Trending Articles