首页 > 解决方案 > VBA PowerPoint 将变量设置为空

问题描述

我对以下代码有疑问。代码现在可以工作,但直到我将变量设置为空(请参阅代码中带有 *** 的部分)。我收到各种错误消息(例如“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

标签: vbaloopspowerpoint

解决方案


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

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


推荐阅读