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.
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.
I'm currently making an automatic Powerpoint from excel using vba and I would like to set a theme to the document, the "Ion" theme to be precise. I have the following code:
Sub CreateFullPres()
Dim ppt As PowerPoint.Application
Dim pres As PowerPoint.Presentation
Dim sld As PowerPoint.Slide
Dim shp2 As Shape
'Create Powerpoint
Set ppt = New PowerPoint.Application
Set pres = ppt.Presentations.Add
ppt.Visible = True
'Add Slide
Set Slide1 = pres.Slides.Add(1, ppLayoutTitle).Shapes.Placeholders
SlideTitle = Sheets("FIBO Monthly Update").Range("B4")
SubTitle1 = Sheets("FIBO Monthly Update").Range("B6")
SubTitle2 = Sheets("FIBO Monthly Update").Range("B7")
Slide1.Item(1).TextFrame.TextRange.Text = SlideTitle
Slide1.Item(2).TextFrame.TextRange.Text = SubTitle1 & ": " & SubTitle2
The code continues after this but this is all that is necessary.
Thanks in advance.
pres.ApplyTheme {full path to your thmx file}
Finding the MS-supplied themes can be tricky. It'll be simpler if you create a presentation based on the Ion theme, save it as a THMX to a convenient location, then specify that path/file.thmx in the code above.
By the way, you'll also want to use
Dim shp2 As PowerPoint.Shape
instead of
Dim shp2 As Shape
which dims it as an Excel shape rather than a PowerPoint shape; they're likely to have different properties.
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 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'm trying to apply a template to a powerpoint through excel. The powerpoint template is embedded within my excel file via insert -> Object. I've successfully used the .applytemplate method to apply a template from file, but I cannot adjust the code to reference the embedded powerpoint template. I tried using OLEObject, but I'm afraid that isn't correct. Please review below code.
Sub ppCreate()
Dim myPP As PowerPoint.Application
Dim myPres As PowerPoint.Presentation
Dim activeSlide As PowerPoint.Slide
Dim ppObj As OLEObject
' Create instance of PowerPoint
Set myPP = CreateObject("Powerpoint.Application")
' For automation to work, PowerPoint must be visible
myPP.Visible = True
' Create a presentation
Set myPres = myPP.Presentations.Add
' Set slide view to Slide Only
myPP.ActiveWindow.ViewType = ppViewSlide
'Resize to 4:3
myPres.PageSetup.SlideSize = 2
'Add a slide
Set activeSlide = myPres.Slides.Add(1, ppLayoutBlank)
'Import Template
Worksheets("CBRDATA").Select
Set ppObj = ActiveSheet.OLEObjects("ppObj") 'NOT WORKING
myPres.ApplyTemplate (ppObj) 'NOT WORKING
myPres.ApplyTemplate "C:\CBR_TEMPLATE_COVER.potx" 'WORKING
Worksheets("CBR").Select
End Sub
Update:
'Test if template exists in C:\
If Dir("C:\CBR_TEMPLATE_COVER.potx") = "" Then
'Open/Save the embedded template to C:\
Set tempPP = CreateObject("Powerpoint.Application")
Worksheets("CBRDATA").OLEObjects("ppObj").Verb 0
tempPP.Visible = True
Set tempPres = tempPP.ActivePresentation
tempPres.SaveCopyAs ("C:\CBR_TEMPLATE_COVER.potx")
tempPres.Close
Else:
End If
' Create instance of PowerPoint
Set myPP = CreateObject("Powerpoint.Application")
This doesn't work because ActiveSheet.OLEObjects("ppObj") is type OLEObject, not PowerPoint.Presentation.
Set ppObj = ActiveSheet.OLEObjects("ppObj") 'NOT WORKING
While manually double-clicking on the object open the POTX file (actually it opens a new blank PPTX using the POTX as template), your assignment statement above isn't doing any of that, it's trying instead to put an OLEObject where a Presentation is expected, and that will always fail.
So, how to "open" the OLEObject? OLEObject has a .Verb method, and the following will perform the object's default action, which in the case of embedded package objects, is usally to "open" them.
The Solution
'Import Template
'## This should Open the template
Worksheets("CBRDATA").OLEObjects("ppObj").Verb 0
'## Assign the ActivePresentation to your ppObj variable
Set ppObj = myPP.ActivePresentation
Editorializing: Embedded OLEObjects are notoriously problematic, and probably not an ideal place to story things like document templates :)