Use a VB.Net 2005 Windows app to save a Word doc as a pdf

I have several hundred word docs that need to be converted to pdfs on a regular basis. These documnets are updated regularly (a couple times a month). The word docs are being created through a vb.net windows app and I am trying to find a way to automatically convert them to a pdf. We need both the doc and the pdf versions for our customers. Right now I have a person manually running the Acrobat distiller to perform this task which is very inefficient. Any help in this matter would be appreciated. The code I was trying to use is below but it is throws an error on the second to last line, when I'm trying to pdf the postscript object.

The error I am getting is:
System.Runtime.InteropServices.COMException was unhandled
  ErrorCode=-2147417851
  Message="The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))"
  Source="Interop.ACRODISTXLib"
  StackTrace:
       at ACRODISTXLib.PdfDistillerClass.FileToPDF(String strInputPostScript, String strOutputPDF, String strJobOptions)
       at PlanProfileDocumentGenerator.PlanProfileDocumentGenerator.btnGeneratePDFsForThisState_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\PlanProfileDocumentGenerator\PlanProfileDocumentGenerator.vb:line 1576
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at PlanProfileDocumentGenerator.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

HERE'S THE CODE:
        newDocNameandPath = filePath & strState & "\" & strdocname & "\" & newDocName
        pdfDocNameandPath = Mid(newDocNameandPath, 1, Len(newDocNameandPath) - 3) & "pdf"

        Dim oWordApp As New Word.Application
        Dim oWordDoc As Word.Document
        Dim missing As Object = System.Reflection.Missing.Value
        Dim printrange_obj As Object = Word.WdPrintOutPages.wdPrintAllPages
        Dim true_obj As Object = True
        Dim false_obj As Object = False
        Dim filename_template As String = newDocNameandPath
        Dim filename_obj As Object = DirectCast(filename_template, Object)

        oWordDoc = oWordApp.Documents.Open(filename_obj, missing, true_obj, missing, missing, missing, _
        missing, missing, missing, missing, missing, missing, _
        missing, missing, missing, missing)
        oWordDoc.Application.Visible = False

        Dim destination_ps As String = "C:\\doc1.ps"
        Dim destination_ps_obj As Object = destination_ps

        oWordDoc.PrintOut(false_obj, missing, printrange_obj, destination_ps_obj, missing, missing, _
        missing, missing, missing, missing, true_obj, missing, _
        missing, missing, missing, missing, missing, missing)

        Dim objDis As New ACRODISTXLib.PdfDistiller
        System.Threading.Thread.Sleep(5000)
        objDis.FileToPDF(destination_ps, pdfDocNameandPath, "")
        objDis = Nothing
pepper777Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

13598Commented:
Two quick things:
Why do you have double slashes in:
  Dim destination_ps As String = "C:\\doc1.ps"

and if you add a messagebox.show(pdfDocNameandPath ) right before the failing line what do you get?
0
pepper777Author Commented:
Thank you so much for your quick reply. I will try this tomorrow morning. I am out of the office for the rest of the day. I will get back to you then. Thanks again.
0
Éric MoreauSenior .Net ConsultantCommented:
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

pepper777Author Commented:
13598,
The extra backslash was just a type-o. I have adjusted it. When I use the messagebox as you stated above I get the exact path that the document should be written to. I do get a document, but it is a log file showing that there was an error in the postscript process. I've attached the log file.
xAetnaInc.-72009.log
0
pepper777Author Commented:
emoreau,
Thank you for your response, but I have no budget for this project.
0
13598Commented:
Does it create the postscript file? Can you see and open doc1.ps?
0
13598Commented:
I have a question for you: does everybody you are writing this program have the AdobePdf printer installed? If so it would be much easier to just printout the document to that printer and it would be automatically converted to pdf.
0
13598Commented:
Does this document have logos or any images? You could try using a simple document with a few lines of text to see quickly if the problem is your code or the document.
To see more information in the log file please make this change:
Viewing or printing a PostScript error message

If you think a PostScript error has occurred, but it doesn't appear on-screen or in your printout, you may be able to view or print the error message by doing one or more of the following:

-- Use an error handler utility. For example, Adobe PageMaker has the Include PostScript Error Handler option in the Print Options dialog box.

-- In Windows, configure the printer to print the error message:

Note: In Windows NT, you cannot configure a printer to print an error message.

Windows XP or 2000:

1. Choose Start > Settings > Printers.

2. Right-click the target printer, and then choose Properties from the pop-up menu.

3. Click Printing Preferences, and then click Advanced.

4. Click PostScript Options, click Send PostScript Error Handler, and then choose Yes from the pop-up menu.

Note: If PostScript Options isn't visible, double-click Document Options.

5. Click OK to close the Advanced Options dialog box, click OK to close the Printing Preferences dialog box, and then click OK to close the printer's Properties dialog box.
0
pepper777Author Commented:
13598,
Because you have helped me so much I am awarding the points to you.
I figured it out and it is working fine. Here is the code in case someone else may need to do this:

Private Sub ConvertDocToPDF
        newDocNameandPath = filePath & strState & "\" & strdocname & "\" & newDocName
        pdfDocNameandPath = Mid(newDocNameandPath, 1, Len(newDocNameandPath) - 3) & "pdf"

        Dim oWordApp As New Word.Application
        Dim oWordDoc As Word.Document
        Dim missing As Object = System.Reflection.Missing.Value
        ' our 'void' value
        Dim printrange_obj As Object = Word.WdPrintOutPages.wdPrintAllPages
        Dim true_obj As Object = True
        Dim false_obj As Object = False
        Dim filename_template As String = newDocNameandPath
        Dim filename_obj As Object = DirectCast(filename_template, Object)

        oWordDoc = oWordApp.Documents.Open(filename_obj, missing, true_obj, missing, missing, missing, _
        missing, missing, missing, missing, missing, missing, _
        missing, missing, missing, missing)
        oWordDoc.Application.Visible = False

        Dim destination_ps As String = "C:\doc1.ps"
        Dim destination_ps_obj As Object = destination_ps

        SetDefaultSystemPrinter("Adobe PDF")

        oWordDoc.PrintOut(false_obj, missing, printrange_obj, destination_ps_obj, missing, missing, _
        missing, missing, missing, missing, true_obj, missing, _
        missing, missing, missing, missing, missing, missing)

        oWordDoc = Nothing
        oWordApp.Quit()
        oWordApp = Nothing
        NAR(oWordApp)

        Dim objDis As New ACRODISTXLib.PdfDistiller
        objDis.FileToPDF(destination_ps, pdfDocNameandPath, "")
        objDis = Nothing
    End Sub

    Private Function SetDefaultSystemPrinter(ByVal strPrinterName As String) As Boolean
        Dim moReturn As Management.ManagementObjectCollection
        Dim moSearch As Management.ManagementObjectSearcher
        Dim mo As Management.ManagementObject

        moSearch = New Management.ManagementObjectSearcher("Select * from Win32_Printer")
        moReturn = moSearch.Get

        For Each mo In moReturn
            Dim objReturn As Object
            Debug.WriteLine(mo("Name"))
            If mo("Name").ToString.Trim.ToUpper = strPrinterName.Trim.ToUpper Then
                mo.InvokeMethod("SetDefaultPrinter", objReturn)
                Return True
            End If
        Next
        Return False
    End Function

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pepper777Author Commented:
13598,
How do I award this to you but still show the the code that I placed above so that others can benefit? I really struggled with this and I don't want anyone else to have to.

Thanks,
Pepper
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.