Scheduling the printing of an Excel file which has an add-in from the command line using visual basic scripting (vbs) via wscript

25 August 2009

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 scheduled basis (7 am 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 an 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 = nothing

The 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 = Nothing

I 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 = Nothing

I was then able to use Windows Task Scheduler to call the VBS file to print my Excel files daily.

Categories: Articles
<a href="https://blog.idx.co.za/author/bruce/" target="_self">Bruce Bean</a>

Bruce Bean

Academy and Site Services Manager

Bruce has over thirty years of experience in the consulting, information technology, project management and software development industries. His hobbies include karate, pilates, rebounding, piano and playing pool.

Show your love!

Recent Posts

Building Success on the Fundamentals of Operational Excellence

In the previous article, we explored how operational excellence extends beyond traditional management, emphasising a culture of continuous improvement. We highlighted the importance of industrial communication systems in enhancing real-time decision-making and...

Reduce Energy Consumption in Buildings with AC Cloud Control

Intesis’ Air Conditioning (AC) Cloud Control is a cloud-based remote-control system that allows comfortable and intuitive control of air conditioners and heat pumps. Based on a brand-agnostic cloud-based platform, it brings remotely accessible smart intelligent...

Revolutionising HVAC systems for a more sustainable future

Heating, Ventilation, and Air Conditioning (HVAC) systems are key in buildings' energy consumption and greenhouse gas emissions. With buildings responsible for over one-third of global energy consumption, optimising HVAC systems is crucial for a sustainable future....

Steady Growth in Industrial Network Market

Market shares 2024 according to HMS Networks – fieldbus, industrial Ethernet and wireless.  Every year, HMS Networks conducts a comprehensive analysis of the industrial network market, aiming to estimate the distribution of new connected nodes by type and protocol...

Moving Beyond Master and Slave Terminology

The terms "master" and "slave" have long been used in technology to describe the relationship between a controlling device (master) and a subordinate or controlled device (slave). This terminology, while deeply embedded in various technical disciplines, has its roots...

An Introduction to Operational Excellence

Operational excellence goes beyond mere operational management. It represents a strategic mindset and a culture of continuous improvement, innovation, and customer-centricity, striving for excellence in all operational and business performance aspects. Trends and...

Driving Productivity with LineView Training

In light of our partnership with LineView Solutions, our team has taken up the opportunity to equip themselves with insights into the LineView system and its impact on the industry. “LineView is a production line monitoring and recording system that automatically...

Exciting New Launch: Onsite ProfiDay

IDX is excited to announce the launch of our onsite ProfiDay offering. This is available to all industries and companies that utilise PROFIBUS and/or PROFINET within their industrial communications systems. What is ProfiDAY? A free onsite training programme, where...

Sign up for our newsletter

Get exclusive updates and a sneak peek into our world every two months. Stay connected, stay informed.

Related Posts

Our expertise lies in establishing, maintaining, and leveraging plant data for business benefit. Through our innovative solutions, we work alongside you to streamline processes, enhance efficiency, and reduce costly downtime.

CONTACT US

Main: +27 (11) 548 9960
Sales: +27 (11) 548 9970

SOCIAL MEDIA

Copyright @2024 Industrial Data Xchange. All rights reserved.