如何将 PowerPoint 中的附加值写入已打开的 Excel 文件?
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该按照您想要的方式工作:
This should work as you want it to:
由于您希望(可能)重复写入 Excel 文件,因此您必须创建并(重新)附加到单个 Excel 应用程序,然后打开并(重新)附加到单个 Excel 文件。下面的示例代码展示了如何做到这一点:
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: