# Using win32ole to print .txt, .pdf, .xps files

**URL:** <https://www.ruby-forum.com/t/using-win32ole-to-print-txt-pdf-xps-files/87972>\
**Category:** Ruby\
**Created:** [March 7, 2007, 9:00am UTC](https://www.ruby-forum.com/t/using-win32ole-to-print-txt-pdf-xps-files/87972 "2007-03-07T09:00:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 7, 2007, 9:00am UTC](https://www.ruby-forum.com/t/using-win32ole-to-print-txt-pdf-xps-files/87972/1 "2007-03-07T09:00:28Z")

</div>

good afternoon to all.

i am creating a simple script to automate the printing of pdf, txt,  
and xps files. i’m using this exercise as my first step in learning  
ruby.

i am able to print doc, xls, and ppt files using code that i put  
together from snippets i got off the net:

require ‘win32ole’  
…  
app = WIN32OLE.new(“word.application”)  
app.Documents.Open("/test.doc")  
testfile = app.ActiveDocument  
testfile.PrintOut()  
…  
testfile.Close()  
app.Quit()  
…

i haven’t been able to find code snippets (or adequate win32ole  
documentation) for printing .txt files (through notepad), or pdf or  
xps.  
is this even possible? through win32ole or otherwise?  
if it is, can anyone provide me with the necessary details as to how?  
if it is not possible, any suggestions for an alternative method?

thank you very much in advance.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 7, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/using-win32ole-to-print-txt-pdf-xps-files/87972/2 "2007-03-07T16:46:56Z")

</div>

removed\_email\_address@domain.invalid wrote:

> …  
> app = WIN32OLE.new(“word.application”)  
> app.Documents.Open(“/test.doc”)  
> testfile = app.ActiveDocument  
> testfile.PrintOut()  
> …  
> testfile.Close()  
> app.Quit()  
> …

If you’ve written VBA code for MS Office programs, you may notice the  
similarity. You’re using COM, an simple programming API that Office and  
some other programs expose. That is, all these methods (Documents, Open,  
ActiveDocument, PrintOut, Close, Quit) are being sent to Word to  
process. If you want to do more advanced stuff, the place you would look  
for reference is in the documentation for VBA in Word.

> i haven’t been able to find code snippets (or adequate win32ole  
> documentation) for printing .txt files (through notepad), or pdf or  
> xps.  
> is this even possible? through win32ole or otherwise?

You can open and print a text file in Word in the same way you deal with  
a Word document.

As far as I know, Adobe Reader doesn’t have a COM interface. You could  
use a macro-type solution such as AutoIt3 (which provides a COM  
interface which can be used to interact with many Windows applications).

I’m not sure about XPS. Try Word’s help to see if you can deal with it  
like a Word document.

Otherwise, if you’re brave, there’s a paper here describing the Vista  
.NET API, which you may be able to use via Ruby’s DL library, or just a  
.NET program rather than Ruby:  
[http://www.microsoft.com/whdc/xps/xps-read.mspx](http://www.microsoft.com/whdc/xps/xps-read.mspx)

Cheers,  
Dave
