Wrox Programmer Forums
|
BOOK: Access 2003 VBA Programmer's Reference
This is the forum to discuss the Wrox book Access 2003 VBA Programmer's Reference by Patricia Cardoza, Teresa Hennig, Graham Seach, Armen Stein; ISBN: 9780764559037
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Access 2003 VBA Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 25th, 2006, 10:59 AM
Registered User
 
Join Date: Jul 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using Word in Access

Below is code I wrote to create a word document based on a template and bookmarks. I need to change the document to be able to include more than one record per letter I can't seem to figure it out.

Dim Host, he As Object
    Dim rstMailPrgrssvLtr As New ADODB.Recordset
    Dim rstMailPrgrssvLtr2 As New ADODB.Recordset
    Dim appWord As New Word.Application
    Dim truncate As String
    Dim Amount As String
    Dim acct As String
    Dim passacct As String
    Dim addrss As String
    Dim merch As String
    Dim count As String




    idletime = 600

        'Defines the MBNA Window as the Host.
    Set he = CreateObject("HostExplorer")
    Set Host = he.CurrentHost


    'Open a recordset based on the qryEmail query.

    rstMailPrgrssvLtr.Open "qryMailPrgrssv3", _
        CurrentProject.Connection, adOpenKeyset, adLockOptimistic
    rstMailPrgrssvLtr2.Open "qryMailPrgrssv2", _
        CurrentProject.Connection, adOpenKeyset, adLockOptimistic

        'If no records exist, display a message and then exit.
        If rstMailPrgrssvLtr.RecordCount = 0 Then
            MsgBox "There are no Progressive Letters to Mail Today.", vbOKOnly, "Progressive Letter"
            Exit Function
        End If
        count = rstMailPrgrssvLtr.RecordCount
        truncate = Left$(rstMailPrgrssvLtr2!AccountNumber, 10)
        Amount = FormatCurrency(rstMailPrgrssvLtr!Amount)
        'Open a document based on the Email template. Turn off spell check.
        'move to the To line bookmark, and then display Word.
         MsgBox ("There are " & count & " Letters being created"), vbOKOnly, "Printing Ltrs"

        MsgBox "Make sure you are logged in to IS and on X100 Screen", vbOKOnly, "Documentation"

    Do Until rstMailPrgrssvLtr.EOF
        merch = rstMailPrgrssvLtr!Merchant
        acct = rstMailPrgrssvLtr2!AccountNumber
        truncate = Left$(rstMailPrgrssvLtr2!AccountNumber, 10)
        Amount = FormatCurrency(rstMailPrgrssvLtr!Amount)
        If rstMailPrgrssvLtr.Fields!FrenchDoc = True Then
            With appWord 'french document code
                .Documents.Add "G:\Risk Control\Fraud\Fraud Back End\Fraud Back End\Project\Affidavit\PrgrssvTemplates\Mailprogrsv LtrTemplateFREN060106.dot"
                .Selection.GoTo wdGoToBookmark, Name:="Trunkate"
                appWord.Selection.TypeText truncate & " "
                .Selection.GoTo wdGoToBookmark, Name:="Name1"
                appWord.Selection.TypeText rstMailPrgrssvLtr!Name & " "
                .Selection.GoTo wdGoToBookmark, Name:="Address"
                appWord.Selection.TypeText rstMailPrgrssvLtr!Address & " "
                .Selection.GoTo wdGoToBookmark, Name:="City"
                appWord.Selection.TypeText rstMailPrgrssvLtr!City & " "
                .Selection.GoTo wdGoToBookmark, Name:="PostalCode"
                appWord.Selection.TypeText rstMailPrgrssvLtr!PostalCode & " "
                .Selection.GoTo wdGoToBookmark, Name:="Name2"
                appWord.Selection.TypeText rstMailPrgrssvLtr!Name & " "
            Do Until rstMailPrgrssvLtr.Fields!AccntId <> rstMailPrgrssvLtr2.Fields!AccountNumber
                .Selection.GoTo wdGoToBookmark, Name:="Amount"
                appWord.Selection.TypeText Amount & " "
                .Selection.GoTo wdGoToBookmark, Name:="Merchant"
                appWord.Selection.TypeText rstMailPrgrssvLtr!Merchant & " "
                .Selection.GoTo wdGoToBookmark, Name:="TransDate"
                appWord.Selection.TypeText rstMailPrgrssvLtr!TransDate & " "
                .Selection.GoTo wdGoToBookmark, Name:="DueDate"
                appWord.Selection.TypeText rstMailPrgrssvLtr!DueDate & " "
                Loop
                .Selection.GoTo wdGoToBookmark, Name:="Name3"
                appWord.Selection.TypeText rstMailPrgrssvLtr!Name & " "
                .Selection.GoTo wdGoToBookmark, Name:="Trunkate2"
                appWord.Selection.TypeText truncate & " "
                .Selection.GoTo wdGoToBookmark, Name:="Merchant2"
                appWord.Selection.TypeText rstMailPrgrssvLtr!Merchant & " "
                .Selection.GoTo wdGoToBookmark, Name:="Amount2"
                appWord.Selection.TypeText Amount & " "
                .Selection.GoTo wdGoToBookmark, Name:="TransDate2"
                appWord.Selection.TypeText rstMailPrgrssvLtr!TransDate & " "
                .Selection.GoTo wdGoToBookmark, Name:="Reference"
                appWord.Selection.TypeText rstMailPrgrssvLtr!ReferenceNumber & " "
                .Visible = False
                appWord.ActiveDocument.SaveAs ("G:\Risk Control\Fraud\Fraud Back End\Fraud Back End\Project\Affidavit\PrgrssvTemplates\" & rstMailPrgrssvLtr!AccountNumber), wdFormatDocument
                appWord.ActiveDocument.PrintOut



                If Len(appWord.ActiveDocument.Path) = 0 Then
                MsgBox "Document needs to be saved first"

                End If









                appWord.ActiveDocument.Close


            End With





        Else 'English document code
            With appWord

                .Documents.Add "S:\Canada\Risk Control\Fraud\Fraud Back End\Fraud Back End\Project\Affidavit\PrgrssvTemplates\Mailprogrsv LtrTemplateEng060106.dot"
                .ActiveDocument.ShowSpellingErrors = False
                .Selection.GoTo wdGoToBookmark, Name:="Truncate"
                appWord.Selection.TypeText truncate & " "
                .Selection.GoTo wdGoToBookmark, Name:="Name"
                appWord.Selection.TypeText rstMailPrgrssvLtr2!Name & " "
                .Selection.GoTo wdGoToBookmark, Name:="Address"
                appWord.Selection.TypeText rstMailPrgrssvLtr2!Address & " "
                .Selection.GoTo wdGoToBookmark, Name:="City"
                appWord.Selection.TypeText rstMailPrgrssvLtr2!City & " "
                .Selection.GoTo wdGoToBookmark, Name:="PostalCode"
                appWord.Selection.TypeText rstMailPrgrssvLtr2!PostalCode & " "
                .Selection.GoTo wdGoToBookmark, Name:="Name2"
                appWord.Selection.TypeText rstMailPrgrssvLtr2!Name & " "
            Do Until rstMailPrgrssvLtr.Fields!AccntId <> rstMailPrgrssvLtr2.Fields!AccountNumber
                .Selection.GoTo wdGoToBookmark, Name:="Amount"
                appWord.Selection.TypeText Amount & " "
                .Selection.GoTo wdGoToBookmark, Name:="Merchant"
                appWord.Selection.TypeText rstMailPrgrssvLtr!Merchant & " "
                .Selection.GoTo wdGoToBookmark, Name:="TransDate"
                appWord.Selection.TypeText rstMailPrgrssvLtr!TransDate & " "
                .Selection.GoTo wdGoToBookmark, Name:="DueDate"
                appWord.Selection.TypeText rstMailPrgrssvLtr!DueDate & " "
                Loop
                .Selection.GoTo wdGoToBookmark, Name:="Amount"
                appWord.Selection.TypeText Amount & " "
                .Selection.GoTo wdGoToBookmark, Name:="Merchant"
                appWord.Selection.TypeText rstMailPrgrssvLtr!Merchant & " "
                .Selection.GoTo wdGoToBookmark, Name:="TransDate"
                appWord.Selection.TypeText rstMailPrgrssvLtr!TransDate & " "
                .Selection.GoTo wdGoToBookmark, Name:="DueDate"
                appWord.Selection.TypeText rstMailPrgrssvLtr!DueDate & " "
                .Selection.GoTo wdGoToBookmark, Name:="Name3"
                appWord.Selection.TypeText rstMailPrgrssvLtr2!Name & " "
                .Selection.GoTo wdGoToBookmark, Name:="Trunkate2"
                appWord.Selection.TypeText truncate & " "
                .Selection.GoTo wdGoToBookmark, Name:="Merchant2"
                appWord.Selection.TypeText rstMailPrgrssvLtr!Merchant & " "
                .Selection.GoTo wdGoToBookmark, Name:="Amount2"
                appWord.Selection.TypeText Amount & " "
                .Selection.GoTo wdGoToBookmark, Name:="TransDate2"
                appWord.Selection.TypeText rstMailPrgrssvLtr!TransDate & " "
                .Selection.GoTo wdGoToBookmark, Name:="Reference"
                appWord.Selection.TypeText rstMailPrgrssvLtr!ReferenceNumber & " "
                .Visible = False
                appWord.ActiveDocument.SaveAs ("Location"End\Project\Affidavit\PrgrssvTemplates\ " & rstMailPrgrssvLtr2!AccountNumber), wdFormatDocument
                appWord.ActiveDocument.PrintOut


                If Len(appWord.ActiveDocument.Path) = 0 Then
                    MsgBox "Document needs to be saved first"
                End If




                appWord.ActiveDocument.Close

            End With

        End If

        rstMailPrgrssvLtr.Fields!MailLtr = True
        rstMailPrgrssvLtr2.Fields!LastModifiedDate = Date
        rstMailPrgrssvLtr.Fields!MailDate = Date
        rstMailPrgrssvLtr2.Fields!Notes = Date & " Letter Mailed"
        rstMailPrgrssvLtr.MoveNext




        rstMailPrgrssvLtr2.MoveNext


Loop

    MsgBox "All documents have been printed", vbOKOnly, "Mail Progressive Letter"
End Function







Similar Threads
Thread Thread Starter Forum Replies Last Post
Access to Word not saving BeckyMack Access 0 April 9th, 2008 07:16 AM
Word tables to access hayk_yer Access VBA 1 June 26th, 2007 10:49 AM
Using an Access Database in Word Toran Access 2 October 19th, 2006 01:25 PM
word data to access treasacrowe Access 1 October 22nd, 2004 03:55 PM
Access OLE to Word GregoryHu Access VBA 0 March 1st, 2004 08:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.