none
Converting XPS file to PDF RRS feed

  • Question

  • Hello, 

    I need to write program which automatically convert all xps files from foler into pdfs. One of my ideas is using pattern -> Opening XPS file, then printing it with "Adobe PDF" printer. 

    Can you give me any help how I can write a program to do it? Or maybe you have another idea how I can do it?

    I write similar topic in VB forum, but unfortunately I can't use these advice's in VBA. Maybe it will be helpful to you: 


    https://social.msdn.microsoft.com/Forums/vstudio/en-US/247d2986-e312-4793-8b08-01a4225bbf37/converting-xps-file-to-pdf?forum=vbgeneral#69af5090-3581-4042-89bc-30f6c6ecea51 Thanks a lot for help.

    Best regards, 
    Robert

    Monday, March 30, 2015 11:54 AM

Answers

  • Worked for me.  Need to be careful on quotes. Run cmd on command prompt to make sure it works.

    Sub xps()
    
      Dim pdfFile As String
      Dim xpsFile As String
      Dim exe As String
      Dim cmd As String
      Dim wsh As Object
      Set wsh = VBA.CreateObject("WScript.Shell")
      Dim waitOnReturn As Boolean: waitOnReturn = True
      Dim windowStyle As Integer: windowStyle = 1
      exe = "C:\temp\ghostxps-9.16-win32\gxps-9.16-win32.exe"
      pdfFile = "C:\temp\myfile.pdf"
      xpsFile = "C:\temp\myfile.xps"
      cmd = """" & exe & """" & " -sDEVICE=pdfwrite -sOutputFile=" & """" & pdfFile & """" & " -dNOPAUSE " & """" & xpsFile & """"
      
      wsh.Run cmd, windowStyle, waitOnReturn
    
    End Sub


    • Edited by mogulman52 Monday, March 30, 2015 5:42 PM
    • Marked as answer by RobRaj Tuesday, March 31, 2015 9:24 AM
    Monday, March 30, 2015 5:38 PM

All replies

  • There appear to be free converters:

    http://download.cnet.com/Free-XPS-to-PDF-Converter/3000-18497_4-75895953.html

    I have not used it.

    Monday, March 30, 2015 12:33 PM
  • Thank you for answer, but I need to write manually a code which is making it, because it will be just one part of my macro.

    Thank you anyway :)

    Monday, March 30, 2015 12:37 PM
  • Then use a command line tool like GhostScript. 

    https://code.google.com/p/ghostscript/downloads/list

    You can call it from VBA with something like this:

    Dim wsh As Object
    Set wsh = VBA.CreateObject("WScript.Shell")
    Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim windowStyle As Integer: windowStyle = 1
    
    wsh.Run "C:\programs\gxps.exe -sDEVICE=pdfwrite -sOutputFile=myfile.pdf -dNOPAUSE myfile.xps", windowStyle, waitOnReturn

    I haven't used it for this but it should work.


    • Edited by mogulman52 Monday, March 30, 2015 2:02 PM
    Monday, March 30, 2015 2:01 PM
  • Thank you a lot! 

    I only want to ask you for little more help with installing this in my macro. I already downloaded this files: 

    http://ghostscript.com/download/gsdnld.html , Ghostscript GPL Release  for Windows (64 bits) and

    http://ghostscript.com/download/gxpsdnld.html 

    and tried with 

    (XPS version, 2 one)

    Sub xps()
    
    
    Dim wsh As Object
    Set wsh = VBA.CreateObject("WScript.Shell")
    Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim windowStyle As Integer: windowStyle = 1
    
    wsh.Run "C:\Users\xxx\Downloads\ghostxps-9.16-win32\ghostxps-9.16-win32.exe -sDEVICE=pdfwrite -sOutputFile=myfile.pdf -dNOPAUSE C:\test\plikxps.xps", windowStyle, waitOnReturn
    
    End Sub
    

    Method "run" or object 'IWshShell3" failed. I get the same error when I try to enter also the first (general) exe ghostscript file.

    How I can fix it?

    Best regards,
    Robert

    Monday, March 30, 2015 3:00 PM
  • Worked for me.  Need to be careful on quotes. Run cmd on command prompt to make sure it works.

    Sub xps()
    
      Dim pdfFile As String
      Dim xpsFile As String
      Dim exe As String
      Dim cmd As String
      Dim wsh As Object
      Set wsh = VBA.CreateObject("WScript.Shell")
      Dim waitOnReturn As Boolean: waitOnReturn = True
      Dim windowStyle As Integer: windowStyle = 1
      exe = "C:\temp\ghostxps-9.16-win32\gxps-9.16-win32.exe"
      pdfFile = "C:\temp\myfile.pdf"
      xpsFile = "C:\temp\myfile.xps"
      cmd = """" & exe & """" & " -sDEVICE=pdfwrite -sOutputFile=" & """" & pdfFile & """" & " -dNOPAUSE " & """" & xpsFile & """"
      
      wsh.Run cmd, windowStyle, waitOnReturn
    
    End Sub


    • Edited by mogulman52 Monday, March 30, 2015 5:42 PM
    • Marked as answer by RobRaj Tuesday, March 31, 2015 9:24 AM
    Monday, March 30, 2015 5:38 PM
  • Now it works great, thank you! :)
    Tuesday, March 31, 2015 9:24 AM
  • I'm not exactly sure what an XFP file is, but the code below will convert all Excel files in a folder into PDF files.

    Sub Convert_Excel_To_PDF()
        Dim MyPath As String, FilesInPath As String
        Dim MyFiles() As String, Fnum As Long
        Dim mybook As Workbook
        Dim CalcMode As Long
        Dim sh As Worksheet
        Dim ErrorYes As Boolean
        Dim LPosition As Integer
     
        'Fill in the path\folder where the Excel files are
        MyPath = "c:\Documents and Settings\shuerya\Desktop\ExcelFiles\"
     
        FilesInPath = Dir(MyPath & "*.xl*")
        If FilesInPath = "" Then
            MsgBox "No files found"
            Exit Sub
        End If
     
        Fnum = 0
        Do While FilesInPath <> ""
            Fnum = Fnum + 1
            ReDim Preserve MyFiles(1 To Fnum)
            MyFiles(Fnum) = FilesInPath
            FilesInPath = Dir()
        Loop
     
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            .EnableEvents = False
        End With
     
        If Fnum > 0 Then
            For Fnum = LBound(MyFiles) To UBound(MyFiles)
                Set mybook = Nothing
                On Error Resume Next
                Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
                On Error GoTo 0
     
                If Not mybook Is Nothing Then
     
                        
                        LPosition = InStr(1, mybook.Name, ".") - 1
                        mybookname = Left(mybook.Name, LPosition)
                        mybook.Activate
                        'All PDF Files get saved in the directory below:
                        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                            "C:\Documents and Settings\shuerya\Desktop\PDFFiles\" & mybookname & ".pdf", _
                            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
                            :=False, OpenAfterPublish:=False
                            
                End If
                
                mybook.Close SaveChanges:=False
     
            Next Fnum
        End If
     
        If ErrorYes = True Then
            MsgBox "There are problems in one or more files, possible problem:" _
                 & vbNewLine & "protected workbook/sheet or a sheet/range that not exist"
        End If
     
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = CalcMode
        End With
    End Sub
    


    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

    Wednesday, April 1, 2015 10:59 PM