13 August 2010

Printing Word Documents To XPS With Default Filenames

I was fairly disappointed when I found that when printing to an XPS file from Word you have to manually specify the filename every time. This is fine if you have one file you want to convert to an XPS file or even a few, but if you have a number it becomes quite a tedious process. I'm now using XPS documents as part of further work flow and I found it frustrating to have to enter the filename every time.



What I actually wanted was an easy method to be able to convert Word documents to image files. A few months ago I wanted to be able to easily convert any document to an image file. There were a range of products out there, but none of them did what I wanted so I rolled my own that was able to convert XPS documents to image files:


If you’re interested in this let me know and I'll upload it so everyone can use it.
Update: I have made this available, see my free software page.

With Word documents you already have the option to right click and print, but this only works in this case if your default printer is set to the XPS writer and you'll still have to specify the file name every time, all in all if you have enough documents this eats up a lot of time.

So what I really wanted to be able to do was not even have to open the document, but instead right click on it and simply select Print To XPS.


I couldn't find the information on how to do this, so I worked out how to do it and thought I would pass on here.

The solution I came up with can be used in two ways:
• If you just want to be able to quickly print the document you are working on to an XPS file with the same name from inside word.
• If you want to be able to right click in explorer and print a word document straight to an XPS file.

There are two parts to installing this:
• Installing the macro into word.
• Creating the right click context menu in explorer using the registry editor.


Installing The Macro Into Word

These instructions are for Microsoft Word 2003, but I'm sure that you can do the same things for other versions of Word, you will just have to be a little clever if things aren’t in the same place.

Update 01/09/2010: I have tested this with Word 2007 under Windows 7 and found that it still works, there is just a little variance in where things are located.

Open up a new word document and go to:
Tools->Macro->VisualBasicEditor

You will see the project explorer, it will contain two documents Normal and something like "Project(Document1)", we are interested in "Normal" because we want to apply the macros to the Normal template so that we can run it from any document, if you select the other option you will apply it to the currently opened document, which means the macro will only work with that document.

Download this macro file I have created, which contains all the macros we need to print to XPS files using the documents name as the name for the outputted XPS file.

Right click on "Normal" and select "Import File...".


Select my macro file from the import file dialog.

The file will now appear under the modules in the normal document and the code should show in the editor.


Close the VisualBasic Editor.

To test, Tools->Macro->Macros, two new macros should appear"
"PrintToXpsWithFilename"
"PrintToXpsWithFilenameAndClose"

You should be able to directly run these two macros:
"PrintToXpsWithFilename" will print to an .xps file with the same name and in the same directory as the current document and leave the document open.
"PrintToXpsWithFilenameAndClose" will do the same, but will close the document (and word) once it has been printed.


If you want you can now assign keyboard shortcuts or menu buttons, but these are fairly trivial so I will leave you to do that yourself.

Those of you who are just interested in the macros and are not interested in the right click functionality can leave us now. For the rest we will progress onto the registry.

Creating The Right Click Context Menu In Explorer Using Registry Editor

If you aren’t familiar editing the registry you might want to do a bit of background reading first.
Obviously backup your registry data first.

Fire up the registry editor.

First we will do it for old .doc documents, but see further down if you want to do it for .docx’s.

Open the following key:
HKEY_CLASSES_ROOT\Word.Document.8\shell\

Create a key called "PrintToXPS", set its (default) string value so that it is "Print To XPS" this will be the name that shows up on the files right click explorer menu.


Now create a sub key in the “PrintToXPS” key called "command", this key will hold the action that we want to perform. So now you should have:
HKEY_CLASSES_ROOT\Word.Document.8\shell\PrintToXPS\command

Set “command”s (default) string value to everything between the brackets but not including them ["C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE" /mPrintToXpsWithFilenameAndClose /q /n "%1 "]

Update 01/09/2010: For other versions of word you need to adjust the path you use. For Word 2007 use:
C:\Program Files\Microsoft Office\OFFICE12\WINWORD.EXE 


If you want to do it for other types of documents we need to find out where the shell command for that document type is located. I imagine it will be the same on your system so you probably won't need to look this up, but if you have trouble see what values have on your system.
To do this expand HKEY_CLASSES_ROOT\, you will see a huge amount of sub keys, you want to scroll down until you find the extension of the word document you want to be able to print from. On my system I can see the following two keys with the default value listed afterward:
HKEY_CLASSES_ROOT\.doc - Word.Document.8
HKEY_CLASSES_ROOT\.docx - Word.Document.12

Just replace this in the key paths you opened above and follow the instructions. So if I wanted to make it work for .docx I would open the following key and then follow the instructions above:
HKEY_CLASSES_ROOT\Word.Document.12\shell\

That should be all, you should now be able to right click on the Word document types you have set up and click “Print To XPS”.




Some further info for those who just need to know:

I think that the above is just a symbolic link to this key:
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.Document.8\shell\

So I have a feeling (although I haven't tested this) that if you want to apply it just to the current user you could put it in:
HKEY_CURRENT_USER\SOFTWARE\Classes\Word.Document.8\shell\Print To XPS

Have fun!

10 comments:

  1. PART III

    Create *.bat file, eg. PrintToXps.bat. Edit it and write

    FOR %%? IN (*.doc) DO "C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE" /mPrintToXpsWithFilenameAndClose /q /n "%%? "
    pause

    Save and copy to directory with *.DOC files. Run *.bat.

    Enjoy :)

    ReplyDelete
  2. In fact yesterday I found that for reasonable quantities you can just select them as a batch in explorer, then click on Print To XPS and the whole batch will be converted at once. The disadvantage to this method is that they are all converted simultaneously, so conceivably if you have big enough batches the batch file method above is probably superior as they are processed sequentially avoid a glut when they are all opened. I have converted 10 at once and found that the performance on my old laptop was more then acceptable.

    ReplyDelete
  3. Despite the fact that less handouts and information sheets are printed (250 rather than 1000), this technique still costs more than the internet printing methodology. Varick Street Litho

    ReplyDelete
  4. Duplicate shops will likewise enable you to make duplicates and bigger sizes. They frequently have an immense scope of paper sizes for you to look over, so in case you're planning make duplicates that are vast you can undoubtedly complete them. color copies

    ReplyDelete
  5. Never-endingly legitimate and an exceptional commitment to the universe of bloggers.
    document compliance solution

    ReplyDelete
  6. A card printing machine is made use of by many businesses today who wish to affordably print ID cards for their employees. These machines can also be used to print business cards that are plastic business cards, and thus more durable and professional in presentation. Business card printing

    ReplyDelete
  7. I know your expertise on this. I must say we should have an online discussion on this. Writing only comments will close the discussion straight away! And will restrict the benefits from this information. cheap every door direct mail postcards print

    ReplyDelete
  8. When you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style. business document software

    ReplyDelete
  9. I respect this article for the very much investigated substance and magnificent wording. I got so included in this material that I couldn't quit perusing. I am awed with your work and aptitude. Much obliged to you to such an extent. dokument

    ReplyDelete