VBA fails when opening PowerPoint presentation on Windows 7 - excel

I wrote a VBA macro in Excel 2007 on Windows XP to copy data from an excel spreadsheet into a powerpoint presentation.
When this macro enabled spreadsheet was run on a fresh install of Windows 7 it fails.
So I pulled out the code that fails to pin point the problem and it seems to fail when trying to open an existing powerpoint file. I have tried running this code in both Office 2010 and Office 2007.
The code I am trying to use it (just the problem parts shown below)
Sub test()
Dim PowerPointApplication As PowerPoint.Application
Dim PowerPointFile As PowerPoint.Presentation
Set PowerPointApplication = CreateObject("PowerPoint.Application")
Set PowerPointFile = PowerPointApplication.Presentations.Open("PATH_TO_FILE\test.pptx")
End Sub
The macro fails on the Presentations.Open line above with the following error
Run-time error '-2147467259 (80004005)':
Method 'Open' of object 'Presentations' failed
I have already enabled the PowerPoint 12.0 Object Library in the references settings in the VBEditor for the spreadsheet. All the other references match exactly with the file that runs without error on my Windows XP box.
I have looked all over the web for an answer and cant find anything. I read something about Windows 7 and offline files, so tried turning that off but it didnt help.
I am logged in as an administrator user as well, and tried moving the pptx that I am opening to other directories as well with no success.
I am running the following version of Windows:
Windows 7 Professional
Service Pack 1
64 Bit
Any help would be appreciated!

Is PATH_TO_FILE a variable (or constant)??
If so shouldn't it be
PowerPointFile = PowerPointApplication.Presentations.Open(PATH_TO_FILE & "\test.pptx")

This does work in office 2016 on Win7 SP1 64bit OS
Should be good for Office 2010.
Could be John's advise on the path to file, also.
Sub test()
Dim PowerPointFile As PowerPoint.Presentation
Dim PPTObj As Object
Set PPTObj = CreateObject("PowerPoint.application")
Set PowerPointFile = PPTObj.Presentations.Open("C:\test.pptx")
End Sub

Related

Excel crashes when creating/generating a powerpoint on Mac

I am trying to generate a Powerpoint from Excel with VBA. On Windows everything is working fine. I have very very simple code atm:
Sub pptTest()
Dim PowPntApp As PowerPoint.Application
Dim PowPntPrsnt As PowerPoint.Presentation
Dim PowPntSlide As PowerPoint.Slide
Set PowPntApp = New PowerPoint.Application
PowPntApp.Visible = True
PowPntApp.Activate
Set PowPntPrsnt = PowPntApp.Presentations.Add
Set PowPntSlide = _
PowPntPrsnt.Slides.Add(1, ppLayoutTitle)
End Sub
When I run this macro on my Macbook, it does open powerpoint with a new slide, but it completely crashes Excel to the point I have to force quit the application. Why is that?
Excel version: 16.61.1 (22052000)
Macbook: MacBook Pro (16-inch, 2021) M1 Chip (no clue if this is relevant)
EDIT
I've ran the code a couple of times. Sometimes it works, sometimes it only crashes Excel, sometimes it only crashes Powerpoint and sometimes it crashes both Excel AND Powerpoint. Very inconsistent. Any thoughts?

Slide Selection in PPT on Mac OS

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.

"ActiveX component can't create object" Error in Excel

When I run the code below, I get the error in my title at the line indicated. I've looked around StackOverflow and elsewhere and can't find any help that works. Any pointers?
This code was working fine 2 days ago in both Windows 10 and Mac OSX, and then I had another (non-VBA) problem and the Microsoft Tech support representative reinstalled Office 365 to try to fix it. Now, I get this same Excel dialog: "ActiveX component can't create object" whenever I try to run this simple sub, both on Windows 10 and Mac OSX.
Sub changeSheetsZoomLevel()
Dim n As Integer, sh As Worksheet
n = 0
For Each sh In ThisWorkbook.Worksheets ' **<-- error occurs here**
With sh
.Activate
ActiveWindow.Zoom = 90
.Columns("A:A").ColumnWidth = 100
.Columns("B:B").ColumnWidth = 70
.Cells.EntireRow.AutoFit
n = n + 1
End With
Next sh
MsgBox n & " sheets reformatted"
End Sub
I'm only showing this one subroutine, but there are many others, including some that manipulate Word documents.
When I click the "Help" button in these dialogs, the next dialog that appears is different on the 2 operating systems, as shown below.
I even tried Microsoft Support again, and they had me install the 64-bit of Office into Windows instead of the 32-bit version. But that didn't help.
Although I looked into the solution suggested in the comment by #MBB70, I found that was not the problem. Instead, here is the solution that worked for me:
1) Make a new Excel workbook and
2) Transfer all vba code into it from my original workbook
3) Transfer all sheets into it from my original workbook
4) Add the needed reference in the VBA Editor using Tools-->References... as shown below:
The new workbook now works properly in both Mac OSX and Windows 10.

Excel.Application.Visibile not working on Office 2016

I need help reguarding the following code written in Visual Basic 6.0.
Private Sub cmdExcel_Click()
Dim obj As Object
Set obj = CreateObject("Excel.Application")
On Error Resume Next
MkDir "c:\temp"
On Error GoTo 0
MousePointer = vbHourglass
On Error GoTo err
objGrid.m.ExportToXLS "c:\temp\test.xls"
obj.Workbooks.Open "c:\temp\test.xls"
obj.Visible = True
obj.Interactive = True
On Error GoTo 0
err:
MousePointer = vbNormal
End Sub
The enviroment: Windows 10, Office Excel 2016.
The problem: the workbook doesn't show up, but I can still find an Excel instance in Task Manager. If I shut down the instance I can find the Excel in c:\temp and the application starts working again.
The goal: show the Excel just created.
Do you have any solutions?
Thanks in advance.
Sara
I found the solution.
The problem wasn't in the code but in some Excel's settings.
First, I modified the code in order to get a more detailed error. Basically I switched the two instructions which allow Excel App to be visibile and the file to be opened:
obj.Visible = True
obj.Workbooks.Open "c:\temp\test.xls"
In this way, I was able to open Excel App and read the reason why the file could not be opened.
As you can see the file type is Excel 4 Worksheets which is blocked on opening.
I had to go Trust Center and disable it.
Just to be sure, I disabled also Excel 4 Workbook.
After performing the steps above, my app worked.

Documents.Open gives 'Locked for editing' error while document is closed

When using the Documents.Open("XX") command in excel. My excel prompts the window where it says that the document is lock for editing, by ME. Which isn't the case.
This command does seem to work when i'm using microsoft word/excel 2010 at work. But at home, using office 2016, it doesn't. This goes for my personal laptop with office 2016 aswell.
The following code is used:
Sub test()
Dim wordDoc As Word.Application
Dim testDoc As Word.Application
Set wordDoc = CreateObject("Word.Application")
Set testDoc = wordDoc.Documents.Open("C:\Users\Me\Desktop\Test.docm")
' Do stuff
End Sub
My thoughts:
Office 2016 seems to open a 'Shadow file' which gives the document 'Open' status. But when restarting my PC the code simply does NOT work. Giving an 'Filename' error, upon restarting the code the 'Locked for editing' error appears.
My question:
Am i using the wrong approach on opening a document in Office 2016? If so, what is the right approach? If not, are there settings that need to be changed in order for this to work?
Thanks in advance,
I've had this happen before, the issue for me was that a previous run of the macro wasn't able to close the file properly. Give this thread a read:
Application.Quit command not closing the entire Excel Application
You could test to see if this is the case by "End Task"ing all MS Word instances and running your macro again, If it's able to run the first time, then this is your issue.
Also, This is the code I used for testing. Dim'ing the variables as "Word.Application" didn't work for me, so if my earlier suggestion doesn't work then try just "Object" instead:
Sub openWord()
Dim wrd As Object
Dim doc As Object
Set wrd = CreateObject("Word.Application")
wrd.Visible = True
Set doc = wrd.Documents.Open("C:\Users\name\Desktop\doc.docx")
doc.Close
wrd.Quit
End Sub

Resources