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