Better ways to create reports as Word Documents
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 -
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
Is there a more efficient and effective method to use for creating reports as Word documents than what I've done here?
Re: Better ways to create reports as Word Documents
I'm glad you liked my FAQ :thumb:
Yes, there are a couple of methods.
You could create a document template (dot) file and use it to create new documents from.
Also, using Bookmarks in the template is an easy way to have "fill-in" type of fields for use in a form letter type scenerio.
Not sure what type of report you are needing but these should give you some ideas.
Re: Better ways to create reports as Word Documents
Quote:
Originally Posted by RobDog888
I'm glad you liked my FAQ :thumb:
Yes, there are a couple of methods.
You could create a document template (dot) file and use it to create new documents from.
Also, using Bookmarks in the template is an easy way to have "fill-in" type of fields for use in a form letter type scenerio.
Not sure what type of report you are needing but these should give you some ideas.
RobDog, thanks for replying.
Two caveats -
- As I understand it, you need to save the template in the "User's Application Data\Microsoft Word\Template" directory. It also gives an error '5151' whenever I try to create a document from a template -
'
Code:
Opens a specific Word document
Private Function DoLoad_WordDoc(docname As String) As String
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Set wordApp = New Word.Application
Set wordDoc = wordApp.Documents.Add("C:\ApplicantReportTemplate.doc", , , False)
wordDoc.SaveAs FileName:="Document1.doc"
wordDoc.Activate
DoLoad_WordDoc = wordDoc.Content
wordDoc.Close SaveChanges:=True
Set wordDoc = Nothing
wordApp.Quit
Set wordApp = Nothing
End Function
- And bookmarks aren't visible in the word document, I'm not sure how you can subsititute this for a report variable either.
Re: Better ways to create reports as Word Documents
What is supposedly the output you wanted?
Re: Better ways to create reports as Word Documents
Quote:
Originally Posted by Erroneous
What is supposedly the output you wanted?
Essentially it's to produce a Word document containing applicant details (eg. Permit ID, Surname, etc...) from a database table. The layout is pretty simple:
I'm just copying an existing Word document which has this layout:
Permit ID: varPermitID
Surname: varSurname Middlename: varMiddlename Firstname: varFirst
Address: varAddress
I then use a Find and Replace (which you can find in VB Help Reference) and subsitute the variable names for the actual values.
Permit ID: 00000
Surname: Smith Middlename: Johnston Firstname: Cal
Address: 12 Sandy Bay Rd
The output is straightforward, it's the best method of putting the database field values into the Word Document that I'm interested in.
Re: Better ways to create reports as Word Documents
Creating a template document with the desired layout is best and create it to be a MailMerge document template.
Here is my FAQ on mailMerges.
http://vbforums.com/showthread.php?t=402097
Re: Better ways to create reports as Word Documents
Quote:
Originally Posted by RobDog888
RobDog, thanks for the link. Regardless, I think I might stick with my original idea. I'm finding Word templates very difficult (if not impossible) to implement in VB6.
It's also important to remember that the application will be installed on multiple computers. So the template directories and database locations will always be different. As far as I can see, the template methodology requires a fixed directory and database, which isn't the case here.
But thanks anyway. :)
Re: Better ways to create reports as Word Documents
:confused: But you are not required to locate the template in the templates directory. Just place it in your app dir if you want.
Re: Better ways to create reports as Word Documents
Quote:
Originally Posted by RobDog888
:confused: But you are not required to locate the template in the templates directory. Just place it in your app dir if you want.
Already tried that and got the error '5151' whenever I try to create a document from a template -
Code:
Opens a specific Word document
Private Function DoLoad_WordDoc(docname As String) As String
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Set wordApp = New Word.Application
Set wordDoc = wordApp.Documents.Add("C:\ApplicantReportTemplate.doc", , , False)
wordDoc.SaveAs FileName:="Document1.doc"
wordDoc.Activate
DoLoad_WordDoc = wordDoc.Content
wordDoc.Close SaveChanges:=True
Set wordDoc = Nothing
wordApp.Quit
Set wordApp = Nothing
End Function
I've gone with my original idea and gotten it working rather nicely. The solution isn't as elegant as using templates, but oh well.