On a recent project I was required to print out an Excel based report which made extensive use of OSIsoft PI DataLink's Excel Add-in on a sechduled basis (7am daily).
Unfortunately Excel does not easily allow for the printing of an Excel file via the command line via switches or parameters when calling Excel.exe (e.g. C:\Windows\MS Office\Excel.exe -print "C:\Reports\My Report.xls").
I was able to find examples on the Internet which made use of cscript or wscript where one calls VBS code, and where one provides the file name as argument, for example:
wscript "C:\Data\My Scripts\printXLS.vbs" "C:\Data\My Reports\Daily Analyser Excursion Report.xls"
Note the convention of using "" around full path filenames as they contain spaces.
The file printXLS.vbs would contain something akin to the following code:
Dim XLApp
Dim XLWkbk
Dim ObjArgs
set ObjArgs = wscript.arguments
if ObjArgs.count <> 1 then
wscript.echo "Invalid passed arguments"
wscript.quit
end if
Set XLApp = CreateObject("Excel.Application")
XLApp.Visible = False
Set XLWkbk = XLApp.Workbooks.Open(objargs(0))
XLWkbk.PrintOut
XLWkbk.Close False
XLApp.Quit
Set XLWkbk = Nothing
Set XLApp = Nothing
Set ObjArgs = nothingThe problem I experienced was Excel Add-ins are not automatically loaded in Excel applications created via Automation. This resulted in the Excel file printing but with OSIsoft PI DataLink Excel Add-in functions not being recognised and #NAME! appearing in the relevant cells on my printout.
The solution I found was to explicitly load the Excel Add-in via XLApp.RegisterXLL ("
fullpath filename")
So my VBS file looked like something this:
Dim XLApp
Dim XLWkbk
Set XLApp= CreateObject("excel.application")
XLApp.Visible = FalseSet XLWkbk = XLApp.Workbooks.Open("C:\OSIsoft\Reports\Scheduled\PI Report - Analyzers - Quality Excursions.xls")
XLApp.RegisterXLL ("C:\Program Files\PIPC\Excel\pipc32.xll")
XLWkbk.Worksheets("Report").Calculate
XLWkbk.PrintOut
XLWkbk.Close False
XLApp.Quit
Set XLWkbk = Nothing
Set XLApp = NothingI also wanted to be able to specify the number of copies to print and which printer should be used namely PRC2 in my case. If the printer is not specified then the windows default printer is used.
I also decided to allow one to specify the Excel workbook and which specific worksheet in the Excel workbook to be printed.
So allowing for parameters the VBS look something like this:
'Arguments'1 Excel Name e.g. "C:\OSIsoft\Reports\Scheduled\PI Report - Analyzers - Quality Excursions.xls"'
2 Worksheet Name e.g. "Report"'
3 Printer Name e.g. "PRC2"'
4 No of copies e.g. 1
Dim XLApp
Dim XLWkbk
Dim ObjArgs
Dim strFileName
Dim strWorkSheetName
Dim strPrinter
Dim intCopies
Set ObjArgs = wscript.arguments
If objArgs.count <> 4 Then
wscript.echo "Invalid Passed Arguments"
wscript.quit
End If
strFileName = objargs(0)
strWorkSheetName = objargs(1)
strPrinter = objargs(2)
intCopies = objargs(3)
Set XLApp= CreateObject("Excel.Application")
XLApp.Visible = False
Set XLWkbk = XLApp.Workbooks.Open(strFileName)
XLApp.RegisterXLL ("C:\Program Files\PIPC\Excel\pipc32.xll")
XLWkbk.Worksheets(strWorkSheetName).Calculate
XLWkbk.PrintOut , , intCopies, , strPrinter
XLWkbk.Close False
XLApp.Quit
Set XLWkbk = Nothing
Set XLApp = Nothing
Set ObjArgs = NothingI was then able to use Windows Task Scheduler to call the VBS file to print my Excel files on a daily basis.
Kind Regards
Bruce Bean
Industrial Data Xchange
Tel: +27 11 548 9964
www.industrialdataxchange.com /
www.idx.co.zaIndustrial Data Xchange (IDX) is a proudly South African Industrial IT solutions company focusing on the provision of products and services to all Industrial sectors including Metals & Mining, Power & Utilities, Oil & Gas, Pulp & Paper and Food & Beverages.
IDX has a broad range of IT skills related to industrial communications, PROFIBUS, OPC, legacy and disparate system integration, MIS, MES, reporting, and the development of real-time systems and custom applications in Microsoft .Net and C++.
IDX is a Microsoft Certified Partner, Microsoft Embedded Partner, OSIsoft SI & ISV, Wonderware SI & ISV, OPC Foundation Member, PROFIBUS International member, South African Electrotechnical Export Council (SAEEC) member and Proudly South African!
IDX operates the PROFIBUS Competence Centre (PCC) for Southern African, the only PCC in Africa. The PROFIBUS Competence Centre (PCC) offers internationally certified PROFIBUS training, auditing services, troubleshooting services, and PROFIBUS-related support.
IDX also operates the OPC Competence Centre (OCC) and recently established OPC Africa, the local chapter for the OPC Foundation. The OPC Competence Centre (OCC) aims to drive and support the adoption and development of OPC skills in the region through training and technical workshops.