如何将 PowerPoint 中的附加值写入已打开的 Excel 文件?

发布于 2025-01-15 04:39:17 字数 478 浏览 2 评论 0原文

我在 PowerPoint 中有一个宏,可以更改 Excel 工作表中的值:

Sub Hello()
Dim xlApp As Object
Dim xlWorkBook As Object

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("TEST.xlsx", True, False)
xlWorkBook.sheets(1).Range("A1").Value = "Hello"

Set xlApp = Nothing
Set xlWorkBook = Nothing
End Sub

每次我通过按按钮激活 PowerPoint 中的宏时,Excel 文件都会再次打开,因此如果按三次,我会打开三个同名的文件。我只想打开一次。

I have a macro in PowerPoint that changes a value in an Excel sheet:

Sub Hello()
Dim xlApp As Object
Dim xlWorkBook As Object

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("TEST.xlsx", True, False)
xlWorkBook.sheets(1).Range("A1").Value = "Hello"

Set xlApp = Nothing
Set xlWorkBook = Nothing
End Sub

Every time I activate the macro in PowerPoint by pressing a button the Excel file is opened again, so if I push three times I have three files with the same name open. I want to open it only one time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只等公子 2025-01-22 04:39:17

这应该按照您想要的方式工作:

Sub Hello()
Dim xlApp As Object
Dim xlWorkBook As Object

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("c:\temp\TEST.xlsx", True, False)
xlWorkBook.sheets(1).Range("A1").Value = "Hello"

' save the file, or there's not much point to this
xlWorkBook.Save
' close the workbook
xlWorkBook.Close
' quit Excel
xlApp.Quit

' Now that Excel has quit, it doesn't really
' matter, but I've swapped these two.
Set xlWorkBook = Nothing
Set xlApp = Nothing

End Sub

This should work as you want it to:

Sub Hello()
Dim xlApp As Object
Dim xlWorkBook As Object

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("c:\temp\TEST.xlsx", True, False)
xlWorkBook.sheets(1).Range("A1").Value = "Hello"

' save the file, or there's not much point to this
xlWorkBook.Save
' close the workbook
xlWorkBook.Close
' quit Excel
xlApp.Quit

' Now that Excel has quit, it doesn't really
' matter, but I've swapped these two.
Set xlWorkBook = Nothing
Set xlApp = Nothing

End Sub
五里雾 2025-01-22 04:39:17

由于您希望(可能)重复写入 Excel 文件,因此您必须创建并(重新)附加到单个 Excel 应用程序,然后打开并(重新)附加到单个 Excel 文件。下面的示例代码展示了如何做到这一点:

Option Explicit

Sub test()
    WriteToWB "hello", CellAddr:="A1"
    WriteToWB "goodbye", CellAddr:="B1", CloseXlFile:=True
End Sub

Sub WriteToWB(ByVal NewValue As Variant, _
              ByVal CellAddr As String, _
              Optional CloseXlFile As Boolean = False)
    Dim xlFile As Excel.Workbook
    Set xlFile = AttachToExcelFile("C:\Temp\Book1.xlsx")
    
    Dim xlSheet As Excel.Worksheet
    Set xlSheet = xlFile.Sheets("Sheet1")
    
    xlSheet.Range(CellAddr).Value = NewValue
    
    If CloseXlFile Then
        xlFile.Close SaveChanges:=True
        QuitExcelApplication
    End If
End Sub

Public Function AttachToExcelFile(ByVal xlFilename As String) As Excel.Workbook
    Dim xlApp As Excel.Application
    Set xlApp = AttachToExcelApplication
    xlApp.Visible = True
    
    '--- this will open the workbook anew, or attach
    '    to the currently open workbook (as long as you
    '    don't close it)
    Dim xlWB As Excel.Workbook
    Set xlWB = xlApp.Workbooks.Open(Filename:=xlFilename, _
                                    ReadOnly:=False)
    Set AttachToExcelFile = xlWB
End Function

Public Sub QuitExcelApplication()
    Dim xlApp As Excel.Application
    Set xlApp = AttachToExcelApplication
    xlApp.Quit
End Sub

Public Function AttachToExcelApplication() As Excel.Application
    '--- finds an existing and running instance of MS Excel, or starts
    '    the application if one is not already running
    Dim xlApp As Excel.Application
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If Err > 0 Then
        '--- we have to start one
        '    an exception will be raised if the application is not installed
        Set xlApp = CreateObject("Excel.Application")
    End If
    Set AttachToExcelApplication = xlApp
End Function

Since you want to (possibly) repeatedly write to an Excel file, you'll have to create and (re)attach to a single Excel application and open and (re)attach to a single Excel file. The example code below shows how this can be done:

Option Explicit

Sub test()
    WriteToWB "hello", CellAddr:="A1"
    WriteToWB "goodbye", CellAddr:="B1", CloseXlFile:=True
End Sub

Sub WriteToWB(ByVal NewValue As Variant, _
              ByVal CellAddr As String, _
              Optional CloseXlFile As Boolean = False)
    Dim xlFile As Excel.Workbook
    Set xlFile = AttachToExcelFile("C:\Temp\Book1.xlsx")
    
    Dim xlSheet As Excel.Worksheet
    Set xlSheet = xlFile.Sheets("Sheet1")
    
    xlSheet.Range(CellAddr).Value = NewValue
    
    If CloseXlFile Then
        xlFile.Close SaveChanges:=True
        QuitExcelApplication
    End If
End Sub

Public Function AttachToExcelFile(ByVal xlFilename As String) As Excel.Workbook
    Dim xlApp As Excel.Application
    Set xlApp = AttachToExcelApplication
    xlApp.Visible = True
    
    '--- this will open the workbook anew, or attach
    '    to the currently open workbook (as long as you
    '    don't close it)
    Dim xlWB As Excel.Workbook
    Set xlWB = xlApp.Workbooks.Open(Filename:=xlFilename, _
                                    ReadOnly:=False)
    Set AttachToExcelFile = xlWB
End Function

Public Sub QuitExcelApplication()
    Dim xlApp As Excel.Application
    Set xlApp = AttachToExcelApplication
    xlApp.Quit
End Sub

Public Function AttachToExcelApplication() As Excel.Application
    '--- finds an existing and running instance of MS Excel, or starts
    '    the application if one is not already running
    Dim xlApp As Excel.Application
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If Err > 0 Then
        '--- we have to start one
        '    an exception will be raised if the application is not installed
        Set xlApp = CreateObject("Excel.Application")
    End If
    Set AttachToExcelApplication = xlApp
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文