Running PowerPoint macro from PowerShell - vba

I have been experiencing troubles when running a PowerPoint Macro from Powershell (running Excel macros already works). As there is not much info on the web regarding this topic, I'm willing that you will be able to help me.
I'm trying to automate reporting in my work area and the flow is to query data from MS SQL Server, import data into Excel file, refresh charts and then build the final report with PowerPoint slides.
PowerPoint file's charts' are linked with the Excel (via File -> Edit Links to Files) and after selecting the specific chart, Chart Tools menu appears where on the Design tab I just click "Refresh Data" and the chart refreshes. I already have a macro which refreshes data automatically for me. Now I want to call that macro from Powershell script. Please see my code below:
$ppt = New-Object -ComObject Powerpoint.Application
$FilePath = '\\c03d03\public\Data\uvall\IBNB\OnePager.pptm'
$presentation = $ppt.presentations.Open($FilePath)
$ppt.Visible = $true
$app = $ppt.Application
$tst = #('test')
$presentation.Application.Run("RefreshData",[ref]$tst) / 1st way
$app.Run("RefreshData", [ref]#()) / 2nd way
$app.Run("RefreshData", #()) / 3rd way
$app.Run("RefreshData") / 4th way
$presentation.save()
$presentation.close()
$ppt.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ppt)
And none of these 4 ways of calling a macro works. Sometimes it just returns an error that the "Sub or function not defined", even if it's truly defined, sometimes it returns "You cannot call a method on a null-valued expression" and other errors.
Related:
Run PowerPoint Macro from PowerShell
Automate Powerpoint Macro
Please also be aware that I'm using Office 2013
Maybe someone can carefully look into this and help me out!

Related

PowerPoint 2013 macro keeps file locked open after close command

I have a PowerPoint VBA function that opens presentations, copies slides into the active presentation, then closes the source presentation. It worked fine in 2010, but fails in 2013 (all on Windows 7) if it tries to open the same presentation more than once. It appears to me that after the presentation.close command is issued, the window is closed, but the file remains locked open until the VBA code exits. So if the code attempts to open that file again it returns the error:
"Method 'Open' of object 'Presentations' failed"
Here's a simplified form of the function I'm running that behaves the same way. I've had a colleague test this again in PowerPoint 2010 and it runs fine. I've also had a colleague test it under his 2013 to make sure it's not something with my particular installation.
Sub testopen()
Dim ppFile As Presentation
Dim i As Integer
Const fpath = "C:\test.pptx"
For i = 1 To 2
Set ppFile = Application.Presentations.Open(fpath)
ppFile.Close
Set ppFile = Nothing
Next i
End Sub
The file test.pptx is just a blank presentation. In debug mode I can see the file opens and closes on the first loop, then on the second loop the open command fails and I can see in Windows explorer that the hidden temporary file still exists, indicating the file is still open, until I exit the VBA code. I also verified that the file is held open by adding in a function to check the file open status.
I've spent probably an hour googling this and cannot find any other descriptions of this problem. I'm sure I can implement a workaround but it's driving me crazy that I can't find any other reports of seemingly such a simple issue. Any suggestions are greatly appreciated! Thanks.
The Best way that I have achieved this is to simply create a VBS file and in the VBS file I call out the desired VBA code. It's little more hassle than to write the VBA code, but it's the solution that worked for me.
For example in the VBS file:
Dim args, objPP
Set args = WScript.Arguments
Set objPP = CreateObject("Powerpoint.Application")
objPP.Open "C:\path\to\file.ppx"
objPP.Visible = True
objPP.Run "The_Macro"
objPP.Save
objPP.Close(0)
objPP.Quit
Or better yet, have the entire code within the VBS file and have it copy the desired slides.
Hope this helps you achieve your result.
Setting the file as Read Only resolved the issue. The open command is now:
Set ppFile = Application.Presentations.Open(fpath, msoTrue)
Also, saving the file before closing it resolved the issue. For that, add:
ppFile.Save
Interestingly, I had already tried setting the Saved property to True (ppFile.Saved = msoTrue), which does NOT work. Thanks to Michael for his suggestion on the VBS script. That does work and I had never run an external VBS script so I learned something new. In this case, I'd prefer to stick with a VBA solution.

Automatically refresh Excel ODC connections and pivots without opening the file PowerShell

I have several 20+ MB Excel files, and they need to be refreshed every week before business starts (Monday 8 AM).
These files contain one Data sheet, and data comes via external connection (ODC file), from an SQL Server view.
They also have one pivot sheet that also needs to be refreshed after the Data sheet is refreshed.
I am trying to find a solution (Windows PowerShell) to automatize the refreshing of Data and Pivot sheets without the need to touch the files.
"Refresh on opening" and other Excel options are not viable because it takes up to 20 minutes to refresh all the connections.
I also don't want to refresh ALL sheets because the file has custom coloring for charts and "Refresh" resets it to Excel default which cannot happen.
I tried this, but it doesn't seem to work with ODC connection? At least, it doesn't do anything.:
Windows PowerShell:
$ExcelApp = new-object -ComObject Excel.Application
$ExcelApp.Visible = $false
$ExcelApp.DisplayAlerts = $false
$Workbook = $ExcelApp.Workbooks.Open("c:\test\ref_test.xlsx", 3, $false, 5, $null, $null, $true)
Start-Sleep -s 30
$Workbook.RefreshAll()
$Workbook|Get-Member *Save*
$Workbook.Save()
$ExcelApp.Quit()
Any ideas?
Office version: 2010, on Windows 7
Possibly the answer on this question can help. The perl script is also available as a pre-compiled exe file.
I would approach this issue by using Excel VBA, and create your Excel file into a .xlsm.
Then update the file w/ Excel VBA commands and functions to refresh your odbc connection, and then save as a new file for distribution.
http://www.vbforums.com/showthread.php?675977-Auto-Open-Refresh-Pivots-Save-Close-Excel-files-using-VB

VBA Word 2010 Paste Error / No Wait Command

I have a project that takes several documents as inputs, does some processing on them, and creates several new documents at the end. I am currently running into problems with pasting content from one Word document into another. The following code snippet seemed relevant:
Set refOrigin = FindReference(OriginDoc)
Set refDest = PasteDoc.Range(PasteDoc.Content.Start, PasteDoc.Content.End)
refDest.Collapse wdCollapseEnd
refOrigin.Copy
refDest.Paste
When running this code, I will occasionally get Run-time error 4198, Command Failed at the paste line in the code. However, when I go into the debugger, I can see that both refDest and refOrigin are valid ranges. Furthermore, when I step through the code line-by-line, it works. However, I can tell that in the instance where it failed, it inserted an embedded Word document already.
I've done some research on the issues and I believe that there is some type of problem of the code running to fast for the clipboard to keep up with it sometimes. This makes sense to me because when I run the macro from a document on a network drive, it runs without a hitch.
I thought that I would be able to simply add a wait command with Application.Wait, but it turns out that Word 2010 doesn't support this command; it's only in Excel.
Does anyone have ideas as to the root of this problem, possible solutions, or any way to give Word 2010 a wait command? Thanks.
For completeness, the following code mimics the Excel `Application.Wait' method from this question.
Dim tmpStart
tmpStart = Timer
Do
DoEvents
Loop While (tmpStart + 1) > Timer

Access Word 'Save As' dialog box with PowerShell script

I've got a PowerShell script (running on Windows Server 2008 R2 Enterprise) that opens a Word doc in Word 2010, performs a SaveAs, and saves the doc as a PDF. In brief my code looks similar to the below:
$word = new-object -ComObject "word.application"
$word.Visible = $true
$doc = $word.documents.open("path\file.doc")
$doc.SaveAs("path\file.pdf", [ref] 17)
$doc.Close()
ps winword | kill
The above works fine, no problems at all and is converting the documents as expected.
My question is:
If I physically open Word myself and navigate to 'File > Save As' I get various options in the dialog when saving as PDF (eg. page range, optimisation etc)
How can I, if at all, access these options from within the PowerShell script when performing the same action?
Any advice would be appreciated. Maybe it's just not possible.
Thanks in advance
After much investigation I've found that option I needed was ExportAsFixedFormat().
The documentation can be found here:
http://msdn.microsoft.com/en-us/library/bb256835%28v=office.12%29.aspx
And you can see it in action within a PowerShell script here:
http://blog.coolorange.com/2012/04/20/export-word-to-pdf-using-powershell/

Need skeleton code to call Excel VBA from PythonWin

I need to invoke a VBA macro within an Excel workbook from a python script. Someone else has provided the Excel workbook with the macro. The macro grabs updated values from an external database, and performs some fairly complex massaging of the data. I need the results from this massaging, and I don't really want to duplicate this in my Python script, if I can avoid it. So, it would be great if I could just invoke the macro from my script, and grab the massaged results.
Everything I know about COM I learned from "Python Programming on Win32". Good book, but not enough for my task at hand. I searched, but haven't found any good examples on how to do this. Does anyone have any good examples, or perhaps some skeleton code of how to address/invoke the VBA macro? A general reference (book, web link, etc) on the Excel COM interfaces would also help here. Thanks.
OK, I got it! Thanks for the help on the Application.Run method. This info, plus the "Microsoft Excel Visual Basic Reference": http://msdn.microsoft.com/en-us/library/aa209782(office.10).aspx--as recommended by Hammond & Robinson in "Python Programming on Win32"--was what was needed.
Here's the skeleton code:
import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="<your Excel File>",ReadOnly=1)
xl.Application.Run("<your macro name>")
#...access spreadsheet data...
xl.Workbooks(1).Close(SaveChanges=0)
xl.Application.Quit()
xl=0
I am sorry, I dont know python enough.
However, the following should help.
Excel's Application object has a Run method - which takes the name of the macro alongwith arguments to it.
Lets assume that the workbook has a macro named test.
Sub test(ByVal i As Integer)
MsgBox "hello world " & i
End Sub
You can call this using Application.Run "test", 1234
This will call the macro and show the messagebox with "hello world 1234".