• Creating Word Document via Automation (Word 97 / VB6)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Creating Word Document via Automation (Word 97 / VB6)

    Author
    Topic
    #376274

    I’m creating an application that has to output data into a Word document. I’m quite new to Word code & don’t really know the object model yet, so any help on this is much appreciated.

    The document will be a ‘Systems Procedure’, so the document has to be in a concise format. I’ve got the following so far, this code is located in a class module named ‘QAProcedure’.

    Public Sub CreateProcedureDocument()

    Dim wrdApplication As Word.Application
    Dim docProcedure As Word.Document
    Dim docParagraph As Word.Paragraph
    Dim objHistory As QAProceduresExtension.IssueHistory
    Dim objProcItem As QAProceduresExtension.QAProcedureItem
    Dim I As Integer

    ‘// Set start value
    I = 2

    ‘// Create word application
    Set wrdApplication = New Word.Application

    ‘// Open the procedure template
    Set docProcedure = wrdApplication.Documents.Open(WORD_TEMPLATE)

    ‘//*************************************************************************************8
    ‘// Start formatting the document

    ‘// Procedure title
    docProcedure.Tables(1).Cell(1, 2).Range = Me.Title

    ‘// Raised by information
    docProcedure.Tables(2).Cell(1, 1).Range = “Raised By: ” & Me.CreatedBy
    docProcedure.Tables(2).Cell(1, 2).Range = “Date: ” & Date

    docProcedure.Tables(2).Cell(2, 1).Range = “Approved By: ” & Me.AuthorisedBy
    docProcedure.Tables(2).Cell(2, 2).Range = “Date: ” & Date

    ‘// Revision history information
    For Each objHistory In HistoryItems
    docProcedure.Tables(3).Cell(I, 1).Range = objHistory.Issue
    docProcedure.Tables(3).Cell(I, 2).Range = objHistory.RevisionDate
    docProcedure.Tables(3).Cell(I, 3).Range = objHistory.AuthorisedBy
    docProcedure.Tables(3).Cell(I, 4).Range = objHistory.RevisionDescription
    I = (I + 1)
    Next

    ‘// Main section, this is the procedure heading and generic information

    ‘// PURPOSE
    Set docParagraph = docProcedure.Paragraphs.Add

    With docParagraph
    .Range = Me.Purpose
    End With

    ‘// Display the document
    wrdApplication.Visible = True

    End Sub

    I’m OK up to the section marked ‘// PURPOSE where I attempt to add a paragraph. I basically want to add a number of paragraphs to the document, 1 for ‘Purpose’, 1 For ‘Scope’, 1 for ‘Assocated Information’. The above code adds the text, but any further attempts to use the Set docParagraph = docProcedure.Paragraphs.Add syntax just picks up the last paragraph created…?

    Is this the correct way to add new text to the document? Or should I only be using the ‘Paragraph’ object to format existing text?

    Thanks

    Viewing 2 reply threads
    Author
    Replies
    • #615631

      I’ve only done that once, just to see if I could, so I’m not sure if that is the best way. I remember being frustrated just like you, but looking at my code, I finally put this line at the beginning

          Set paraEnd = docProcedure.Paragraphs.Add

      then whenever I wanted to add a paragraph, I used the code:

          Set docParagraph = docProcedure.Paragraphs.Add(paraEnd.Range)
    • #615647

      I Haven’t analyzed your code, but you could try cheating:

      Set docParagraph = docProcedure.Paragraphs.Add
      ‘Do whatever
      Set docParagraph = nothing
      Set docParagraph = docProcedure.Paragraphs.Add

      Might be a better way, but I haven’t eaten yet today. My growling stomach is hampering my thought processes.

    • #615792

      Couple of suggestions:

      • If you don’t really need an object reference to the new paragraph, use a general range identifier such as docProcedure.Content and the .InsertAfter method, terminating each insertion with & vbCrLf.
      • Also, use the Documents.Add method to create the new document, or you will end up editing your original template.[/list]Hope this helps.
    Viewing 2 reply threads
    Reply To: Creating Word Document via Automation (Word 97 / VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: