Integrating with Word, Excel, Outlook, PDF
for Office Automation
John Chun, PhD PMP
Director, IT
Takeda Pharmaceuticals International Co.
Cheryl Budaj
Sr. Manager, IT Operations
Takeda Pharmaceuticals International Co.
© 2014 ServiceNow All Rights Reserved
We Aspire to Cure Cancer™
• Fully integrated biopharmaceutical company
• Founded in 1993
– Based in Cambridge, MA with approximately 1,400 employees
– Wholly-owned subsidiary of Takeda Pharmaceuticals since May 2008
• Focused exclusively in oncology
• VELCADE® (bortezomib) for Injection
– Treatment for multiple myeloma and relapsed mantle cell lymphoma
• MEPACT® (mifamurtide) powder for infusion
– Treatment for Osteosarcoma in Europe and Switzerland
• http://www.millennium.com
© 2014 ServiceNow All Rights Reserved 2
Agenda
Objective – what were we trying to accomplish?
Motivation – why did we do this?
Solution – how did we do this?
Tips
Top Takeaways
© 2014 ServiceNow All Rights Reserved 3
Objective
Script
Automation
© 2014 ServiceNow All Rights Reserved 4
Motivation
• Deployed ServiceNow in 2012 – 2013
• Validated Incident, Problem, Change, Configuration per FDA regulations
• Lots of documentation: requirements, specifications, test scripts, traceability matrix
• Documentation was labor intensive, time consuming, and error prone
• Had to find a better way…
© 2014 ServiceNow All Rights Reserved 5
Solution – Requirements Tracker
• Built Requirements Tracker in ServiceNow
Multiple apps
State driven
© 2014 ServiceNow All Rights Reserved 6
Solution – Overview of exporting to Word templates
1. Install ServiceNow ODBC driver on PC
2. Connect to ServiceNow database using Word VBA and ADO
– VBA (Visual Basic for Application)
– ADO (ActiveX Data Objects)
3. Query ServiceNow database
4. Format data using HTML + CSS
5. Insert formatted contents in Word
6. Post processing Script
– Update Table of Contents
Automation
– Clean up Document Properties
© 2014 ServiceNow All Rights Reserved 7
Solution – Prepare Word VBA
1. In Word, ensure the Developer menu is visible
2. Go to Developer > Visual Basic
3. Go to Tools > References…
4. Add the following references
– Visual Basic for Application
– Microsoft Office 12.0 Object Library
– Microsoft Word 12.0 Object Library
– Microsoft ActiveX Data Objects 6.0 Library
Versions may vary depending on the version of Office
© 2014 ServiceNow All Rights Reserved 8
Solution – Open ServiceNow Database
• Install ServiceNow ODBC driver (match 32/64-bit to Office)
http://wiki.servicenow.com?title=ODBC_Driver
• Create ODBC connection in Control Panel > Administrative Tools
• Open database connection in VBA
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
With cn
.ConnectionString = "DSN=ODBC_Name;uid=admin;pwd=admin;"
.Open
End With
© 2014 ServiceNow All Rights Reserved 9
Solution – Query ServiceNow Database
• Query database
Dim rs As New ADODB.Recordset
rs.Open "SELECT COUNT(*) CNT FROM incident", cn
s = "" 'string to build
Do While Not rs.EOF
s = s & rs(0) & vbCrLf 'keep building string
rs.MoveNext
Loop 'iterate over all records
rs.Close
Set rs = Nothing 'clean up
© 2014 ServiceNow All Rights Reserved 10
Solution – Create Word Documents
• Simple insert
Selection.Text = s
• Find & Replace – mimic Mail Merge
Selection.HomeKey Unit:=wdStory 'put cursor at top
If Selection.find.Execute(FindText:=find) Then Selection.Text = s
• Insert HTML
PutHTMLClipboard s 'helper function to put HTML content to clipboard
Selection.PasteSpecial , , , , WdPasteDataType.wdPasteHTML
© 2014 ServiceNow All Rights Reserved 11
Solution – Export to PDF
• Export to PDF
ActiveDocument.ExportAsFixedFormat _
OutputFileName:= strFileName, _
ExportFormat:= wdExportFormatPDF, _
OpenAfterExport:=True, _
OptimizeFor:= wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, _
FROM:=1, To:=1, _
Item:=wdExportDocumentContent, _
IncludeDocProps:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, _
DocStructureTags:=True, _
BitmapMissingFonts:=True
© 2014 ServiceNow All Rights Reserved 12
Solution – Protect Document
• Protect with a Password
ActiveDocument.SaveAs FileName:=strFileName, _
Password:=strOpenPwd, _ 'password for opening file
WritePassword:=strModPwd 'password for saving file
• Protect with a Digital Signature
ActiveDocument.Signatures.AddNonVisibleSignature
Dim sig As Signature
Set sig = ActiveDocument.Signatures.Add 'open dialog box to add signature
sig.AttachCertificate = True
ActiveDocument.Signatures.Commit
© 2014 ServiceNow All Rights Reserved 13
Solution – Create Excel Spreadsheets
• In Excel, create a table from a record set
– Add reference to “Microsoft ActiveX Data Objects 6.0 Library”
– Use the same code for record set used previously for Word
Set ws = ActiveSheet
'add column headings
For iCol = 1 to rs.Fields.Count
ws.Cells(1, iCol).Value = rs.Fields(iCol - 1).Name
ws.Cells(1, iCol).Font.Bold = True
Next
ws.Range("A2").CopyFromRecordset rs 'paste record set
© 2014 ServiceNow All Rights Reserved 14
Solution – Create PowerPoint from Excel
• Export Excel data range or chart to PowerPoint
– Add reference to “Microsoft PowerPoint 12.0 Object Library”
Set ppt = New PowerPoint.Application
ppt.Activate
Set pres = ppt.Presentations.Add 'add new presentation
Set slide = pres.Slides.Add(1, ppLayoutBlank) 'add new slide
Range(rangeName).CopyPicture xlScreen, xlPicture
ActiveSheet.ChartObjects(chartName).Chart.CopyPicture xlScreen, xlPicture
Set shapes = slide.Shapes
shapes.Paste 'paste data or chart as picture
pres.SaveAs fileName
© 2014 ServiceNow All Rights Reserved 15
Solution – Create Outlook Email
• In Word, compose and send an Outlook email
– Add reference to “Microsoft Outlook 12.0 Object Library”
– Can create mail, contact, task, appointment, note, journal, etc.
Set olook = New Outlook.Application
Set mail = olook.CreateItem(olMailItem)
mail.To = "
[email protected]"
mail.Cc = "
[email protected]"
mail.Subject = "Hello world"
mail.HTMLBody = htmlString 'composed from dynamic content
mail.Display
mail.Send
© 2014 ServiceNow All Rights Reserved 16
Alternate Solutions
VBA Web Client Web Server
Visual Basic for Application Use JavaScript for Use server-side
Office automation Office component
• Easy to start • JavaScript library can • Requires third-party
be shared commercial Office
• Single user component (Aspose,
• Multi users Syncfusion, etc.)
• Hard to maintain
• Need client ODBC • Alternatively, use
driver XML + XSLT to create
DOCX from
• Use web services ServiceNow server
Improved performance and scalability
Source Information Here
© 2014 ServiceNow All Rights Reserved 17
Tips – ODBC
• Match 32/64-bit ODBC driver to the installed Office
• ODBC driver is from Progress Software; get their manual (some in manual incorrect)
– http://media.datadirect.com/download/docs/openaccess/alloa/sqlref/wwhelp/wwhimpl/js/html/wwhelp.htm
• Use SQL Query tool: Toad Data Point Freeware (renewable 120-day license)
– http://toadworld.com/m/freeware/547.aspx
• Use care when running ODBC queries on PRODUCTION
– They’re read-only, but may impact performance; consider using data warehouse
© 2014 ServiceNow All Rights Reserved 18
Tips – VBA
• To learn VBA, use the Record Macro feature
– Caution: recorded macros may be unwieldy
• VBA code can be stored in document itself or Word application – choose wisely
• Learn Word object model, properties, methods
– Get VBAWD12.chm help file (http://microsoft.com/download/details.aspx?id=22719) and books
© 2014 ServiceNow All Rights Reserved 19
Top Takeaways
Use ServiceNow for ITSM, SRM, BPM, ALM, etc.
Integrate with Microsoft Office automation
Automation
Export ServiceNow contents in popular
Office formats with unlimited possibilities!
© 2014 ServiceNow All Rights Reserved 20
John Chun, PhD PMP Cheryl Budaj
Director, IT Sr. Manager, IT Operations
Takeda Pharmaceuticals Takeda Pharmaceuticals
International Co. International Co.
[email protected] [email protected]© 2014 ServiceNow All Rights Reserved 21