📜 ⬆️ ⬇️

Automate word processing using the Microsoft Word API

Much of the text documents today are created and edited in Microsoft Word. The presence of this program on almost every computer makes it possible to automate the printing, processing and export of text documents to PDF using the Microsoft Word API .

In this article, I will explain how to automate the printing of text documents and the conversion of documents into PDF.

An example of a source code for printing a text document written in Microsoft Visual Basic .NET language looks like this:

Dim app = CreateObject("Word.Application") Dim doc = app.Documents.Open("D:\in\my resume.doc") app.PrintOut(False) doc.Close() app.Quit() 

This code looks very simple. But to automate the printing of a list of documents should take into account a number of nuances:
')
1. People need the ability to set the name of the printer to be used.
2. You need to make Word work in invisible mode, and not appear on the screen when processing each document
3. It is necessary to disable all informational messages and requests for confirmation of operations with documents.
4. It is necessary to disable the addition of the files processed in automatic mode to the “Recent files” list.

The finalized version of our program will look like this:

 Dim app = CreateObject("Word.Application") app.Visible = False app.DisplayAlerts = 0 app.FeatureInstall = 0 app.DisplayRecentFiles = False app.DisplayDocumentInformationPanel = False app.AutomationSecurity = 3 Dim wdOptions = app.Options wdOptions.WarnBeforeSavingPrintingSendingMarkup = False wdOptions.SavePropertiesPrompt = False wdOptions.DoNotPromptForConvert = True wdOptions.PromptUpdateStyle = False wdOptions.ConfirmConversions = False Dim doc = app.Documents.Open("D:\in\my resume.doc") doc.Application.ActivePrinter = "Xerox Global Print Driver PS" app.PrintOut(False) doc.Saved = True doc.Close(0) app.Quit() 

I believe that this code is quite enough to pass the university programming test. But before introducing it into a commercial application, several more questions should be solved.

1. This code will not work if the input file has the attribute “Read only”.
2. The performance of your program can be increased several times, if you open and close only documents, and leave Microsoft Word open.
3. Many users need the ability to print only a specified range of pages, and not the entire document, as well as the ability to print multiple copies of the document.
4. This code will not work if your program is launched from Windows Tasks Scheduler or as Windows Service.

In this article I will not talk about how to solve these four problems, so as not to make the life of my readers too boring. Let me just say that in the Print Conductor , FolderMill and 2Printer programs they were successfully solved by me.

To convert a document to PDF, you need to replace the PrintOut function with ExportAsFixedFormat. After this, our code will look like this:

 Dim app = CreateObject("Word.Application") app.Visible = False app.DisplayAlerts = 0 app.FeatureInstall = 0 app.DisplayRecentFiles = False app.DisplayDocumentInformationPanel = False app.AutomationSecurity = 3 Dim wdOptions = app.Options wdOptions.WarnBeforeSavingPrintingSendingMarkup = False wdOptions.SavePropertiesPrompt = False wdOptions.DoNotPromptForConvert = True wdOptions.PromptUpdateStyle = False wdOptions.ConfirmConversions = False Dim doc = app.Documents.Open("D:\in\my resume.doc") doc.ExportAsFixedFormat("D:\out\my resume.pdf", 17) doc.Saved = True doc.Close(0) app.Quit() 

This example will work if you have Microsoft Word 2007 SP2 or a newer version on your computer.

The ExportAsFixedFormat function will not work if no printers are installed on the computer. Or if one of the installed printers is not assigned as a system default printer.

The code for exporting documents to PDF can be improved by implementing the ability to export parts of the document pages, as well as the ability to export documents to PDF / A. Microsoft has implemented both of these features in the ExportAsFixedFormat function.

Examples of commercial applications that use this code to convert documents to PDF: DocuFreezer and FolderMill .

Source: https://habr.com/ru/post/275989/


All Articles