未创建VBA对象?

时间:2019-01-09 12:44:37

标签: vba object powerpoint

在以下代码I中,有时并非总是如此!在“设置PP = pptApp.Presentations.Open(pptVorlage)”处出现错误。当我使用“?pptApp”在即时窗口中检查“ pptApp”的值时,VBA无法返回值。

这是什么意思?是否未创建对象?还是在代码期间设置为null?

谢谢!

Public myfilename As String

Sub Saveas_PDF()
    Dim PP As PowerPoint.Presentation
    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

    Set pptApp = New PowerPoint.Application

    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)
        Dim pptVorlage As String
        pptVorlage = myfilename

        Set PP = pptApp.Presentations.Open(pptVorlage) 'sometimes error (remote server machine not found.) + pptApp seems to be empty?
        PP.UpdateLinks
        pptApp.Visible = True
        Debug.Print (PP.Name)
        AppActivate (PP.Name)
        PP.Close
        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

    Set pptApp = Nothing
End Sub

Sub filepicker()

 Dim i As Variant
 MsgBox ("In the following dialog please choose the current file")
 Dim myfilenamepicker As FileDialog
 Set myfilenamepicker = Application.FileDialog(msoFileDialogFilePicker)
 myfilenamepicker.InitialFileName = "C:\Users\XY"
 myfilenamepicker.Show
 If myfilenamepicker.SelectedItems.Count <> 0 Then
 myfilename = myfilenamepicker.SelectedItems(1)
 End If


End Sub

Function IsAppRunning(ByVal sAppName) As Boolean
    Dim oApp As Object
    On Error Resume Next
    Set oApp = GetObject(, sAppName)
    If Not oApp Is Nothing Then
    Set oApp = Nothing
    IsAppRunning = True
    End If
End Function

1 个答案:

答案 0 :(得分:0)

我相信问题是由于问题中的代码具有两行相同的内容

Set pptApp = New PowerPoint.Application

每行将导致VBA启动PowerPoint的新实例,并尝试将它们分配给相同的对象变量(pptApp)。这是导致远程服务器错误的原因。

注释第二行,该行将启动PowerPoint的新实例,并查看是否工作得更好-或至少有所不同。