Trying to build a macro to copy textboxes from Powerpoint to Ex - excel

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)

Related

Excel hard crashes when I try to modify a PowerPoint presentation through macro

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.

Excel VBA crashing when referencing a PowerPoint slide index number

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

Unable to open an existing PowerPoint Presentation (2016) from Excel using VBA

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.

Activating an Excel Workbook created by a Visio ShapeReport [duplicate]

So basically, I have a Visio file that has lots of shapes and data. I'm trying to create a shape report into a new excel file, and then have the excel file turn the exported data into a pivot table programmatically.
I have a macro running in Visio that generates the excel file with data in normal table form already. I want to be able to run a macro in Visio that activates the excel window of exported data and runs a macro to make it into a pivot table. However, any excel macro code I put into my visio macro modules is unrecognized (e.g. "Range"), presumably because they're not words recognized by Visio.
My question is this: How do I run a macro that alters an Excel file FROM a Visio module?
I know how to call a macro IN an Excel file from Visio (Excel.run "ModuleNAME"), but that would require the macro to already be in Excel. Since I'm creating new Excel files when I get shape reports, these files don't have any macros in them.
An alternative solution would be if I was able to export shape data report from Visio as a new sheet to an EXISTING Excel file that already contained the macro, but I'm not sure how to do this either... (export shape reports from Visio to existing Excel file)
My Code to Generate Excel Report:
Sub Excel2()
Visio.Application.Addons("VisRpt").Run
("/rptDefName=ReportDefinition_2.vrd/rptOutput=EXCEL")
End Sub
I would like to run this macro after running Excel2()
Sub NewMacro()
AppActivate "Microsoft Excel"
Dim AppExcel As Excel.Application
Set AppExcel = CreateObject("Excel.Application")
'Do all sorts of fancy stuff with making pivot tables
'Do all sorts of fancy stuff with making pivot tables
End Sub
You can just use the Get/CreateObject function to use an Excel application instance from Visio.
Have a look at this article:
http://msdn.microsoft.com/en-us/library/gg251785.aspx
You might also find this useful:
http://support.microsoft.com/kb/309603
...it describes the opposite direction (ie controling Visio from another app), but in your case you'd do something like:
Dim AppExcel As Excel.Application
On Error Resume Next
Set AppExcel = GetObject(, "excel.application")
If AppExcel Is Nothing Then
Set AppExcel = CreateObject("excel.application")
End If
Hope that helps.

Paste/Copy Range of Cells from Excel to a Bookmark in Word using WORD VBA

I am looking at inserting/pasting a range of text data (40 columns) from Excel into bookmarks in Word. Most of the answers are done using Excel VBA, which is so not practical for my use case as I will have the Word document open, add a button that would run this 'import data' macro. I actually already have a button in the doc that inserts images into bookmarks, so that's one more reason I don't want to do it via Excel VBA.
I know this is not great code, but for the lack of definite leads, I'm throwing it here and hope that this gives you an idea of what I'm trying to achieve:
Sub ImportData()
Workbooks.Open ("\Book2.xlsm")
ActiveWindow.WindowState = xlMinimized
ThisWorkbook.Activate
Windows("Book2.xlsm").Activate
Range("A1:AF1").Select
Selection.Copy
Documents("test.docm").Activate
Selection.GoTo What:=wdGoToBookmark, Name:="Overlay_1"
Selection.Paste
End Sub
PS: It would be great if I could sort of 'transpose' the 40 columns into rows as it is pasted in Word.
Here's an update to my code based off #Variatus 's advice:
Sub ImportData()
Dim wb As Workbooks
Dim ws As Worksheets
Dim objSheet As Object
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
wb.Open ("C:\Users\pc\Documents\Book2.xlsm")
Set objSheet = CreateObject("Excel.Application")
ActiveWindow.WindowState = xlMinimized
Set ws = Workbooks("Book2.xlsm").Sheets("Sheet1")
ws.Range("A1").Value.Copy
With objWord.ActiveDocument
.Bookmarks("Bookmark_1").Range.Text = ws.Range("A1").Value
End With
End Sub
I'm getting this error:
Runtime Error '91':
Object variable or With block variable not set.
Notice how I stuck with a single cell reference for now (A1). I'll just update my code as I learn along the way :)
When you click the button in your Word document you want the following sequence to be initiated.
Create an Excel application object. Make sure that a reference to Excel has been set (VBE > Tools > References) so that Excel's VBA objects are available.
Using the Excel application object, open the workbook. Create an object. Place the object in an invisible window.
Definitely forget about activating or selecting anything in either the workbook or your Word document. The latter is active and remains active from beginning to end. The bookmarks are points in your document you can reference and manipulate by name without selecting them. The Excel workbook is invisible. You can access any part of it using the Range object.
The data you want from your workbook are contained in Worksheets. Be sure to create an object for the worksheet you are about to draw data from.
Excel tables don't translate very well into Word tables. If you do want to go that way I suggest that you use VBA to create the table you want in Excel (transpose the data before you import them into Word). However, you may find it easier to first create the tables you want in Word and then just copy values from your Excel source into the word tables. That would involve taking one cell value at a time and placing it into one Word table cell. Transposing would be done by the algorithm you employ.
Close the workbook. Quit the Excel application. Set the Excel application = Nothing. At the end of your macro everything is as it was before except that your document has data in it which it didn't have before.
Each of the above six points will lead you to at least one question which you can ask here after you have googled the subject and written some code. In fact, I strongly urge you to create one Main procedure (the one which responds to your button click) and let that procedure call various subs which carry out the individual tasks and functions to support the subs. The smaller the parts you create the easier it is to write the code, to find questions to ask and get answers to them. If you plan your project well expect to have about 12 procedures in it by the time you are done. Good luck!

Resources