[AccessD] Inserting text in a word document

Jim Dettman jimdettman at earthlink.net
Fri Feb 10 10:54:32 CST 2006


John

  The code snippets below insert data from a record set.

Jim.

    Const pelFldWWBookmark = 0
    Const pelFldAccessField = 1
    Const pelFldWWFont = 2
    Const pelFldWWPoints = 3
    Const pelFldWWBold = 4
    Const pelFldWWItalics = 5
    Const pelFldWWUnderline = 6
    Const pelWordMacroRun = 1
    Const pelWordFileCloseNoSave = 2

  ' Begin Automation conversation with Word
    ' Don't need to intialize mobjWord if it already points to Word
    If mobjWord Is Nothing Then
        On Error Resume Next
        Set mobjWord = GetObject(, "Word.Application")
        If Err <> 0 Then
            Err = 0
            Set mobjWord = CreateObject("Word.Application")
        End If
        If Err <> 0 Then
            MsgBox "Error: Word Automation object could not be created.",
vbCritical + vbOKOnly, strProc
            GoTo AutomateWordDone
        End If
        On Error GoTo AutomateWordErr
    End If



    ' Create a new document for each data record.
    Do While Not rstData.EOF
        mobjWord.WordBasic.FileOpen varDocAndPath

        ' Send data, if any, over to document and format it
        If varSendFields Then
            ' Move through tblAutoFields row by row
            ' and send over data to document
            For intJ = 0 To intFieldsRecLim - 1
                ' Jump to bookmark
                mobjWord.Selection.GoTo What:=wdGoToBookmark,
Name:=avarFields(pelFldWWBookmark, intJ)
                ' Format the font per field specification
                mobjWord.WordBasic.FormatFont avarFields(pelFldWWPoints,
intJ), IIf(avarFields(pelFldWWUnderline, intJ), 1, 0), , , , , , , , , , , ,
, , avarFields(pelFldWWFont, intJ), IIf(avarFields(pelFldWWBold, intJ), 1,
0), IIf(avarFields(pelFldWWItalics, intJ), 1, 0)
                ' Insert the data at the bookmark
                mobjWord.WordBasic.INSERT
pelNullToZLS(rstData(avarFields(pelFldAccessField, intJ)))
            Next intJ
        End If ' varSendFields

        Debug.Print "Loaded and formatted data for " & rstData.[Name]

        ' Print the document, if required
        ' Otherwise shift focus to document for user edit
        If varDocPrint Then
            ' Print the document if required
            ' Use a Print Macro if one has been specified
            DoEvents
            If IsNull(varDocMacroPrint) Then
                mobjWord.WordBasic.FilePrint , , , , , , ,
pelNullToZLS(varCopies)
            Else
                mobjWord.WordBasic.ToolsMacro varDocMacroPrint,
pelWordMacroRun
            End If
            Debug.Print "Printed document"
        End If

        DoEvents
        DoEvents
        If varDocPrint Then
            mobjWord.WordBasic.FileClose pelWordFileCloseNoSave
            Debug.Print "Executed File Close"
            DoEvents
            DoEvents
        End If

        ' If requested as one record only,
        ' it's time to bail out!
        If fOneRec Then
            Exit Do
        Else
            rstData.MoveNext
        End If

    Loop
    rstData.Close
    Set rstData = Nothing

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com]On Behalf Of John Colby
Sent: Friday, February 10, 2006 11:47 AM
To: 'Access Developers discussion and problem solving'
Subject: [AccessD] Inserting text in a word document


Does anyone have code to insert lines of text in a word document at a
bookmark?

John W. Colby
www.ColbyConsulting.com


--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com





More information about the AccessD mailing list