Converting Word Documents to PDF Files

Jay | Thursday, November 11, 2010 at 7:39:00 AM | 4 Comments so far |
Tags: | | | | | | |
Filed Under:
I was assigned to process Word documents and convert them into PDF files. But due to a large number of files, it is very tedious for me to do it one by one and manually. There’s no problem doing it manually, but considering the time constraints, it is not recommended to do it most especially when I am beating the deadline.

Since I have knowledge in programming using Visual Basic, I can develop a simple interface in order to process such task automatically. It is indeed a brilliant idea as it will benefit our department a lot, most especially with regard to delivering the service in a small span of time. The main problem was I don’t know where to start. Given the idea is there, I just need a simple planning before going deep into its technical aspects.

What I did first was opened up my Intenet browser to search for some ideas on how to automate this kind of task. Fortunately, there have been hundreds or more tips to consider and available on the web. I tried those I think can be useful but not all of them gives me the desired output.

The existing interface I’ve developed is too much complicated as it involves accessing the registry and other system files. Unfortunately, the IT administrators give me limited access on the desktop computer I used.

What happened to my existing program?

The said program doesn’t work properly and errors occurred. Several tests were done, until I’ve decided to make some revisions of the existing program. Research was done again including trial and error method. It really gives me a hard time.

The research done provides me some answers. This time, it will guarantee me that the program will me high efficiency rate. The main challenge probably is to develop an algorithm that will run the program faster when executed.

Sample Visual Basic Interface

The Visual Basic codes below execute the conversion process. Please refer to the given Visual Basic comments to note what every line of code does.

Private Sub WordToPDF_Click()
‘These are variable declarations
Dim wdAPPS As New Word.Application
Dim cntDOC As Integer
Dim flTEMP As String
Dim flREFS As String
Dim wdPATH As String
Dim wFILE1 As String
Dim wFILE2 As String


‘This will create new Word application object
Set wdAPPS = CreateObject(“Word.Application”)


‘Initializing the number of Word documents
cntDOC = 0


‘This loop will process the conversion of Word documents one by one
While cntDOC <= docFLE.ListCount – 1
‘This is where the Word document in the list stored depending on its index
flTEMP = docFLE.List(cntDOC)


‘This is the filename of Word document that is displayed during the process
wdSTAT = flTEMP
wdSTAT.Refresh


‘This prepares the Word document to be exported to Acrobat Distiller printer
‘If the condition is met then the value of the variable ‘flREFS’ will be executed
Select Case Right(flTEMP, 4)
Case “.doc”: flREFS = Left(flTEMP, Len(flTEMP) – 4)
Case “docx”: flREFS = Left(flTEMP, Len(flTEMP) – 5)
End Select


‘This is the whole path of the Word documents, with and without their extension
wFILE1 = wdPATH & “\” & flREFS
wFILE2 = wdPATH & “\” & flTEMP


‘This opens the Word document currently processed
wdAPPS.Documents.Open wFILE2, , 1
‘This prevents the Word application from displaying
wdAPPS.Visible = False
‘This is where the conversion process happens
‘This line of code is what I’ve been longing for
‘That this development will a lot easier with this method
wdAPPS.ActiveDocument.ExportAsFixedFormat wFILE1, wdExportFormatPDF
‘This closes the active document without saving anything
wdAPPS.ActiveDocument.Close wdDoNotSaveChanges


‘This closes the Word application
Call wdAPPS.Quit
‘The variable wdAPPS is set to Nothing
Set wdAPPS = Nothing
‘This variable doing the increments
cntDOC = cntDOC + 1
Wend


‘This message box appears after all the Word documents in the list have been processed
MsgBox “Word to PDF conversion: done!”, vbInformation, “Conversion”
End Sub

How the program works?

When the ‘Convert Files’ button is clicked, the document conversion processing started. It will convert every single Word documents listed in the ListBox to PDF file until it reads the last Word document to be converted. When the loop does not satisfy with the given condition, it will be terminated. When the loop finishes the processing, a message box will appear to note the user that conversion is already done...that’s it!

After I finished converting the Word documents to PDF files, I checked the output if the conversion process worked properly. This is very important to have an idea if there’s wrong with the process or the source code. When I checked the converted file, the information has prompted me that the file needs to be opened witht the higher version of Adobe Reader.

I used Acrobat Distiller in the conversion process as it is very helpful with regard to embedded fonts as some of the Word documents contain special characters. Since I only have Adobe Reader version 5 in my workstation due to limited access, the said message box always prompted me to use higher version of Adobe Reader. I asked the assistance of the IT people to view the file using higher version of Adobe Reader and the feedback was the file viewed fine.

I can say that the conversion has been successful. But in order to view the PDF file properly, I suggest that use Adobe Reader with higher version. You can download the latest version here.

The interface presented in this post is the result of the existing program revision. What I want to quote is that the method ExportAsFixedFormat makes my job easier. From the complicated source codes into a single method, it simplifies the process in a manner that they just produced the same output. You can try the code above for you to see how it works. You can also tweak it depending on your needs or depending on your usage.

Share this:
Rate this post:
{[['']]}
Like it?

4 comments:

Your comments are highly appreciated. Feel free to share your thoughts.

  1. I'm grateful for you because of this excellent content material. You genuinely did make my day :

    ReplyDelete
  2. Hi! Thank you and you appreciate this post. I do hope it can help even in small ways as what this article is intended to. This post undergo through several test to make it work. You can use it and tweak it for your needs to be met. Again, thanks for dropping by on my site.

    ReplyDelete
  3. Fabulous posting bro. This important is just a tremendously nicely structured post, just the critical info I was looking just for. I praise you

    ReplyDelete
  4. I've been following your blog since you started. You have made amazing progress. This site is an inspiration for all pursuing a long transition versus the big chop.

    - Rob

    ReplyDelete

JOBS-AT-FREELANCER.com


Philippines Blog Directory Blogs lists and reviews The Number Cracker Blog TopOfBlogs Personal Blog Directory Blogarama - The Blog Directory Personal Blogs Personal Blog Personal Blogs Online Marketing Globe of Blogs Personal blog at BlogFlare Blogging Fusion Blog Directory My Zimbio Top Personal-Journals Sites