I have inherited some code which uses three global variables
Global PPTApp As PowerPoint.Application
Global PPTPres As PowerPoint.Presentation
Global PPtSlides As PowerPoint.Slide
Later on in the code it uses them in the following way
Sub PasteTablesPPT(TargetText As String, PPTRange As Range)
Dim TargetSlide As PowerPoint.Slide
PPTApp.Activate
For Each PPtSlides In PPTPres.Slides 'Error on colleagues PC
With PPtSlides.Shapes.Title.TextFrame
If .HasText Then
If UCase(.TextRange.Text) = UCase(TargetText) Then
TargetNum = CInt(PPtSlides.SlideIndex)
Exit For
End If
End If
End With
Next
On my PC this works as it should i.e. it activates the open powerpoint application and then loops through each of the slides within that presentation.
However on my colleagues PC, the runs into an error on the line I have flagged. The specific error is Error 451 and I think it's to do with PPtSlides not being recognized as part if PPtPres.Slides. Also in debug mode when I hover over PPtSlides it says ="Nothing".
We have the same references check in VBA tools, could anyone shed some light on why this would work on my PC and not my colleagues?
EDIT:
The part where PPTPres is defined (in another sub and this is just an extract of that sub)
On Error GoTo ErrHandler
Set PPTApp = GetObject(class:="PowerPoint.Application")
PPTApp.Visible = msoTrue
Set PPTPres = PPTApp.Presentations("Testing File")
Exit Sub
In the sub PasteTablesPPT, try to declare PPtSlides as PowerPoint.Slide
dim PPtSlides as PowerPoint.Slide
Related
#james wrote a while back in response to a similar question that the proper way to fo this is:
Sub CreatePres()
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppSlide As PowerPoint.Slide
Dim ppTextbox As PowerPoint.Shape
Set ppApp = New PowerPoint.Application
ppApp.Visible = True
ppApp.Activate
Set ppPres = ppApp.Presentations.Add
slidesCount = ppPres.Slides.Count
Set ppSlide = ppPres.Slides.Add(slidesCount + 1, ppLayoutTitle)
ppSlide.Shapes(1).TextFrame.TextRange.Text = "Hello world"
ppSlide.Shapes(2).TextFrame.TextRange.Text = Date
slidesCount = ActiveWindow.Selection.SlideRange.SlideIndex
Call slide2(slidesCount)
End Sub
Sub slide2(i As Integer)
Set ppSlide = ppPres.Slides.Add(i + 1, ppLayoutTitle)
ppSlide.Select
ppSlide.Shapes(1).TextFrame.TextRange.Text = "Hello world"
ppSlide.Shapes(2).TextFrame.TextRange.Text = Date
End Sub
I am however getting a "By ref mismatch Argumnenr error on the 'Call Slide2(slidecount)'-- it's the las line of the first sub.
I'm using Office 365 on Windows 10 Pro.
Thanks in advance
You didn't DIM your slidesCount variable, so VBA creates a variant when you assign a value to it. Your Sub slide2 expects an integer, so it throws an error.
To save further trouble like this, ALWAYS put Option Explicit at the top of every module. That'll prevent undeclared variables from causing problems like this.
Go to Tools | Options and put a check next to Require Variable Declaration to have VBA automatically insert Option Explicit for you.
It's also a good idea to use the correct variable type. Any .Count value in the PPT object model will be a Long, not an integer. VBA will generally convert between the two when it figures it needs to. Usually it's right. Sometimes it's not. Then it all hits the fan.
The Powerpoint-Presentation is running on two devices at the same time. So one of the presenations is in read-only mode.
I would like to have a macro, which updates the presentation in read-only-mode, so the changes on the presentation (write-mode) are applied.
The macro will be started manually by a button.
I already tried to write a macro that restarts the presentation, but wasn't successfull.
Sub Prog()
Dim DestinationPPT As String
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Set PowerPointApp = CreateObject("PowerPoint.Application")
DestinationPPT = "xxx.ppsm"
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
myPresentation.SlideShowSettings.Run
Application.Quit
End Sub
Seems like the command Application.Quit is closing both presentations that are opened.
You need to close a certain file and not the whole application.
Dim PPTFile As Object
Set PPTFile = CreateObject("PowerPoint.Application").Presentations.Open(DestinationPPT)
PPTFile.Close
Use File.Close instead of Application.Quit
I hope this helped.
I am trying to write a code to open a PPTX file from excel VBA and update links in ppt.
below is the code that i have got but while the code is trying to update links i am getting the
Run time error 438 Object does not support this property or method
Sub kunal()
Dim PPObj As Object
Set PPObj = CreateObject("PowerPoint.application")
With PPObj
.Presentations.Add
.Presentations.Open Filename:="Y:\Desktop\Month End\One_Shot\Template AVP Report Package\ABD-OME SDeeson.pptx"
.Visible = True
.UpdateLinks
.Presentation.Save
.Quit
Set PPObj = Nothing
End With
End Sub
438 means that you are trying to access a property of a method or object, that does not exist. Thus, you need a presentation object and not application object to update the links.
Try like this:
Option Explicit
Sub kunal()
Dim PPObj As Object
Dim pptPresentation As Object
Set PPObj = CreateObject("PowerPoint.application")
Set pptPresentation = PPObj.presentations.Open("C:\test.pptx")
With PPObj
.presentations.Add
.Visible = True
pptPresentation.UpdateLinks
pptPresentation.Save
pptPresentation.Close
.Quit
'Set PPObj = Nothing - No need for this
End With
End Sub
Application Object MSDN
Presentation Object MSDN
First, not sure why you need .Presentations.Add if at the following line you are opening an existing Presentation.
Second, the line of .UpdateLinks is a property of the Presentation, not the PowerPoint.Application.
Code
Option Explicit
Sub kunal()
Dim ppApp As Object
Dim ppPres As Object
Set ppApp = CreateObject("PowerPoint.application")
With ppApp
.Visible = True
' .Presentations.Add ' <-- not sure why you need to open a new Presentation ?
Set ppPres = .Presentations.Open(Filename:="Y:\Desktop\Month End\One_Shot\Template AVP Report Package\ABD-OME SDeeson.pptx")
ppPres.UpdateLinks
ppPres.Save
ppPres.Close
.Quit
End With
Set ppPres = Nothing
Set ppApp = Nothing
End Sub
I need to run a PowerPoint sub from R via:
shell(shQuote(normalizePath("C:/.../VBA_Script.vbs"))).
The script VBA_Script should trigger a sub called request_bank, which should open amsgboxwith the value of the variablebank(=J. P. Morgan`).
I get the error:
Application.Run: Invalid request. Sub or function not defined, Code: 80048240, MS PowerPoint 2013.
I just tried all the different Run.-Paths mentioned in this thread Run PowerPoint Sub from Excel. I still get the error. I wonder why the same code is working if I run the same Sub in Excel or if I add the rows:
Dim PSlide
Set PSlide = PPres.Slides(1).Duplicate
But that's no clean solution for me. There must be a better way.
VBS-Script:
Option Explicit
CallPMacro
Sub CallPMacro()
Dim PApp
Dim PPres
'Dim PSlide
Set PApp = CreateObject("PowerPoint.Application")
Set PPres = PApp.Presentations.Open("C:\...\test.pptm", 0, True)
'Set PSlide = PPres.Slides(1).Duplicate
PApp.Visible = True
PApp.Run "request_bank"
PApp.Quit
Set PPres = Nothing
Set PApp = Nothing
End Sub
VBA-Code from the Sub request_bank in the test.pptm:
Sub request_bank()
Dim bank As String
bank = "J.P. Morgan"
MsgBox ("bank: " & bank)
End Sub
Any idea how to fix it?
I am currently experiencing a strange error. We have developed a tool that is used by many people and ONE of them has problems after he got a new computer.
The macro opens a PPT file located on the network (the user has access to the presentation - I tested this).
Here is the code:
Dim ppapp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim MyPath
MyPath = Workbooks("MyTool.xls").Sheets("Update").Range("start")
Set ppapp = New PowerPoint.Application
ppapp.WindowState = ppWindowMinimized
ppapp.Visible = True
Set PPPres = ppapp.Presentations.Open(MyPath, msoTrue, msoTrue)
The macro fails at this line:
Set PPPres = ppapp.Presentations.Open(MyPath, msoTrue, msoTrue)
Run-time error -2147467259 (80004005): PowerPoint could not open the file
The strange thing is that it works for all users except one.
The platform is Win7 and Excel 2010.
Any help is much appreciated!
Disclaimer on my answer with a limited knowledge of programming and VBA. My only experience is through excel and word.
Is it a problem with the office excel reference library is it? It may be better to make the code late bind rather than early bind if you've got the program go to different systems.
Dim the Powerpoint application and presentation as objects and change references to their numerical values.
Dim ppapp As Object
Dim PPPres As Object
Dim MyPath
MyPath = Workbooks("MyTool.xls").Sheets("Update").Range("start")
Set ppapp = New PowerPoint.Application
ppapp.WindowState = 2 'this would have to be changed to the numerical code; ppWindowMinimized = 2
ppapp.Visible = True
Set PPPres = ppapp.Presentations.Open(MyPath, -1, -1) 'these would also may have to be changed, not sure though - -1 = msoTrue.