0

I have an VBA Macro that at times requires me to create a new slide with a new embedded excel spreadsheet, then edit that spreadsheet. A simplified version of this code:

Dim sld As Slide
Dim shp As shape
Dim pptWorkbook As Object

Set sld = ActivePresentation.slides.add(ActivePresentation.slides.Count + 1, ppLayoutBlank)
Set shp = sld.Shapes.AddOLEObject(100, 100, 100, 100, "Excel.Sheet")
DoEvents
If shp.Type = msoEmbeddedOLEObject Then
    'Error thrown here
    Set pptWorkbook = shp.OLEFormat.Object
    pptWorkbook.Sheets(1).Cells(1, 1).value = "Stuff"
End If

About half of the time running this code results in the error:

Method object of object OLEFormat failed

This occurs on the shp.OLEFormat.Object call, I believe that this is due to "AddOLEObject" not creating the excel object in time to provide access to the property(but this is just a hypothesis). I have tried various ways of getting around this by error handling and sleep functions but so far I have been unable to create a new excel object and modify its contents within the same function without generating some error.

So my question is: How do you, with VBA, add a new embedded excel spreadsheet within a PowerPoint document and edit its contents within the same function/sub?

Update 1

I have successfully run this code on other machines, so this issue may be environmental, related with my system, and not an issue with my methodology. It also could be permission related, similar to This Post.

Update 2

I have reinstalled Office, restarted, run PowerPoint as administrator, and have added logic to account for the issue detailed in This post. Still no progress, I wonder if anyone can replicate the error that I am receiving?

4

1 回答 1

0

我通过删除注册表中的所有相关键来重置我的所有办公设置来解决这个问题(详见此处):

HKEY_CURRENT_USER\软件\微软\办公室

经过进一步调查,结果发现我收到此错误消息的原因是因为我已将“OLAP PivotTable Extensions”(链接)添加到 excel 中,由于某种原因,这与“AddOLEObject”方法冲突。因此,只需删除扩展程序的注册表项即可有效地从 excel 中删除扩展程序并解决了我的问题。完全卸载扩展程序时也观察到相同的效果。

HKEY_CURRENT_USER\Software\Microsoft\Office\Excel\Addins\OlapPivotTableExtensions2016

于 2016-01-28T18:26:08.220 回答