Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: calling a Word doc from access


Message #1 by erezmor@n... on Sat, 28 Jul 2001 14:59:46
dear someone

how do i open a word document from access code?

and once opened, how do i activate a procedure (a macro) in that document?

i'm using word's mail-merge to produce a letter with data coming from the selected record in access

but i want the document shown (to the user) to be the merged one with all fields filled up, and 

not the original source.

hope i made myself clear

thanx

erez.
Message #2 by "Richard Lobel" <richard@a...> on Sat, 28 Jul 2001 14:40:42 -0700
Here is an example of the code I wrote to do a Word mail merge. The

code, opens an instance of Word, fills in the mail merge fields from the

data in Access, prints the document (you could show it instead), and

then closes the document without saving changes so that you don't end up

saving the merged document.



Private Sub cmdOK_Click()

    Dim intFrame As Integer

    Dim objWdApp As Object

    Dim objWd As Object

    Dim objDoc As Object

    Dim db As Database

    Dim rst As Recordset

    Dim rstType As Recordset

    Dim rstFish As Recordset

    Dim rstDues As Recordset

    Dim strMsg As String

    Dim strTypeString As String

    Dim strFishString As String

    Dim strTypeCriteria As String

    Dim strFishCriteria As String

    Dim strDocument As String

    Dim curDues As Currency

    

    On Error Resume Next

    strMsg = "Do you want to print all renewals?"

    

    intFrame = fraNotice.Value

    If MsgBox(strMsg, vbQuestion + vbYesNo) = vbYes Then

        'Get Word if it is already running and assign it an object

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

        If Err.Number = 429 Then 'Word is not running

            ' Open Word and assign it an object

            Err.Number = 0

            Set objWdApp = CreateObject("Word.Application")

            ' Word could not start

            If Err.Number = 429 Then

                MsgBox "Could not start Microsoft Word!", vbCritical

                Exit Sub

            End If

        End If

        

        If Err Then GoTo Err_cmdOK_Click

        'Assign an object to the running instance of Word

        Set objWd = objWdApp.Application

        'Handle other errors

        On Error GoTo Err_cmdOK_Click

        Set db = CurrentDb

        

'******************************************************

'Handle Associates Renewals

'******************************************************



        'check if we are running first or second notices

        'run the associates renewal query

        If intFrame = 1 Then

            Set rst = db.OpenRecordset("qryAssociateRenewal",

dbOpenDynaset)

        ElseIf intFrame = 2 Then

            Set rst 

db.OpenRecordset("qryAssociateRenewalSecondNotice", dbOpenDynaset)

        End If

        

        Set rstDues = db.OpenRecordset("SELECT Amount FROM tblDues WHERE

strCategory = 'Associate'", dbOpenSnapshot)

        curDues = rstDues!Amount

        'Get all the records

        With rst

            .MoveLast

            .MoveFirst





        Do Until .EOF

        'open the associate renewal document

        objWd.Documents.Open (fGetRightToken(Application.CurrentDb.Name)

& "Renew(A).doc")

        ' set a reference to the active document

        Set objDoc = objWd.ActiveDocument

       ' Fill in Company Name

        objDoc.Fields(1).Result.Text = !strName

        ' Fill in main Contact Name

        objDoc.Fields(2).Result.Text = !Contact



        'If the contact has a title fill it in

        If Not IsNull(!strTitle) Then

            objDoc.Fields(3).Result.Text = ", " & !strTitle

        End If



        ' Fill in Company Address

        If IsNull(!strAddress2) Then

            objDoc.Fields(4).Result.Text = Nz(!strAddress)

        Else

            objDoc.Fields(4).Result.Text = !strAddress & ", " &

!strAddress2

        End If



       ' Fill in city, state and zip

        objDoc.Fields(5).Result.Text = Nz(!CSZ)

       

        ' Fill in Phone Number

        objDoc.Fields(6).Result.Text = Nz(!strPhone1Cnty) &

Nz(!strPhone1City) & !strPhone1

        

        ' Fill in Fax Number

        objDoc.Fields(7).Result.Text = Nz(!strFaxCnty) & Nz(!strFaxCity)

& !strFax

        

        ' Fill in e-mail address

        objDoc.Fields(8).Result.Text = Nz(!strEMail)

        

        ' Fill in web site

        objDoc.Fields(9).Result.Text = Nz(HyperlinkPart(!hypWebSite,

acDisplayText))

        

        ' Fill in Services Provided

        objDoc.Fields(10).Result.Text = Nz(!memServices)

        

        'Fill in the dues

        objDoc.Fields(11).Result.Text = Format(curDues, "####")

        

        'Print the document

        objDoc.PrintOut

        

        'Close the document without saving changes

        objDoc.Close SaveChanges:=wdDoNotSaveChanges

        

        'Go the next record

        .MoveNext

        

        'Do it all over again

        Loop

        End With



Hope this helps

Richard Lobel







Message #3 by "Peter Kaufman" <kaufman@l...> on Sun, 29 Jul 2001 09:31:15 +0700
You already received a good reply, but if you want more examples of

automation, get this article from the MS Knowledge Base:



Q260410





Peter



-----Original Message-----

From: erezmor@n... [mailto:erezmor@n...]

Sent: July 28, 2001 3:00 PM

To: Access

Subject: [access] calling a Word doc from access





dear someone

how do i open a word document from access code?

and once opened, how do i activate a procedure (a macro) in that document?

i'm using word's mail-merge to produce a letter with data coming from the

selected record in access

but i want the document shown (to the user) to be the merged one with all

fields filled up, and

not the original source.

hope i made myself clear

thanx

erez.

---






  Return to Index