#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.
Related
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 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
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.
I'm pretty rusty with VBA and I was hoping someone could point me in the right direction, or help me out. What I'm trying to do is run a macro that will cycle through each active container on my sheet, and then count the number of shapes within that container. Then I'd like to update a field in the shape data for the container with the number of shapes.
This is what I have so far:
Public Sub countContainers()
Dim vsoPage As Visio.Page
Dim vsoDocument As Visio.Document
Dim vsoDocuments As Visio.Documents
Dim vsoPages As Visio.Pages
Dim vsoContainerShape As Visio.Shape
Dim containerId As Variant
For Each containerId In vsoPage.GetContainers(visContainerIncludeNested)
Set vsoContainerShape = vsoPage.Shapes.ItemFromID(containerId)
Debug.Print vsoContainerShape.NameU
Next
End Sub
The error I get is Object Variable or With Block variable not set
Any ideas?
The error is because you have declared the vsoPage but you never assigned it to any page in your document.
Add a line such as this to initialize it and the error goes away:
Set vsoPage = ActivePage
I am really struggling with this, I don't seem to be able to find the reason for my code not working:
Dim i1 As Integer
Dim PPapp As Object, XLapp As Object
Dim slide1 As Slide, slide2 As Slide, slide3 As Slide
Dim PPoutput As Presentation
Dim output_table As ShapeRange
Set PPapp = New PowerPoint.Application
Set XLapp = Excel.Application
Set PPoutput = PPapp.Presentations.Open("Q:\SDPMaler\blank.potx", untitled:=msoTrue, withwindow:=msoTrue)
Set slide1 = ppoutput.Slides.AddSlide(1, ppoutput.SlideMaster.CustomLayouts(13))
XLapp.ActiveWorkbook.Sheets("PPT output").Range("y4:ae11").Copy
Set output_table = slide1.Shapes.PasteSpecial(ppPasteJPG, msoFalse, "", 1, "", msoTrue)
And here my code fails: The macro creates the powerpoint, adds the slide in the right layout and even pasts the table as picture
.PasteSpecial(DataType:=ppPasteOLEObject, link:=msoTrue)
works the same way, but with either I get "Run-time error '13': Type mismatch" on the last row of the pasted code. Even though #13 is usually an easy error to find I am really stuck this time.
Hope someone can help me
Thanks
P.S.: II am using Office 2010 and am running the macro in Excel
My guess:
Change this:
Dim output_table As ShapeRange
To:
Dim output_table As PowerPoint.ShapeRange
assuming you've set a reference to PowerPoint, or
Dim output_table As Object
if you're using late binding
By dimming it as ShapeRange in Excel, you're creating a variable to hold an Excel shaperange, but when you paste into PPT, you get a PowerPoint shaperange, which would lead to a type mismatch.
I'm guessing that the .PasteSpecial function does not return a ShapeRange so when you try to assign the result of .PasteSpecial to variable (output_table) declared as a ShapeRange object you get a type mismatch.
Try Dim output_table as Variant and then debug it by looking at TypeName(output_table) -- if you even NEED the result of PasteSpecial.