Hi Everyone, i m in middle of the project where i need to make print to pdf for every available document, could be ‘pdf’,‘.png’,‘jpg’,‘jpeg’,‘.mdg’ or others.
i know VBA but for vb.net i m not expert, somehow i mange the code which i allowing me to make print to pdf for ‘pdf’ and ‘.msg’ only rest of the format it does not take and come out.
also just want to highlight when it runs for even ‘pdf’, it opens a dailog box to enter the file name save
here is the code:-
Try
Dim psi As New System.Diagnostics.ProcessStartInfo()
Dim filepath = Inputfile
psi.UseShellExecute = True
psi.Verb = “print”
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.FileName = filepath
Dim myProcess As System.Diagnostics.Process
myProcess = System.Diagnostics.Process.Start(psi)
myProcess.WaitForInputIdle()
Dim localProces As Diagnostics.Process = Diagnostics.Process.GetProcessById(myProcess.Id)
localProces.CloseMainWindow()
localProces.Close()
Catch ex As Exception
'MessageBox.Equals(ex.Message())
End Try
how i can improve this code make print to pdf for all kind of file and by pass the dialog box asking to save the file, pls suggest it should directly it take take the output file path from my out argument
' Assuming inputFile and outputFile are passed as arguments to the Invoke Code activity
' inputFile As String, outputFile As String
Try
' Determine the file type
Dim fileExtension As String = System.IO.Path.GetExtension(inputFile).ToLower()
Select Case fileExtension
Case ".pdf"
PrintPDF(inputFile, outputFile)
Case ".png", ".jpg", ".jpeg", ".bmp", ".tiff"
PrintImage(inputFile, outputFile)
Case ".msg"
PrintMSG(inputFile, outputFile)
Case Else
Throw New NotSupportedException("File type not supported")
End Select
Catch ex As Exception
' Handle exceptions as needed
Throw ex
End Try
' Function to print PDF files
Private Sub PrintPDF(inputFile As String, outputFile As String)
Dim psi As New ProcessStartInfo()
psi.UseShellExecute = True
psi.Verb = "printto"
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.FileName = inputFile
psi.Arguments = """Microsoft Print to PDF""" & " """ & outputFile & """"
Dim myProcess As Process = Process.Start(psi)
myProcess.WaitForExit()
End Sub
' Function to print image files
Private Sub PrintImage(inputFile As String, outputFile As String)
Using bmp As New System.Drawing.Bitmap(inputFile)
Dim pd As New PrintDocument()
pd.PrinterSettings = New PrinterSettings()
pd.PrinterSettings.PrinterName = "Microsoft Print to PDF"
pd.PrinterSettings.PrintFileName = outputFile
pd.PrinterSettings.PrintToFile = True
AddHandler pd.PrintPage, Sub(sender, e)
e.Graphics.DrawImage(bmp, e.PageBounds)
End Sub
pd.Print()
End Using
End Sub
' Function to print MSG files
Private Sub PrintMSG(inputFile As String, outputFile As String)
Dim psi As New ProcessStartInfo()
psi.UseShellExecute = True
psi.Verb = "printto"
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.FileName = inputFile
psi.Arguments = """Microsoft Print to PDF""" & " """ & outputFile & """"
Dim myProcess As Process = Process.Start(psi)
myProcess.WaitForExit()
End Sub
Thank very much for the reply, is this working on your system, because i have few errors.
like below attached. also can u pls tell me do we required to write sub main() and End sub because it says end sub expected
The code provided by @Kalpesh_Chaudhari will not work in InvokeCode activity without adjustment - e.g. you can not define “Sub” function inside InvokeCode.
Maybe he will explain his proposal in more detail later.
Cheers
EDIT: I suppose it is some AI generated code not taking into account InvokeCode activity context and limitations. But it may be used as basis for coded workflow.
yes i checked it, unfortunately now working it has the same error which i showed previously. m i missing any dll here also do we need to use sub(), if not required then how it will get the function argument @J0ska can you also comment if missing any dll to printdocument and setting
You could try reorganize the provided code by putting the actual code of each “Sub” into respective “Case” - instead of calling of the “Sub”
' Assuming inputFile and outputFile are passed as arguments to the Invoke Code activity
' inputFile As String, outputFile As String
Try
' Determine the file type
Dim fileExtension As String = System.IO.Path.GetExtension(inputFile).ToLower()
Select Case fileExtension
Case ".pdf"
Dim psi As New ProcessStartInfo()
psi.UseShellExecute = True
psi.Verb = "printto"
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.FileName = inputFile
psi.Arguments = """Microsoft Print to PDF""" & " """ & outputFile & """"
Dim myProcess As Process = Process.Start(psi)
myProcess.WaitForExit()
Case ".png", ".jpg", ".jpeg", ".bmp", ".tiff"
Using bmp As New System.Drawing.Bitmap(inputFile)
Dim pd As New System.Drawing.Printing.PrintDocument()
pd.PrinterSettings = New PrinterSettings()
pd.PrinterSettings.PrinterName = "Microsoft Print to PDF"
pd.PrinterSettings.PrintFileName = outputFile
pd.PrinterSettings.PrintToFile = True
AddHandler pd.PrintPage, Sub(sender, e)
e.Graphics.DrawImage(bmp, e.PageBounds)
End Sub
pd.Print()
End Using
Case ".msg"
Dim psi As New ProcessStartInfo()
psi.UseShellExecute = True
psi.Verb = "printto"
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.FileName = inputFile
psi.Arguments = """Microsoft Print to PDF""" & " """ & outputFile & """"
Dim myProcess As Process = Process.Start(psi)
myProcess.WaitForExit()
Case Else
Throw New NotSupportedException("File type not supported")
End Select
Catch ex As Exception
' Handle exceptions as needed
Throw ex
End Try
yes i did this, and thanks to know that there are no errors now coming from code prompt. but unfortunately when i m running this getting same error printdocument and printer setting are not defined. then i tried to pass it as an argument through “Edit arguments” button. now UiPath is throwing only exception has been thrown by the target of an invocation. and you were right there was no dll issue because i can see those properties by putting pd.printersettings(). any idea for the latest exception thrown by ui path , how to even know what is the error
this is the code now i m using
Dim output As String
output=“D:\New Sample\abc.pdf”
Try
’ Determine the file type
Dim fileExtension As String = System.IO.Path.GetExtension(inputFile).ToLower()
Select Case fileExtension
Case ".pdf"
'PrintPDF(Inputfile, output)
Dim psi As New ProcessStartInfo()
psi.UseShellExecute = True
psi.Verb = "printto"
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.FileName = inputFile
psi.Arguments = """Microsoft Print to PDF""" & " """ & output & """"
Dim myProcess As Process = Process.Start(psi)
myProcess.WaitForExit()
Case ".png", ".jpg", ".jpeg", ".bmp", ".tiff"
'PrintImage(inputFile, output)
Using bmp As New System.Drawing.Bitmap(inputFile)
'Dim pd As New PrintDocument()
pd.PrinterSettings = New PrinterSettings()
pd.PrinterSettings.PrinterName = "Microsoft Print to PDF"
pd.PrinterSettings.PrintFileName = output
pd.PrinterSettings.PrintToFile = True
AddHandler pd.PrintPage, Sub(sender, e)
e.Graphics.DrawImage(bmp, e.PageBounds)
End Sub
pd.Print()
End Using
Case ".msg"
'PrintMSG(inputFile, output)
Dim psi As New ProcessStartInfo()
psi.UseShellExecute = True
psi.Verb = "printto"
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.FileName = inputFile
psi.Arguments = """Microsoft Print to PDF""" & " """ & output & """"
Dim myProcess As Process = Process.Start(psi)
myProcess.WaitForExit()
Case Else
Throw New NotSupportedException("File type not supported")
End Select
Catch ex As Exception
’ Handle exceptions as needed
Throw ex
End Try
see how i m passing argument rather declaring in code stage
I prepared a test bench for just PDF printing.
The code runs fine but Acrobat throws error “Access denied”.
I do not have time to troubleshoot it so you need to google or ask AI.
i think there is some issue because if run it for pdf it eventually opened a saveas dialog box to save the new file, however in the case of nonpdf (specially non pdf and nonmsg), we are directing it to output variable, hard to understand this
For MSG the error thrown is:
System.ComponentModel.Win32Exception: An error occurred trying to start process ‘\myfolder\myfile.msg’ with working directory '\myfolder. No application is associated with the specified file for this operation. at UiPathCodeRunner_5ac56c8f17054f0f9bbb32be7fd47676.Run(String inputFile, String outputFile)
So I guess the code has some flaw/the method is not suitable for this case