Excel / PowerPoint VBA和后期绑定

时间:2017-11-22 04:22:18

标签: vba excel-vba powerpoint powerpoint-vba excel

我一直在构建一个工具,用于从Excel工作簿中的数据生成PowerPoint幻灯片。我在办公室的电脑和家里的电脑之间来回移动。工作计算机有Excel 2013而家用计算机有2016年。这通常不是问题...当我从家用计算机移动到工作计算机时,我只需要将参考从v16对象库更改为v15对象库。

本周早些时候虽然我遇到了一个错误但我无法解决...详细here解决它的一个建议是切换到后期绑定,所以我不需要引用。这是一个非常简单的开关,但它导致了导致ppt幻灯片的错误......

在原始(早期绑定)版本中,我设置了像

Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim PPLayout As CustomLayout
Dim PPshape As Variant
Dim tBox As PowerPoint.Shape

然后

Set PPApp = New PowerPoint.Application
Set PPPres = PPApp.Presentations.Open(fileName, msoTrue, , msoFalse)

然后幻灯片的一部分是用

构建的
wsGenerator.Activate
wsGenerator.Range("B32:L37").Select
Selection.Copy
With PPSlide.Shapes.PasteSpecial(ppPasteMetafilePicture)
     .Top = 450
     .Left = 50
     .Height = 100
     .Width = 325
End With

结果幻灯片的一部分如下所示

enter image description here

底部的表格是上面代码粘贴的部分。

当我改为后期绑定时,我只是进行了这些更改

'Dim PPApp As PowerPoint.Application
Dim PPApp As Object

'Dim PPPres As PowerPoint.Presentation
Dim PPPres As Object

'Dim PPSlide As PowerPoint.Slide
Dim PPSlide As Object

'Dim PPLayout As CustomLayout
Dim PPLayout As Object

'Dim tBox As PowerPoint.Shape
Dim tBox As Object

Dim PPshape As Variant

然后

'Set PPApp = New PowerPoint.Application
Set PPApp = CreateObject("PowerPoint.Application")

其他一切都是一样的。结果图表现在看起来像这样

enter image description here

请注意,它现在从幻灯片的底部延伸出来。

关于这是什么的任何想法?

1 个答案:

答案 0 :(得分:4)

您似乎已将声明更改为延迟绑定,但您可能仍在使用某些PowerPoint常量(例如ppPasteMetafilePicture),这些常量将不再解析为其早期绑定值,而是默认为0

您需要为ppPasteMetafilePicture定义一个本地常量,以使该值可用。

顺便说一下,你应该总是使用Option Explicit,然后VBE会自动发现未声明的常量。