Link to home
Start Free TrialLog in
Avatar of pepper777
pepper777Flag for United States of America

asked on

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
Avatar of 13598
13598
Flag of United States of America image

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?
Avatar of pepper777

ASKER

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.
Avatar of Éric Moreau
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
emoreau,
Thank you for your response, but I have no budget for this project.
Does it create the postscript file? Can you see and open doc1.ps?
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.
SOLUTION
Avatar of 13598
13598
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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