Powerpoint VBA: Open Source of Linked Excel Chart - vba

I have a PowerPoint presentation with several Excel charts pasted as links. Using VBA, I have checked, and the type of these shapes is msoLinkedOLEObject.
Knowing this, I would like to first open the Excel file that contains the original chart through VBA and then use the "update link" command of the chart, like this:
I wish to open the workbooks first because using the "update link" command with an open Excel file is usually faster for me than just using the "update link" command directly (maybe because some PowerPoint presentations and some workbooks are really extense).
For the sld.Shapes(i).LinkFormat part of the code, I can use .UpdateLinks to directly refresh the data, but I cannot find any way to directly open the Excel source file (in the same way I can by manually clicking the linked chart).
Could this be achieved?
Sub UpdateExcelLinkedCharts()
Dim pres As Presentation
Dim sld As Slide
Dim shp As Shape
Set pres = Application.ActivePresentation
'Loop through all active slides in the presentation
For Each sld In pres.Slides
If sld.SlideShowTransition.Hidden = msoFalse Then
'If the slide is a msoLinkedOLEObject, proceed to update its link
For i = 1 To sld.Shapes.Count
If sld.Shapes(i).Type = 10 Then
sld.Shapes(i).LinkFormat.UpdateLinks
End If
Next i
End If
Next sld
MsgBox ("All Links Updated!")
End Sub

You can either use
sld.Shapes(i).OLEFormat.Activate
Or
sld.Shapes(i).OLEFormat.DoVerb
followed by sld.Shapes(i).LinkFormat.Update
Both will open the linked object so you can edit it. But notice that this is not enough to have a reference to an Excel Object that you can manipulate through VBA.

Related

Change linked excel file via VBA in PowerPoint

I have multiple slides with multiple charts having sample data. I want to update these charts with actual data which is present in excel files located in the same folder. Can I do this via VBA inside ppt and how?
I read this answer edit chart data in powerpoint but this does not specify where I can mention the excel file and select the specific data.
Please help!
On the question you linked to, the top answer provided the following code:
With ActivePresentation.Slides(sl).Shapes(sh).Chart.ChartData
.Activate
.Workbook.Sheets(1).Range("A1").Value = "test_data"
.Workbook.Close
End With
One of the comments on the answer pointed out that once you Activate the ChartData, you're basically dealing with Excel. You can also launch an actual instance of Excel inside the Powerpoint VBA, then just open your workbooks, extract the data from them and put it into your ChartData objects like you would do if you were working in VBA in excel.
In order to launch an instance of Excel in powerpoint VBA try the following:
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open "C:\lol\Book1.xlsx", True, False
'once Book1 is open you can extract data from Book1 and move it to
'the appropriate place in .ChartData.Workbook
Set xlApp = Nothing
(code above sourced from this answer)

Referencing Excel Sheet placed in PowerPoint

I have added an Excel sheet into a PowerPoint 2010 document using the Insert tab --> Object --> Microsoft Excel 97-2003 Worksheet (Create New) option. I want to reference some of the cells in the Excel sheet in another slide of my PowerPoint. Is there a way to do this?
The purpose is I have a client who insists on a PPT report, except I need to use Excel to create the information required. Rather than constantly having two documents open and transferring the info from the Excel sheet to the PPT slide, I wanted to consolidate into one document, thus the Excel sheet added into the PPT file.
I'm not an expert at VBA by any means, but I know enough to muddle my way through if I need to use VBA to accomplish this.
I've uploaded pictures of an example (I hope). On slide 1, I have three cells filled in using the inserted Excel sheet. Slide 2 is where I need to reference those cells (text boxes with text in red). The information in those cells will change week from week and I need the text boxes in slide 2 to update with it. Any help would be appreciated.
Use the Selection Pane in PowerPoint to identify the embedded object's name, and reference it, then use the OLEFormat.Object to get a handle on the Workbook object, and from there you're just working with an instance of Excel.Workbook class, so all your familiar Excel properties & methods should be available:
Option Explicit
'Requires reference to Excel
Sub populate_text_box()
Dim obj As Object
Dim tb As Shape 'TextBox
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Set obj = ActivePresentation.Slides(1).Shapes("Object 3")
Set wb = obj.OLEFormat.Object
Set ws = wb.Sheets(1)
Set tb = ActivePresentation.Slides(2).Shapes("TextBox 1") 'Modify slide/shape name as needed
tb.TextFrame2.TextRange.Text = ws.Range("B2").Value
Set tb = ActivePresentation.Slides(2).SHapes("TextBox 2") 'Modify slide/shape name as needed
tb.TextFrame2.TextRange.Text = ws.Range("D2").Value
Set tb = ActivePresentation.Slides(2).SHapes("TextBox 3") 'Modify slide/shape name as needed
tb.TextFrame2.TextRange.Text = ws.Range("F2").Value
End Sub

VBA code to auto update PowerPoint links to Word

I have hundreds of PowerPoint presentations that all have linked Word objects in them. How can I auto-update all of the links without opening each presentation? I'm assuming it'll be done with VBA, but the only VBA examples I can find are for auto-updating linked Excel objects. I'm not familiar enough with VBA to modify the code.
Ultimately, I'd like to run this via Command Prompt.
Using PowerPoint & Word 2013.
DragonSamu's right ... but to get you started, try this from within PPT itself, then work out how to rewrite it in a scripting language or something that will compile to an EXE
For each file you want to process, open the file (via code) then
Call UpdateLinks(ActivePresentation)
Sub UpdateLinks(oPres As Presentation)
Dim oSl As Slide
Dim oSh As Shape
For Each oSl In oPres.Slides
For Each oSh In oSl.Shapes
If oSh.Type = msoLinkedOLEObject Then
oSh.LinkFormat.Update
End If
Next
Next
End Sub

Updating Powerpoint embedded in excel using vba

I have a excel file in which 3 Powerpoint presentations are embedded(as object). These are the blank templates of the deck I want to prepare. How can I assign these embedded Powerpoint presentations through VBA. I know that I can keep Powerpoint presentations separately and access them but I want to make them into one file this time. Thanks in advance
OK, assuming you have a PPT presentation embedded in Excel and its name is "Object 1"
Dim oSh As Shape
Set oSh = ActiveSheet.Shapes("Object 1")
With oSh
' Uncomment the appropriate line for the result you want
' Show
'.OLEFormat.Verb (1)
' Edit (in place)
'.OLEFormat.Verb (2)
' Open
.OLEFormat.Verb (3)
End With

How to edit / ungroup EMF pastes in PPT using VBA?

it's Martin from Berlin (Germany - so please excuse my wrong English)...
I'm trying to edit EMF pastes in PPT using VBA. The EMF pastes are Excel charts and I used
ActiveWindow.Selection.SlideRange.Shapes.PasteSpecial DataType:=3 (enhanced metafile)
Without VBA it is easy: Just rightclick the image and "ungroup" (2x).
In VBA I tried the following:
1. Selecting the right shape (works), then
ActiveWindow.Selection.ShapeRange.Ungroup.Select
This runs on an error: "...cannot ungroup".
In another thread a "solution" was given: Recording a macro -> not possible in PPT 2007!
When I record a macro in PPT 2003, it says exactly the same:
ActiveWindow.Selection.ShapeRange.Ungroup.Select
but it doesn't work.
It seems that there should be one step before that converts the emf image into "office format" (if you do it without VBA after clicking "ungroup" a message box occurs that askes, if you want to convert the graphics into "office format").
Any idea what to do that ungroup is working with VBA?
This works here:
Sub UngroupPastedChart()
Dim oShRange As ShapeRange
Dim oSh As Shape
Set oShRange = ActiveWindow.Selection.SlideRange.Shapes.PasteSpecial(ppPasteEnhancedMetafile)
Set oShRange = oShRange.Ungroup
Set oShRange = oShRange.Ungroup
End Sub
and add oShRange.Select if you want the ungrouped shapes to be selected at the end.