Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Nov 2001
    Location
    London, UK, England
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating Word Document via Automation (Word 97 / VB6)

    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

  2. #2
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Creating Word Document via Automation (Word 97 / VB6)

    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
    <pre> Set paraEnd = docProcedure.Paragraphs.Add</pre>

    then whenever I wanted to add a paragraph, I used the code:
    <pre> Set docParagraph = docProcedure.Paragraphs.Add(paraEnd.Range)</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating Word Document via Automation (Word 97 / VB6)

    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.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Creating Word Document via Automation (Word 97 / VB6)

    Couple of suggestions:

    <UL><LI>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.

    <LI>Also, use the Documents.Add method to create the new document, or you will end up editing your original template.[/list]Hope this helps.

  5. #5
    2 Star Lounger
    Join Date
    Nov 2001
    Location
    London, UK, England
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating Word Document via Automation (Word 97 / VB6)

    Thanks for all your suggestions, I'll be giving each one a go...

Posting Permissions

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