pepper777
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.InteropServ ices.COMEx ception was unhandled
ErrorCode=-2147417851
Message="The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))"
Source="Interop.ACRODISTXL ib"
StackTrace:
at ACRODISTXLib.PdfDistillerC lass.FileT oPDF(Strin g strInputPostScript, String strOutputPDF, String strJobOptions)
at PlanProfileDocumentGenerat or.PlanPro fileDocume ntGenerato r.btnGener atePDFsFor ThisState_ Click(Obje ct sender, EventArgs e) in C:\Inetpub\wwwroot\PlanPro fileDocume ntGenerato r\PlanProf ileDocumen tGenerator .vb:line 1576
at System.Windows.Forms.Contr ol.OnClick (EventArgs e)
at System.Windows.Forms.Butto n.OnClick( EventArgs e)
at System.Windows.Forms.Butto n.OnMouseU p(MouseEve ntArgs mevent)
at System.Windows.Forms.Contr ol.WmMouse Up(Message & m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Butto nBase.WndP roc(Messag e& m)
at System.Windows.Forms.Butto n.WndProc( Message& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.De buggableCa llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Unsaf eNativeMet hods.Dispa tchMessage W(MSG& msg)
at System.Windows.Forms.Appli cation.Com ponentMana ger.System .Windows.F orms.Unsaf eNativeMet hods.IMsoC omponentMa nager.FPus hMessageLo op(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoopInner (Int32 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoop(Int3 2 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Run (Applicati onContext context)
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.OnRun( )
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.DoAppl icationMod el()
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.Run(St ring[] commandLine)
at PlanProfileDocumentGenerat or.My.MyAp plication. Main(Strin g[] Args) in 17d14f5c-a337-4978-8281-53 493378c107 1.vb:line 81
at System.AppDomain.nExecuteA ssembly(As sembly assembly, String[] args)
at System.AppDomain.ExecuteAs sembly(Str ing assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.Hos tingProces s.HostProc .RunUsersA ssembly()
at System.Threading.ThreadHel per.Thread Start_Cont ext(Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
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.wdPri ntAllPages
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_templa te, Object)
oWordDoc = oWordApp.Documents.Open(fi lename_obj , missing, true_obj, missing, missing, missing, _
missing, missing, missing, missing, missing, missing, _
missing, missing, missing, missing)
oWordDoc.Application.Visib le = False
Dim destination_ps As String = "C:\\doc1.ps"
Dim destination_ps_obj As Object = destination_ps
oWordDoc.PrintOut(false_ob j, 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.Sl eep(5000)
objDis.FileToPDF(destinati on_ps, pdfDocNameandPath, "")
objDis = Nothing
The error I am getting is:
System.Runtime.InteropServ
ErrorCode=-2147417851
Message="The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))"
Source="Interop.ACRODISTXL
StackTrace:
at ACRODISTXLib.PdfDistillerC
at PlanProfileDocumentGenerat
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at Microsoft.VisualBasic.Appl
at Microsoft.VisualBasic.Appl
at Microsoft.VisualBasic.Appl
at PlanProfileDocumentGenerat
at System.AppDomain.nExecuteA
at System.AppDomain.ExecuteAs
at Microsoft.VisualStudio.Hos
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Threading.ThreadHel
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.
Dim printrange_obj As Object = Word.WdPrintOutPages.wdPri
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_templa
oWordDoc = oWordApp.Documents.Open(fi
missing, missing, missing, missing, missing, missing, _
missing, missing, missing, missing)
oWordDoc.Application.Visib
Dim destination_ps As String = "C:\\doc1.ps"
Dim destination_ps_obj As Object = destination_ps
oWordDoc.PrintOut(false_ob
missing, missing, missing, missing, true_obj, missing, _
missing, missing, missing, missing, missing, missing)
Dim objDis As New ACRODISTXLib.PdfDistiller
System.Threading.Thread.Sl
objDis.FileToPDF(destinati
objDis = Nothing
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.
I use http://www.aspose.com/categories/.net-components/aspose.words-for-.net/default.aspx to do that kind of job
ASKER
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
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
ASKER
emoreau,
Thank you for your response, but I have no budget for this project.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
Why do you have double slashes in:
Dim destination_ps As String = "C:\\doc1.ps"
and if you add a messagebox.show(pdfDocName