Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2003
    Location
    New Zealand
    Posts
    47

    Unanswered: open word from access

    I need to open a word document using a template from access.

    I can open any word document. And i can open a template........but i can't open/create a new word document using a template! Does anyone know how to do this.

    I am writing data from my database to a word document and need to use a template for the layout of the data.

    Thanks in advance.
    Nerdy Girl

  2. #2
    Join Date
    Oct 2002
    Location
    Amsterdam
    Posts
    8
    You will need a .doc which has all the data and a .dot which has the merging fields.

    .doc
    Create a table with field headers (1) and data fields (2)

    Example:
    (1)CompanyName (1)ContactName
    (2)De Smurfen (2)Grote Smurf
    (2)ECEM (2)Maarten Roos

    Or use the following line to create a RTF document from a query:
    PHP Code:
    DoCmd.OutputTo acOutputQuerystrQueryacFormatRTFstrDataSourceFalse 
    .dot
    (use the toolbar "mail merge")

    • Open a data source to the .doc.
    • Use the "Insert mail merge fields" button to add "companyname" and "contactname"
    • Save as template and close.


    Use the following code to mail merge .dot with .doc from a MS Access application. This routine also creates the .doc file if you include a valid query.

    PHP Code:
    Public Sub MailMerge(strTemplate As StringOptional ByVal strQuery As String)
        
    Dim strPath As String
        Dim strDataSource 
    As String
        
        Dim doc 
    As Word.Document
        Dim wrdApp 
    As Word.Application
            
        On Error 
    GoTo HandleErrors
        
        glrStatus 
    "Busy with Mail Merge to Word." vbCrLf vbCrLf "One moment please..."
        ' Delete the rtf file, if it already exists.
        strPath = FixPath(CurrentProject.Path)
        If strQuery <> "" Then
            strDataSource = strPath & strQuery & ".doc"
        End If
        If strQuery <> "" Then
            Kill strDataSource
        
            ' 
    Export the data to rtf format
            DoCmd
    .OutputTo acOutputQuerystrQueryacFormatRTFstrDataSourceFalse
        End 
    If

        
    ' Start Word using mailmerge template
        Set wrdApp = New Word.Application
        Set doc = wrdApp.Documents.Add(strPath & strTemplate)
            
        ' 
    Do the mail merge to a new document.
        
    With doc.MailMerge
            
    If strQuery <> "" Then
                
    .OpenDataSource Name:=strDataSource
            End 
    If
            .
    Destination wdSendToNewDocument
            
    .SuppressBlankLines True
            With 
    .DataSource
                
    .FirstRecord wdDefaultFirstRecord
                
    .LastRecord wdDefaultLastRecord
            End With
            
    If .State wdMainAndDataSource Then .Execute
        End With
        doc
    .Close (False)
        
    ' Display the mail merge document
        wrdApp.Visible = True
        
    ExitHere:
        Set doc = Nothing
        Set wrdApp = Nothing
        glrStatus
        Exit Sub
        
    HandleErrors:
        Select Case Err.Number
            Case 53         ' 
    File not found
                Resume Next
            
    Case Else
                
    MsgBox Err.Number ": " Err.Description
                Resume ExitHere
        End Select
        Resume
    End Sub 
    Succes.

    Maarten Roos
    ECEM European Chemical Marketing bv
    E m.roos@ecem.com
    W www.ecem.com

Posting Permissions

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