|
-
Mar 28th, 2007, 11:20 PM
#1
Thread Starter
Lively Member
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?
"Intelligent people may or may not appear Smart. Smart people may be Intelligent, but don’t bet on it. Intelligent and Smart people do Stupid things on occasion. Stupid people are not Intelligent. Everyone is ignorant. Who needs a drink?"
- Brian Hendrix
-
Mar 29th, 2007, 12:14 AM
#2
Re: Better ways to create reports as Word Documents
I'm glad you liked my FAQ 
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 29th, 2007, 12:42 AM
#3
Thread Starter
Lively Member
Re: Better ways to create reports as Word Documents
 Originally Posted by RobDog888
I'm glad you liked my FAQ 
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.
Last edited by Spacehamster; Mar 29th, 2007 at 12:46 AM.
"Intelligent people may or may not appear Smart. Smart people may be Intelligent, but don’t bet on it. Intelligent and Smart people do Stupid things on occasion. Stupid people are not Intelligent. Everyone is ignorant. Who needs a drink?"
- Brian Hendrix
-
Mar 29th, 2007, 01:38 AM
#4
Lively Member
Re: Better ways to create reports as Word Documents
What is supposedly the output you wanted?
-
Mar 29th, 2007, 05:24 PM
#5
Thread Starter
Lively Member
Re: Better ways to create reports as Word Documents
 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.
"Intelligent people may or may not appear Smart. Smart people may be Intelligent, but don’t bet on it. Intelligent and Smart people do Stupid things on occasion. Stupid people are not Intelligent. Everyone is ignorant. Who needs a drink?"
- Brian Hendrix
-
Mar 29th, 2007, 05:32 PM
#6
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 29th, 2007, 05:41 PM
#7
Thread Starter
Lively Member
Re: Better ways to create reports as Word Documents
 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.
"Intelligent people may or may not appear Smart. Smart people may be Intelligent, but don’t bet on it. Intelligent and Smart people do Stupid things on occasion. Stupid people are not Intelligent. Everyone is ignorant. Who needs a drink?"
- Brian Hendrix
-
Mar 29th, 2007, 08:43 PM
#8
Re: Better ways to create reports as Word Documents
But you are not required to locate the template in the templates directory. Just place it in your app dir if you want.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 29th, 2007, 09:55 PM
#9
Thread Starter
Lively Member
Re: Better ways to create reports as Word Documents
 Originally Posted by RobDog888
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.
"Intelligent people may or may not appear Smart. Smart people may be Intelligent, but don’t bet on it. Intelligent and Smart people do Stupid things on occasion. Stupid people are not Intelligent. Everyone is ignorant. Who needs a drink?"
- Brian Hendrix
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|