VBA PowerPoint将变量设置为空

时间:2019-01-08 00:24:07

标签: vba loops powerpoint

我对以下代码有疑问。该代码现在可以正常工作,但是直到我将Variables设置为空时才起作用(请参见代码中带有***的部分)。我收到各种错误消息(例如“ 462远程服务器计算机不存在或不可用”)

我的问题是:为什么我必须将这些变量设置为空?我想这与我使用循环正确有关吗?

谢谢!

Sub Saveas_PDF() 
Dim PP As PowerPoint.Presentation
Dim prs As PowerPoint.Presentation
Dim Sl As PowerPoint.Slide
Dim sh As Variant
Dim company As String
Set Dropdown.ws_company = Tabelle2
company = Dropdown.ws_company.Range("C2").Value

Dim strPOTX As String
Dim strPfad As String
Dim pptApp As Object

Call filepicker

Dim Cell As Range

For Each Cell In Dropdown.ws_company.Range(Dropdown.ws_company.Cells(5, 3), 
Dropdown.ws_company.Cells(Rows.Count, 
3).End(xlUp)).SpecialCells(xlCellTypeVisible)

 Dropdown.ws_company.Range("C2") = Cell

 Set pptApp = New PowerPoint.Application

 Dim pptVorlage As String
 pptVorlage = myfilename


 Set PP = pptApp.Presentations.Open(pptVorlage)



 PP.UpdateLinks 'Datei --> Informationen --> Verknüpfungen --> Automatisch 
 aktualisieren Haken setzen

 Dim newpath As String
 newpath = Replace(myfilename, "AXO", "" & Cell & " AXO")

 Dim newpathpdf As String
 newpathpdf = Replace(newpath, "pptx", "pdf")


 PP.ExportAsFixedFormat "" & newpathpdf & "", ppFixedFormatTypePDF, 
 ppFixedFormatIntentPrint


 pptApp.Visible = True

 Debug.Print (PP.Name)
 AppActivate (PP.Name)

 PP.Close

***Set pptApp = Nothing
Set PP = Nothing***

Next

Set pptApp = New PowerPoint.Application
If IsAppRunning("PowerPoint.Application") Then
  If pptApp.Windows.Count = 0 Then

    pptApp.Quit
  End If
End If
End Sub

1 个答案:

答案 0 :(得分:0)

可能是因为您正在以下行中的de循环内调用构造函数“ New”:Set pptApp = New PowerPoint.Application

在de foreach之前移动“ Set pptApp = New PowerPoint.Application”行并尝试。