WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Excel
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Excel to PDF


Update: Check out the 2007 Microsoft Office Add-in: Microsoft Save as PDF http://www.microsoft.com/downloads/details.aspx?FamilyId=F1FC413C-6D89-4F15-991B-63B07BA5F2E5&displaylang=en


;////////////////////////////////////////////////////////////////////////////////
;//  WINBATCH - Excel Script to Print WorkSheet To PDF File with OLE Code
;////////////////////////////////////////////////////////////////////////////////
;//  1. Optionally Set/Reset INI Entries for Print Properties. [suggested]
;//  2. Sets Printer and Outputs Sheet 1
;//  3. VBA Code to Print all sheets and combine to single PDF suggested
;//
;//
;//  Stan Littlefield update:04/01/2001
;////////////////////////////////////////////////////////////////////////////////

;ini settings - sometbing like
;cINI   = strcat(DirGet(),"XCELPDF.INI")
;pArray = ArrDimension(9,0,0,0,0)
;pArray[0] = 0
;pArray[1] = 0
;pArray[2] = 2
;pArray[3] = 1
;pArray[4] = 0
;pArray[5] = 0
;pArray[6] = 841
;pArray[7] = 595
;pArray[7] = 1
;IniWritePvt("Settings","cpmarginwhole",pArray[0],cINI)
;IniWritePvt("Settings","cpmarginpart",pArray[1],cINI)
;IniWritePvt("Settings","cpunits",pArray[2],cINI)
;IniWritePvt("Settings","custom",pArray[3],cINI)
;IniWritePvt("Settings","bDocInfo",pArray[4],cINI)
;IniWritePvt("Settings","bExecViewer",pArray[5],cINI)
;IniWritePvt("Settings","cpheightwhole",pArray[6],cINI)
;IniWritePvt("Settings","cpwidewhole",pArray[7],cINI)
;IniWritePvt("Settings","orient",pArray[8],cINI)

cXLS = AskFileName("Select Excel Template",".\","Excel Files|*.xls|","*.xls",1)

:start
If cXLS == ""
   Return
Endif

cOUT = StrReplace( StrUpper(cXLS), ".XLS",".PDF")
IF FileExist(cOUT) == @TRUE
  FileDelete(cOUT)
Endif

cText = strcat("Opening File...",@CRLF)
BoxOpen("Outputting %cXLS% to PDF",cText)

DB             = ObjectOpen("Excel.Application")
DB.Visible     = @FALSE
DB.UserControl = @FALSE
oAPP           = DB.Workbooks

oAPP.Open("%cXLS%")
oACT   = DB.ActiveWorkbook
oWS    = DB.Worksheets(1)
oWS.Activate
currentPrinter = DB.ActivePrinter
DB.ActivePrinter = "Acrobat PDFWriter on LPT1:"

cName  = oWS.Name
cText  = strcat(cText,"Printing WorkSheet:%cName%",@CRLF)
Boxtext(cText)
oWS.PrintOut
DB.ActivePrinter = currentPrinter
BoxShut()


ObjectClose(oWS)
ObjectClose(RS)
oAPP.Close()
ObjectClose(oAPP)
ObjectClose(DB)
exit

:cancel
cXLS == ""
Goto start


; VBA Code For printing all sheets and combining them into a
; single PDF
For i = 1 To Worksheets.Count()
        Set wks = Worksheets(i)
        pdfName$ = TARGET_DIR & "wks" & i & ".pdf"
        WriteIniFile "PDFFilename", pdfName$
        Worksheets(i).PrintOut
    Next
    
    Set pdDoc1 = CreateObject("AcroExch.PDDoc")
    Set pdDoc2 = CreateObject("AcroExch.PDDoc")
    szTarget = TARGET_DIR & "wks1.pdf"
    Call pdDoc1.Open(szTarget)
    
    For i = 2 To Worksheets.Count()
        n = pdDoc1.GetNumPages()
        bOK = pdDoc2.Open(TARGET_DIR & "wks" & i & ".pdf")
        bOK = pdDoc1.InsertPages(n - 1, pdDoc2, 0, pdDoc2.GetNumPages(), False)
        pdDoc2.Close
        Kill TARGET_DIR & "wks" & i & ".pdf"
        bOK = pdDoc1.Save(1, szTarget)
    Next i
    

CleanUp:
    pdDoc1.Close
    Set pdDoc1 = Nothing
    Set pdDoc2 = Nothing

Article ID:   W16615
File Created: 2007:07:20:08:53:58
Last Updated: 2007:07:20:08:53:58