Click here to Skip to main content
14,930,087 members
Articles / Programming Languages / Visual Basic
Posted 14 May 2008


31 bookmarked

universal printing without office

Rate me:
Please Sign up or sign in to vote.
4.43/5 (4 votes)
14 May 2008CPOL1 min read
With the free wordviewer 2003/2007 and some xml docs you can print everthing any time


Printing solution that is reliable, easy to mantain, no extra libary's needed. Just download the free microsoft word viewer. Make some XML templates and let the code do the job for you.


I search a long time to get a good printing solution that can fill templates an has no need for drivers or references.

First of all we need a word 2003 (or 2007) xml file that whe use as template source. Because this file is not zipped like docx or open source alternatif odt, whe can read it as a text file and replace without any problem. In the XML template we put the 'TEXTFIELDS' for replacement. In this example i use the markers #...# as replacement field.

First whe read the source, do the replacements and the write the temp printfile. So we always keep the original template.

Dim BRONDOCUMENT As String = "test.xml"  ' Path to then source file
TEMPDOCUMENT = "TEMP.xml" 'this will be the output file with the changes
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(BRONDOCUMENT)
' WORD xml is not zipped so whe can copy an replace everthing without any problem
' I used a external textfile with the data to change. In this example whe do it inhere.
fileContents = fileContents.Replace("#NAAM#", "DE CAT BEN")
fileContents = fileContents.Replace("#ADRESSE#", "A STREET IN BELGIUM")
fileContents = fileContents.Replace("#URL#", "<a href="%22">WWW.HETNETOP.BE</a>")
fileContents = fileContents.Replace("#CITY#", "KORTRIJK")
My.Computer.FileSystem.WriteAllText(TEMPDOCUMENT, fileContents, False)

Then all whe have to do is to make some buttons, PRINT, VIEW/EDIT and CANCEL.

'be sure that xml is assosiated with MS word or wordview
Process.Start(TEMPDOCUMENT) 'you can at some minimized window things here
My.Computer.Keyboard.SendKeys("^p", True)
My.Computer.Keyboard.SendKeys("~", True)
For Each proc As Process In Process.GetProcesses()
    If InStr(proc.MainWindowTitle, "TEMP") > 0 Then proc.CloseMainWindow()

The VIEW/EDIT button is just the cal 'PROCESS.START(TEMPDOCUMENT)' so whe get the wordviewer in all his glory.

And guess, CANCEL just ends the program... Really simple ! but very fast and reliable in practice !

With this concept you can make a universal windows printer engine, with input the source template XML file and one with fill DATA, so you can call this from other programs, without all the problems that printing can give you...

PS: the test file TEST.XML is in the bind\debug dir.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer delcatto
Belgium Belgium
Ben DE CAT, works in Belgium or online as freelance. Native language is Dutch so the english is a bit wacky ?!
Makes software for the Belgium gouverment since 1998. Started with Cobol, delphi an visual basic 5.00 and now uses VB2008.

Comments and Discussions

Generalshellexecute Pin
Member 63246915-May-08 5:12
MemberMember 63246915-May-08 5:12 
GeneralRe: shellexecute Pin
delcatto15-May-08 6:33
Memberdelcatto15-May-08 6:33 
GeneralTemplate as Embedded Resource [modified] Pin
Ilíon14-May-08 17:34
MemberIlíon14-May-08 17:34 
AnswerRe: Template as Embedded Resource Pin
delcatto15-May-08 2:10
Memberdelcatto15-May-08 2:10 
JokeThis is really KISS Pin
delcatto14-May-08 12:47
Memberdelcatto14-May-08 12:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.