Dion-NZ
04-14-2016, 06:55 PM
Hi all,
I've tried searching around to figure out why I'm getting a certain error after running my script (even though it actually does what I want it to do!) but can't find out what I've done wrong...
Full disclosure: I'm two days new to VB macros!
What I want is a .vbs script to run that opens my PowerPoint file (relative directory), runs the PowerPoint macro (which updates object links, shows a message box when completed then saves and closes the PowerPoint)...
However, even though my .VBS script does the function I want, it gives up an error that I don't understand at the end:
Application.Run : Invalid request. Sub or function not defined.
Code: 80048240
Source: Microsoft PowerPoint 2016
I've read about other users getting this error but their solutions don't seem to work for me, so something else I've done must be affecting it!
Here's the .VBS script:
Set WshShell = CreateObject("WScript.Shell")
strCurDir = WshShell.CurrentDirectory
Set oApp = CreateObject("Powerpoint.Application")
Set oPres = oApp.Presentations.Open(strCurDir & "\macrotestppt.pptm")
Set oSlide = oPres.Slides(1).Duplicate
oApp.Run "macrotestppt.pptm!UpdateLinks2"
And here is the macro I'm running inside PowerPoint:
Sub UpdateLinks2()
Dim ExcelFile
Dim exl As Object
Set exl = CreateObject("Excel.Application")
ExcelFile = (ActivePresentation.Path & "\test.xlsx")
Dim i As Integer
Dim k As Integer
'Go through every slide
For i = 1 To ActivePresentation.Slides.Count
With ActivePresentation.Slides(i)
'Go through every shape on every slide
For k = 1 To .Shapes.Count
'Turn of error checking s that it doesn 't crash if the current shape doesn't already have a link
On Error Resume Next
'Set the source to be the same as teh file chosen in the opening dialog box
.Shapes(k).LinkFormat.SourceFullName = ExcelFile
If .Shapes(k).LinkFormat.SourceFullName = ExcelFile Then
'If the change was successful then also set it to update automatically
.Shapes(k).LinkFormat.AutoUpdate = ppUpdateOptionAutomatic 'other option is ppUpdateOptionManual
End If
On Error GoTo 0
Next k
End With
Next i
'UPDATE LINKS COMPLETE
'--------------------------------------------------------------
Application.DisplayAlerts = False
ActivePresentation.Save
Application.DisplayAlerts = True
Application.Quit
End Sub
'UPDATE LINKS COMPLETE
'--------------------------------------------------------------
Application.DisplayAlerts = False
ActivePresentation.Save
Application.DisplayAlerts = True
Application.Quit
End Sub
As I said, the script does actually work but it still gives a weird error at the end that I want to fix. I've found that removing the bit in the PPT macro that saves and closes PowerPoint seems to stop the error from showing up, so maybe it's something to do with my VBS script running a macro that closes the application?
Can someone please help..??
Thanks,
Dion
I've tried searching around to figure out why I'm getting a certain error after running my script (even though it actually does what I want it to do!) but can't find out what I've done wrong...
Full disclosure: I'm two days new to VB macros!
What I want is a .vbs script to run that opens my PowerPoint file (relative directory), runs the PowerPoint macro (which updates object links, shows a message box when completed then saves and closes the PowerPoint)...
However, even though my .VBS script does the function I want, it gives up an error that I don't understand at the end:
Application.Run : Invalid request. Sub or function not defined.
Code: 80048240
Source: Microsoft PowerPoint 2016
I've read about other users getting this error but their solutions don't seem to work for me, so something else I've done must be affecting it!
Here's the .VBS script:
Set WshShell = CreateObject("WScript.Shell")
strCurDir = WshShell.CurrentDirectory
Set oApp = CreateObject("Powerpoint.Application")
Set oPres = oApp.Presentations.Open(strCurDir & "\macrotestppt.pptm")
Set oSlide = oPres.Slides(1).Duplicate
oApp.Run "macrotestppt.pptm!UpdateLinks2"
And here is the macro I'm running inside PowerPoint:
Sub UpdateLinks2()
Dim ExcelFile
Dim exl As Object
Set exl = CreateObject("Excel.Application")
ExcelFile = (ActivePresentation.Path & "\test.xlsx")
Dim i As Integer
Dim k As Integer
'Go through every slide
For i = 1 To ActivePresentation.Slides.Count
With ActivePresentation.Slides(i)
'Go through every shape on every slide
For k = 1 To .Shapes.Count
'Turn of error checking s that it doesn 't crash if the current shape doesn't already have a link
On Error Resume Next
'Set the source to be the same as teh file chosen in the opening dialog box
.Shapes(k).LinkFormat.SourceFullName = ExcelFile
If .Shapes(k).LinkFormat.SourceFullName = ExcelFile Then
'If the change was successful then also set it to update automatically
.Shapes(k).LinkFormat.AutoUpdate = ppUpdateOptionAutomatic 'other option is ppUpdateOptionManual
End If
On Error GoTo 0
Next k
End With
Next i
'UPDATE LINKS COMPLETE
'--------------------------------------------------------------
Application.DisplayAlerts = False
ActivePresentation.Save
Application.DisplayAlerts = True
Application.Quit
End Sub
'UPDATE LINKS COMPLETE
'--------------------------------------------------------------
Application.DisplayAlerts = False
ActivePresentation.Save
Application.DisplayAlerts = True
Application.Quit
End Sub
As I said, the script does actually work but it still gives a weird error at the end that I want to fix. I've found that removing the bit in the PPT macro that saves and closes PowerPoint seems to stop the error from showing up, so maybe it's something to do with my VBS script running a macro that closes the application?
Can someone please help..??
Thanks,
Dion