Open an Embedded Object in Excel using VBA - vba

In an ms office document I've embedded / inserted an external document (object) (PDF in my case).
After opening the document, when I click on the PDF object icon, It opens up the PDF file embedded in it.
Using VBA / Macro I want to do the same thing, Where I'll have to run a macro and it will open up the embedded PDF file(Without clicking on the PDF ICON).
Is it possible?
Thanks,

Excel:
You can get the OLEObject form the OLEObjects of the Worksheet. See OLEObjects - https://msdn.microsoft.com/en-us/library/office/ff840244.aspx, OLEObject - https://msdn.microsoft.com/en-us/library/office/ff838421.aspx, OLEObject members - https://msdn.microsoft.com/EN-US/library/office/ff841208.aspx.
There is a method Verb which has a verb for opening the object. See https://msdn.microsoft.com/EN-US/library/office/ff838827.aspx - Verbs - https://msdn.microsoft.com/EN-US/library/office/ff820926.aspx
Example:
Sub test()
With ActiveSheet
Set o = .OLEObjects("Objekt 1")
o.Verb xlVerbOpen
End With
End Sub
"Objekt 1" is the name of the object in the Excel worksheet. The object must be in the active sheet.
Word:
In Word it depends on if the embedded object is in an InlineShape or an Shape. And there is no OLEObjects collection. So you must handle with Shape.OLEFormat. See InlineShapes - https://msdn.microsoft.com/en-us/library/office/ff822592.aspx, Shapes - https://msdn.microsoft.com/en-us/library/office/ff845240.aspx, Shape - https://msdn.microsoft.com/en-us/library/office/ff196943.aspx, OLEFormat - https://msdn.microsoft.com/EN-US/library/office/ff197153.aspx.
Example:
Sub test()
With ActiveDocument
Set oShape = .InlineShapes(1) 'The embedded object is the first InlineShape.
'Set oShape = .Shapes(1) 'The embedded object is the first Shape.
Set oOLEFormat = oShape.OLEFormat
oOLEFormat.Open
End With
End Sub

In short, when you already know which object you are referring to:
Excel
Sheets("Sheet1").OLEObjects("Object 1").Activate
Word
ActiveDocument.InlineShapes(1).OLEFormat.Open

Try this:
Sub test()
With ActiveSheet
Set o = .OLEObjects("Objekt 1")
o.Verb xlPrimary
End With
End Sub

Related

Use VBA to find name of linked Excel chart object in PowerPoint

Suppose I copy an Excel chart and then in PowerPoint I do Paste Special > Keep Source Formatting and Link Data. This will create a Shape object whose .Chart.ChartData.IsLinked property is True. What I am trying to figure out is how I can use VBA inside of PowerPoint to determine what Excel chart object is being linked to. For example, consider the code below:
Private Sub PrintLinks()
Dim pptPresentation As Presentation
Dim pptSlide As Slide
Dim pptShape As Shape
Set pptPresentation = ActivePresentation
For Each pptSlide In pptPresentation.Slides
For Each pptShape In pptSlide.Shapes
If pptShape.Type = msoChart Then
Debug.Print pptShape.LinkFormat.SourceFullName
Debug.Print pptShape.Chart.Name
End If
Next
Next
End Sub
The .LinkFormat.SourceFullName property points to the name of the Excel file, but does not indicate the name of the chart object within the file that is being linked to. Likewise, the .Chart.Name property does print a name like Chart 1, but I've found that this does not correspond to the name of the chart within Excel itself, i.e. when you click on the chart in Excel and look at the object name in the upper left next to the formula bar it can be something different.

Use VBA to change source file of chart pasted into PowerPoint using Link Data option

I have a PowerPoint presentation in which I create charts in Excel and then link them into the PowerPoint. There are two ways to do this:
Paste Special > Paste Link > Microsoft Excel Chart Object
Paste > Keep Source Formatting and Link Data / Use Destination Theme and Link Data
I would late like to use VBA to change the source Excel file. To do this, consider the following code:
Private Sub PrintLinks()
Dim pptPresentation As Presentation
Dim pptSlide As Slide
Dim pptShape As Shape
Set pptPresentation = ActivePresentation
For Each pptSlide In pptPresentation.Slides
For Each pptShape In pptSlide.Shapes
If pptShape.Type = msoChart Or pptShape.Type = msoLinkedOLEObject Or pptShape.Type = msoLinkedChart Then
Debug.Print pptShape.LinkFormat.SourceFullName
pptShape.LinkFormat.SourceFullName = "PATH/TO/NEW/FILE"
pptShape.LinkFormat.Update
End If
Next
Next
End Sub
This will work for the Paste Link case, but not the Link Data case, in which case pptShape.Type = msoChart. My question is if there is a way to make it work with Link Data as well. Wtih Paste Link, the SourceFullName property will point to a specific chart object, like filename1.xlsx!Chart 1, and changing it to filename2.xlsx!Chart 1 will work as expected. In contrast, under the Link Data option the SourceFullName property only points to filename1.xlsx and I cannot figure out how to see what chart object within the file it is pointing to. Regardless, if I change SourceFullName to filename2.xlsx no error will be thrown, but as far as I can tell the pointer is still to filename1.xlsx, as the chart doesn't change.

VBA Word: Change Data of charts

I want to change the data of a chart in a Word Document, but I can't find the right way to address my charts. I tried several techniques, but nothing worked. (I´d love to open a ExcelSheet in which I can just change the Data)
So to put it all together: I want to change the data (not the source), of a MS Word chart, which looks like that:
Edit(13.8.):
After request, I try to give you some "reference Code" to work with.
Sub ChangeChart()
Dim aktDocument As Document
Dim chrt As Chart
Dim SourceSheet As Excel.Worksheet
Set aktDocument = ActiveDocument
Set SourceSheet = aktDocument.Shapes(1).Chart.OpenSourceData 'I know it´s not that easy
SourceSheet.Range("B5") = newStuff
aktDocument.Shapes(1).Chart.SetSourceData = SourceSheet
End Sub
I know this may sounds utopic and ridiculous, but I just don´t know, how to address the chart in the right way, or to even work with it properly.
Edit(15.08):
Even after recreating the old charts, the following code is not able to find a shape which has a chart. And therefore it stops when the index is out of range.
Sub Test()
i = 0
Do While i < 100
i = i + 1
If ActiveDocument.Shapes(i).HasChart Then
MsgBox "found one!"
End If
Loop
End Sub
Solution(30.08.):
The answer from #Cindy Meister was the solution to my problem. After further working with it, I came to the problem, that the ChartData always opens on the screen, while running the code.
Just for reference this question was my workaround.
All Office applications use the Excel engine to create and manage charts. In Word, charts can be formatted in-line with the text or with text wrap formatting. In the former case, a chart object needs to be addressed via the InlineShapes collection, in the latter via the Shapes collection.
Since your sample code uses Shapes(1) I've used that in the code snippet below. If it's not certain that the first Shape in the document is the chart, but you've assigned the Shape a name, you can use that as the index value (for example Shapes("MyChart"). Or you can loop the Shapes collection and check HasChart.
HasChart returns True if the Shape (or InlineShape) is a Chart. It's then possible to set Shape.Chart to an object variable. The chart's data can be accessed using Chart.ChartData.Activate - if you don't use Activate it's not possible to access the data when the chart's worksheet is stored in the Word document. Only then can Chart.ChartData.Workbook return a workbook object, and through that the worksheet can be accessed using ActiveSheet. From that point on, it's like working with the Excel object model.
Sub ChangeChart()
Dim aktDocument As Document
Dim shp As Word.Shape
Dim chrt As Word.Chart
Dim wb As Excel.Workbook, SourceSheet As Excel.Worksheet
Set aktDocument = ActiveDocument
Set shp = aktDocument.Shapes(1)
If shp.HasChart Then
Set chrt = shp.Chart
chrt.ChartData.Activate
Set wb = chrt.ChartData.Workbook
Set SourceSheet = wb.ActiveSheet
SourceSheet.Range("B5").Value2 = newData
End If
End Sub

How to find equation editor in word document using VBA?

I am writing a macro in VBA Excel, which is used to do some data processing on a word document. During this, I've changed the font name for the entire document to Times New Roman. But I don't want the same change applied to the 'equation editor' boxes in the document, since their font is "Cambria Math". Changing the font to Times New Roman is resulting into ambiguous data.
The Equations object changed post 2007. Pre 2007, you could work with those objects by declaring Field objects. For example
UNTESTED
Sub Sample()
Dim fldEqn As Field
For Each fldEqn In ActiveDocument.Fields
If fldEqn.Type = wdFieldEmbed Then
If InStr(1, fldEqn, "Equation.3") Then
With fldEqn.Result.Font
'
'~~> Rest of the code
'
End With
End If
End If
Next oField
End Sub
To work with the Equation Objects from 2007 onwards you have to use the OMaths collection.
You can change the font of all the equations using this code
Sub Sample()
Dim eqns As OMath
For Each eqns In ActiveDocument.OMaths
With eqns.Range.Font
'
'~~> Rest of the code
'
End With
Next
End Sub

Deactivating OLEObject with VBA breaks ribbon in Word 2007

I am writing a script that loops through the embedded excel sheets in my document with VBA. I activate them, do some modifications and go on with the next one. Afterwards, I want the last sheet to be deactivated again and I want the cursor to return to the start of the document.
I have the following code so far:
Private Sub DeactivateOleObject(ByRef oOleFormat As OLEFormat)
On Error Resume Next
oOleFormat.ActivateAs "This.Class.Does.Not.Exist"
End Sub
Sub AutoOpen()
Dim lNumShapes As Long
Dim lShapeCnt As Long
Dim xlApp As Object
Dim wrdActDoc As Document
Set wrdActDoc = ActiveDocument
For lShapeCnt = 1 To wrdActDoc.InlineShapes.Count
If wrdActDoc.InlineShapes(lShapeCnt).Type = wdInlineShapeEmbeddedOLEObject Then
Dim oOleFormat As OLEFormat
Set oOleFormat = wrdActDoc.InlineShapes(lShapeCnt).OLEFormat
oOleFormat.Activate
DeactivateOleObject oOleFormat
End If
Next lShapeCnt
End Sub
I borrowed the deactivation code from Gary McGill. However, this method of deactivation breaks the ribbon in Word 2007.
I can imagine that it would be nicer to reactivate the main document instead of deactivating the OLEObject, but adding wrdActDoc.Activate doesn't seem to do this.
Is it possible to deactivate the excel worksheet without breaking the ribbon?
I don't agree with "tricking" Word like that with Gary's code you reference. See my other post on Update embedded excel file programmatically for how to safely deactivate (but know that it is SendKeys, so it will never be 100% perfect).