I have tried to use a code snippet from a previous question on Stack Overflow just starting with opening an existing PowerPoint file from Excel using VBA. The example code snippet will not work for me.
I am using Excel 2016, and the reference object mode is Microsoft Object Model 16 Library, not the Model 12 referred to earlier. Maybe this is the source of the problem. The procedure I wrote, modified from this site was
Public Sub OpenPPT()
Dim DestinationPPT As String
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
'Easier to define manually set links up front so it's easier to change/modify
DestinationPPT = "D:\Downloads\Automate_Excel\IR_Seeker_UpdateRevA.pptx"
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
End Sub
The routine gags on the Set myPresentation line
Try adding this line, so that you open the PowerPoint Application, before you try to open the Presentation in it:
Set PowerPointApp = New PowerPoint.Application
As an analogy: you need to make sure that you are looking at a building before you try to open the door...
You should change
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
To
Set myPresentation = PowerPointApp.myPresentation.Open(DestinationPPT)
I would also add "Option explicit" outside of all the subs so it will yell at you if you have a undefined variable as well as correct some variable typos.
Related
Helo all,
I'm trying to automate the reporting system at my work. To this end I'm setting up a Macro that, at the press of a button in Excel, will:
Open a new presentation from a given template (.potx) in a given path
Add various charts and tables
Save the newly created presentation in another given path
As this is pretty new to me I'm moving step by step, but I'm stuck at the beginning. This is my code so far:
Dim PP As PowerPoint.Application
Dim report As PowerPoint.Presentation
Dim report_activeslide As PowerPoint.Slide
'Dim Slide_1_text As Shape
Dim path_template As String
Dim path_report As String
path_template = "path_template.potx"
path_report = "path_report"
Set PP = New PowerPoint.Application
Set report = PP.Presentations.Open(path_template, False, True, True)
PP.Visible = msoTrue
'Set report_activeslide = report.Slides(1)
report.SaveAs path_report, ppSaveAsOpenXMLPresentation, msoTrue
End Sub
As of now I'm able to open the presentation from the template and correctly save it. However, the moment I actually start doing anything on the presentation itself, for example taking the comment off the
'Set report_activeslide = report.Slides(1) line, Excel hard crashes.
Does anyone know where the problem is?
I'm running Office 365 on Mac if that may be of any difference.
As Ike in the comments pointed out, the code works on Windows (I had the chance to test it myself). This pointed me to the possibility of my problem being due to Mac, not to code and, as it turns out, this is correct. Indeed there are more than a few people reporting the same issue (see for example: Excel VBA crashing when referencing a PowerPoint slide index number)
So for now, until Microsoft provides a better implementation of OLE there is nothing I can realistically do to solve this.
Attempted to use early binding to assign a powerpoint presentation to a variable so i can call it again to dig deeper into the slides and then shapes to copy textboxes into Excel
but pptApp.Presentations.Open("Presentation1.pptx") keeps getting highlighted for "compile error: Invalid or unqualified reference"
Sub test()
Dim pptApp As Application
Set pptApp = New PowerPoint.Application
pptApp.Visible = True
pptApp .Presentations.Open("Presentation1.pptx")
End Sub
I am familiar with working my way around Excel's object library but powerpoint is still quite new to me and I can't find much information online regarding copying items from powerpoint into excel as usually its done the other way round, would appreciate any additional advice on the useful codes in Powerpoint object library (this is a personal project to automate a mundane task at work)
A couple of years ago I posted a question about an issue I was having with Excel and Powerpoint VBA on Mac OS X. I never managed to solve the problem, and instead worked around it by installing bootcamp and running it on Windows 10. However, I now have exactly the same problem again, I'd rather not use Windows, and the issue doesn't seem to have resolved itself through updates (I'm now using 365, having started with Office 2016) so I'm back here asking for help.
I have some VBA code that starts a Powerpoint session from Excel and manipulates PowerPoint using content from the Excel workbook. The problem comes whenever I try to reference a specific slide. No matter how I do it, Excel crashes. The crash always occurs at the point of making the reference to the slide index. Here's some simplified code that will trigger the crash:
Sub test()
Dim myApp As PowerPoint.Application
Dim myPres As PowerPoint.Presentation
Dim mySlide As PowerPoint.Slide
Set myApp = CreateObject("PowerPoint.Application")
Set myPres = myApp.Presentations.Add
Set mySlide = myPres.Slides.Add(Index:=1, Layout:=ppLayoutTitleOnly)
Set mySlide = myPres.Slides.Add(Index:=2, Layout:=ppLayoutTitleOnly)
Set mySlide = myPres.Slides(1)
End Sub
That last line before the end sub will cause the crash. This doesn't happen running on Windows. What on earth is going on?
It's incredibly frustrating. I've wasted tons of time trying to fix this, or work around it. I've seen one or two people asking similar questions elsewhere, but never seen a solution to it.
Grateful for any assistance
Thanks
I am trying to create a VBA program to take screen shots from excel to paste them in a PPT template. I have been able to have success with Windows, but I am struggling to get this to work on MAC. I know that there are issues getting Office family products to talk to each other on Office for Mac, but I'm hoping that some of the older problems have been fixed. The questions about this topic, for the most part, are a few years old.
I am able to use the code below to open a ppt template from a directory, but as soon as I try to select a specific slide, it crashes excel. This is the most comprehensive dialog about this topic, but its from around 3 years ago. https://answers.microsoft.com/en-us/msoffice/forum/all/vba-copy-from-mac-excel-2016-and-paste-to/7d177c40-501b-471f-b4b4-81735652d492
Am I missing something stupid? Is there a workaround? Half of my organization uses PC and half Mac, so just using Windows isn't the solution.
Sub CreateDealReviewPPT()
'Declare PPT Variables
Dim PPTApp As PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim PPTSlide As PowerPoint.Slide
Dim PPTShape As PowerPoint.Shape
'Ranges for the directories
PPTTemplate = Sheets("Start Here - ISSP Instructions").Range("PPTTemplate").Value
Set PPTApp = CreateObject("PowerPoint.Application")
Set PPTPres = PPTApp.Presentations.Open(PPTTemplate)
PPTApp.Visible = True
PPTApp.Activate
PPTSlide = PPTPres.Slides(5)
PPTSlide.Select
When I get to the PPTSlide = PPTPres.Slides(5) it crashes.
I am on Mac OS Catalina. I am using Excel and PPT versions 16.37 on office365.
Office for Mac has a long-standing known bug with CreateObject. It just doesn't work.
In addition, the OLE implementation in Office for Mac is limited in PowerPoint. You can call Excel and Word objects from PowerPoint, but you can't call PowerPoint from either Word or Excel.
On top of that, the post-2011 PowerPoint VBA object model is full of holes: commands and parameters that work fine in Windows fail on the Mac. Sorry, but it's a mess.
I want to reference different workbooks in my code and I have used this code:
Dim Basicwb As Excel.Workbook
Dim Basic As Excel.Application
Set Basic = New Excel.Application
Set Basicwb = Basic.Workbooks.Open("X:\Job\Masroori\3-042-PMS.xlsx")
but the problem is how can I refrence it if I dont want to open it each time. I used this code (without .Open) but I get this Error! : "Subscript out of range"
Set Basicwb = Basic.Workbooks("X:\Job\Masroori\3-042-PMS.xlsx")
Also, I dont want to activate the workbook each time, Is there any way?
Taken from the msdn site for the Workbooks property:
"Returns a Workbooks collection that represents all the open workbooks. Read-only."
hence the last line of your code gives you an error since the file is not open. AFAIK, you cannot reference objects within a workbook if that workbook is not open. You can access whatever a workbook has without activating it (so without using .Activate), but it has to be open. Maybe this SO question is of help to you:
Open Excel file for reading with VBA without display
If your workbook is open, you can do the following:
Dim wBook as Excel.Workbook, rngTemp as range
Set wBook = workbooks("wbName.xls")
With wBook
' Do stuff, no need to activate. Example:
set rngTemp=.sheets(1).usedRange
End With
I hope this helps?