Firstly, I've already read RobDog's guide on how to read/write Word documents. Great stuff.
I'm developing functionality for generating an Applicant Report (as a Word Document). ReportTemplate.doc is a Word Document with the variable names (eg. varSurname, varPermitID). The subroutines will create a new copy of ReportTemplate.doc (called ApplicantReport.doc) and subsitute the variable names for the actual report values.
In rough pseudocode it can be summarised as:
Copy ReportTemplate.doc to ApplicantReport.Doc
Find and Replace variableNames in ApplicantReport.doc with report values
Save the ApplicantReport.doc
And here's an example of some of the Word-related subroutines I've been using. I'll be using something similar in the actual program. Just create a button in a form, and use the Word Object Library as a reference -
Is there a more efficient and effective method to use for creating reports as Word documents than what I've done here?Code:' Copies a specific Word document Private Sub DoCopy_WordDoc(origdoc As String, copydoc As String) Dim wordApp As Word.Application Dim wordDoc As Word.Document Set wordApp = New Word.Application Set wordDoc = wordApp.Documents.Open(origdoc) wordDoc.Activate wordDoc.SaveAs copydoc wordDoc.Close SaveChanges:=False Set wordDoc = Nothing wordApp.Quit Set wordApp = Nothing End Sub ' Finds and replaces list of original strings with replacement ' strings within a Word Document object. Private Sub DoMultipleReplace_WordDoc(docname As String, _ origStrings() As String, _ replaceStrings() As String) Dim wordApp As Word.Application Dim wordDoc As Word.Document Dim i As Integer ' Loop Counter ' Open the Word document Set wordApp = New Word.Application Set wordDoc = wordApp.Documents.Open(docname) wordDoc.Activate For i = 0 To UBound(origStrings) - 1 With wordApp.Selection.Find .ClearFormatting .Text = origStrings(i) .Replacement.ClearFormatting .Replacement.Text = replaceStrings(i) .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue End With Next i ' Close the Word Document wordDoc.Close SaveChanges:=True Set wordDoc = Nothing wordApp.Quit Set wordApp = Nothing End Sub Private Sub cmd1() Dim origStrings(1) As String Dim replaceStrings(1) As String origStrings(0) = "Selection" replaceStrings(0) = "DavidLim" Call DoCopy_WordDoc("C:\Document1.doc", "C:\Document2.doc") Call DoMultipleReplace_WordDoc("C:\Document2.doc", origStrings, replaceStrings) End Sub




Reply With Quote