Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004
    Posts
    7

    Arrow Unanswered: Form info merged to Word

    I am trying to merge information from my form and subform to a Microsoft Word document. I have made a "mymerge.doc and setup bookmarks for the information that is going to be replaced with info out of my form. The problem I am having is that all the information gets merged from the Form but the code can not jump to the subform and merge the info over from the subform. I need to get some code put in to get the info from the subform. CustomerFirstName and CustomerLastName are in the subform, this is the info that is popping up an error. I can only guess this is because it is in the subform. I just need some code to transition to the subform. Any ones help is greatly appreciated.

    Private Sub MergeButton_Click()
    On Error GoTo MergeButton_Err

    Dim objWord As Word.Application

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

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

    'Open the document.
    .Documents.Open ("C:\MyMerge.doc")

    'Move to each bookmark and insert text from the form.
    .ActiveDocument.Bookmarks("USWNumber").Select
    .Selection.Text = (CStr(Forms!Analyst!USWNumber))
    .ActiveDocument.Bookmarks("AnalystFirstName").Sele ct
    .Selection.Text = (CStr(Forms!Analyst!AnalystFirstName))
    .ActiveDocument.Bookmarks("Combo35").Select
    .Selection.Text = (CStr(Forms!Analyst!Combo35))
    .ActiveDocument.Bookmarks("Supervisor").Select
    .Selection.Text = (CStr(Forms!Analyst!Supervisor))
    ActiveDocument.Bookmarks("CustomerFirstName").Sele ct
    .Selection.Text = (CStr(Forms!Analyst!CustomerFirstName))
    ActiveDocument.Bookmarks("CustomerLastName").Selec t
    .Selection.Text = (CStr(Forms!Analyst!CustomerLastName))



    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
    Else
    MsgBox Err.Number & vbCr & Err.Description
    End If

    Exit Sub

    End Sub

  2. #2
    Join Date
    Oct 2003
    Location
    Roanoke, Va
    Posts
    445

    Smile Re: Form info merged to Word

    Originally posted by caseyoconnor10
    I am trying to merge information from my form and subform to a Microsoft Word document. I have made a "mymerge.doc and setup bookmarks for the information that is going to be replaced with info out of my form. The problem I am having is that all the information gets merged from the Form but the code can not jump to the subform and merge the info over from the subform. I need to get some code put in to get the info from the subform. CustomerFirstName and CustomerLastName are in the subform, this is the info that is popping up an error. I can only guess this is because it is in the subform. I just need some code to transition to the subform. Any ones help is greatly appreciated.

    Private Sub MergeButton_Click()
    On Error GoTo MergeButton_Err

    Dim objWord As Word.Application

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

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

    'Open the document.
    .Documents.Open ("C:\MyMerge.doc")

    'Move to each bookmark and insert text from the form.
    .ActiveDocument.Bookmarks("USWNumber").Select
    .Selection.Text = (CStr(Forms!Analyst!USWNumber))
    .ActiveDocument.Bookmarks("AnalystFirstName").Sele ct
    .Selection.Text = (CStr(Forms!Analyst!AnalystFirstName))
    .ActiveDocument.Bookmarks("Combo35").Select
    .Selection.Text = (CStr(Forms!Analyst!Combo35))
    .ActiveDocument.Bookmarks("Supervisor").Select
    .Selection.Text = (CStr(Forms!Analyst!Supervisor))
    ActiveDocument.Bookmarks("CustomerFirstName").Sele ct
    .Selection.Text = (CStr(Forms!Analyst!CustomerFirstName))
    ActiveDocument.Bookmarks("CustomerLastName").Selec t
    .Selection.Text = (CStr(Forms!Analyst!CustomerLastName))



    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
    Else
    MsgBox Err.Number & vbCr & Err.Description
    End If

    Exit Sub

    End Sub
    You want to access the control on the main form that is the subform and then through the form property of the subform you can refer to each control on the form.

    Me.SubformName.Form.ControlName

    That should get you to the info you need.
    Gregg


    DAO, ADO, SQL, Automation and anything else I can pick up.

  3. #3
    Join Date
    Mar 2004
    Posts
    7

    Re: Form info merged to Word

    OK...I apologize...I am very new to VB, could you ellaborate a little further. What exactly do you mean "access the control on the main form that is the subform"

  4. #4
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    If you type in Me. you should see all of the methods, properties and other controls on the form. If you let Access name the subform control then it will begin with 'sub'. So in the code for the form type me.sub and you should see the name of you subform.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •