复制 PowerPoint 用户输入数据以打开 Excel 电子表格

Copy PowerPoint user input data to open Excel spreadsheet

我在 Powerpoint 中有一个用户输入数据的宏。我希望将此数据保存在已经打开的电子表格中。

如何让 PowerPoint 与电子表格通信?

我明白了

error 9: Subscript out of range.

Sub SaveBatchNo()

Dim strResult As String
strResult = InputBox("Please enter batch number.")

Workbooks("PP test.xlsx").Worksheets(1).Range("A1").Value = strResult

SlideShowWindows(1).View.GotoSlide 2

End Sub

一种方法如下:

Sub SaveBatchNo()
    Dim strResult As String
    Dim ExcelApp As Object, WB As Object
    
    strResult = InputBox("Please enter batch number.")
    
    On Error GoTo errHandler    'https://docs.microsoft.com/ru-ru/office/vba/language/reference/user-interface-help/on-error-statement
    
    Set ExcelApp = GetObject(, "Excel.Application") ' get Excel application if it's running
    Set WB = ExcelApp.Workbooks("PP test.xlsx") ' get Workbook "PP test.xlsx" if it's opened
    WB.Worksheets(1).Range("A1").Value = strResult
    
    On Error GoTo 0
    
    ActivePresentation.SlideShowSettings.Run    ' without this instruction the next instruction raises the error
  
    SlideShowWindows(1).View.GotoSlide 2
   
    Exit Sub
errHandler:
   MsgBox "Running Excel app. with opened Workbook 'PP test.xlsx' not detected", vbCritical + vbOKOnly, "Sub SaveBatchNo()"
End Sub