VB projects - Using Word template
Description: Writing the program that open selected text file, apply Word template to it and print out the resulting document
Minimum requirements: vb5, MS Word 7.0
Download: source code
Screenshot:

Project: Standard EXE
Controls: txtFile (textbox), txtTemplate (textbox), cmdFile (button), cmdTemplate (button), cmdRun (button), dlg (common dialog control)
Additional project references: Microsoft Word Object Library
Code:
Private Sub cmdFile_Click()
dlg.FileName = "*.txt"
dlg.ShowOpen
txtFile = dlg.FileName
End Sub
Private Sub cmdTemplate_Click()
dlg.FileName = "*.dot"
dlg.ShowOpen
txtTemplate = dlg.FileName
End Sub
Private Sub cmdRun_Click()
Dim wa As Word.Application
Dim wt As Word.template
Dim strTemp As String
Dim strOutput As String
Set wa = New Word.Application
wa.AddIns.Add txtTemplate, True
Set wt = wa.Templates(txtTemplate)
wa.Documents.Add wt.FullName, newtemplate:=False
Open txtFile For Input As 1
While Not EOF(1)
Line Input #1, strTemp
strOutput = strOutput & strTemp &
vbCrLf
Wend
Close #1
Clipboard.Clear
Clipboard.SetText (strOutput)
wa.Selection.WholeStory
wa.Selection.Paste
wa.ActiveDocument.PrintOut
While wa.BackgroundPrintingStatus > 0
DoEvents
Wend
Set wt = Nothing
wa.Quit (wdDoNotSaveChanges)
Set wa = Nothing
End Sub
|