【发布时间】:2021-01-19 16:36:22
【问题描述】:
我正在尝试调整从 Excel 复制到 PowerPoint 中的表格的大小,但是当我尝试将活动形状设置为调整大小时出现以下错误。
Run-time error '-2147188160 (80048240)':
Method 'Item' of object 'Shapes' failed
错误在代码中突出显示了这部分:
Set shp = sld.Shapes(i)
奇怪的是,在我点击“调试”并再次点击“运行”后,代码将继续在我的循环中正常运行,直到它遇到同一行代码。 PowerPoint 幻灯片上已经有 10 个形状,这就是我设置 i=11 的原因。 知道如何消除此错误吗?或者比我目前正在做的更好的调整表格大小的方法
Option Explicit
Sub PPT_Populator()
Dim ppt_app As New PowerPoint.Application
Dim pres As PowerPoint.Presentation
Dim sld As PowerPoint.slide
Dim shp As PowerPoint.shape
Dim wb As Workbook
Dim rng As Range
Dim vSheet$
Dim vRange$
Dim vWidth As Double
Dim vHeight As Double
Dim vTop As Double
Dim vLeft As Double
Dim vSlide As Long
Dim i As Long
Set pres = ppt_app.Presentations.Open([pptPath])
Set wb = ThisWorkbook
i = 11
For Each rng In Range("Market_Objects_Table")
With ThisWorkbook.Sheets("PPT Populator")
vSheet$ = .Cells(rng.Row, 4).Value
vRange$ = .Cells(rng.Row, 5).Value
vWidth = .Cells(rng.Row, 6).Value
vHeight = .Cells(rng.Row, 7).Value
vTop = .Cells(rng.Row, 8).Value
vLeft = .Cells(rng.Row, 9).Value
vSlide = .Cells(rng.Row, 10).Value
End With
ThisWorkbook.Sheets(vSheet$).Activate
ThisWorkbook.Sheets(vSheet$).Range(vRange$).Copy
Set sld = pres.Slides(vSlide)
pres.Slides(vSlide).Select
ppt_app.CommandBars.ExecuteMso ("PasteExcelTableDestinationTableStyle")
Set shp = sld.Shapes(i) <----error happens here
With shp
.LockAspectRatio = msoFalse
.Top = vTop
.Left = vLeft
.Width = vWidth
.Height = vHeight
End With
Set sld = Nothing
Set shp = Nothing
Application.CutCopyMode = False
i = i + 1
Next rng
End Sub
【问题讨论】:
-
虽然您的代码在粘贴到 PPT 后立即执行下一条语句,但有时粘贴需要更长的时间。尝试在粘贴后添加一个 doevents 循环,给 PPT 一些时间来处理粘贴。
标签: excel vba powerpoint