【发布时间】:2018-05-06 19:17:09
【问题描述】:
我已经查看了这个问题的很多答案,但无法弄清楚我做错了什么。我正在尝试创建一个 pdf 文件。我从 excel 文件中获取数据并将其复制到 powerpoint 中。然后我尝试另存为 pdf,但它在宏的保存 pdf 部分不断给我一个错误(需要对象)(见下文)。我尝试多次更改它,但仍然无法使其正常工作。下面附上代码。解决此问题后,我需要能够更改我粘贴的对象的大小 - 我该怎么做。
Sub CreatePDFfiles_4()
Dim PPapp As Object
Dim PPPres As Object
Dim first_file As Boolean
Dim investorname As String
Dim path As String
Sheets("printing").Select
Range("g2").Select
file1 = ActiveCell.Value
Range("g3").Select
path = ActiveCell.Value
Range("g8").Select
investorname = ActiveCell.Value
Range("i8").Select
cor_file_name = ActiveCell.Value
DestinationPPT = "C:\Users\name\Documents\company\Investment Model\printing macro\template.pptx"
While investorname <> "end"
ActiveCell.Offset(0, -1).Select
print_data = ActiveCell.Value
If print_data = "Yes" Then
' Initialize PowerPoint Object Library
Set PPapp = CreateObject("Powerpoint.Application")
PPapp.Visible = True
' Open presentation
Set PPPres = PPapp.Presentations.Open(DestinationPPT)
'Copy excel file data
Windows(file1).Activate
Sheets(investorname).Select
Range("b1:r46").Select
Selection.Copy
'Paste into existing powerpoint template slide that is open
PPPres.slides(1).Shapes.Paste
'Save as pdf
PPPres.ExportAsFixedFormat ActivePresentation.path & "\" & cor_file_name & ".pdf", ppFixedFormatTypePDF, ppFixedFormatIntentPrint
PPapp.Quit
Set PPapp = Nothing
【问题讨论】:
-
除非您引用了 Powerpoint 库,否则您需要为所有常量赋值,例如
ppFixedFormatTypePDF。将Option Explicit作为代码模块的第一行,它会抱怨任何未定义的内容。 -
Re“在我解决了这个问题之后……” - 在当前问题得到解决之后,提出另一个问题。试图在一个问题中解决多个问题最终会以“过于宽泛”而结束问题。
-
我没有写太多代码。我尝试在 Sub CreatePDFfiles_4() 之后将其放入,但它不会接受。我应该把 Option Explicit 放在哪里?
-
Option Explicit应该是代码模块的 first 行 - 即在任何其他代码行之前。转到代码的最顶部(可能是您的Sub语句,也可能是其他内容),在之前插入一行,输入Option Explicit。 -
除了那些 PPT 常量之外,您还应该在 Excel VBA 中遇到
ActivePresentation的问题。cor_file_name来自哪里?您似乎没有粘贴该 Sub 的所有代码?
标签: vba pdf powerpoint