假设我想在一个外部程序中编写一个VBA代码,该代码打开一个Excel文件,运行一个宏,保存(并且对任何弹出窗口说是)并关闭Excel。是否有可能这样做?如果是这样,我将如何去实施它?是否可以通过外部命令在Excel中运行宏?
回答
您可以启动Excel,打开工作簿,然后从VBScript文件中操作工作簿。
将以下代码复制到记事本中。
更新'MyWorkbook.xls'和'Sheet1'参数。
用vbs扩展名保存并运行它。
Option Explicit
On Error Resume Next
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\MyWorkbook.xls")
xlBook.Sheets("Sheet1").Cells(1, 1).Value = "My text"
xlBook.Sheets("Sheet1").Cells(1, 1).Font.Bold = TRUE
xlBook.Sheets("Sheet1").Cells(1, 1).Interior.ColorIndex = 6
xlBook.Save
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
上面这个代码启动Excel打开工作簿,在单元格A1中输入一个值,使其变为粗体并更改单元格的颜色。该工作簿然后保存并关闭。然后关闭Excel。
如何使用上述代码运行存储在工作簿中的宏? – Nerdtron 2011-12-16 15:34:37
http://stackoverflow.com/q/10232150/1303610检查了这一点。 – 2012-05-09 17:27:08
根据您试图实现的目标,您还可以通过(OLE)Automation从另一个应用程序(例如Access或Word)或从另一个环境(如Visual Basic(6)中编写的自己的应用程序控制Excel。通过.Net使用您选择的语言来实现这一点也是可能的(尽管有些比其他语言更容易实现)。
想要从外部应用程序控制Excel还是仅从外部触发现有工作簿中的VBA宏?
再次,根据您的意图,工作簿可能会有一个自动打开宏,它可能是基于外部值(例如ini文件或数据库值)的条件运行。
我可以想到几种方法来做到这一点。
You can start excel by creating a file with NotePad or a similar text editor.
Here are some steps:
Launch NotePad
Add the following line of text. Replace test.xlsm with the name and path for your file:
start Excel.exe "C\test.xlsm"
Save the file as "Test.bat".
Run the batch file.
The batch file should launch Excel and then open your file. The code in your workbook should run
OR
再次,使用记事本。
Option Explicit
On Error Resume Next
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\MyWorkbook.xls", 0, True)
xlApp.Run "MyMacro"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
或者,这个。
'Code should be placed in a .vbs file
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\Users\Ryan\Desktop\Sales.xlsm'!SalesModule.SalesTotal"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing
现在,这是不是“外部命令”,但Windows任务调度会做打开该文件为你的一个不错的工作,一旦被打开,你可以运行像一个小脚本你看到下面的一个。
Private Sub Workbook_Open()
Call CommandButton1_Click
End Sub
https://www.sevenforums.com/tutorials/11949-elevated-program-shortcut-without-uac-prompt-create.html
- 1. 从外部Excel使用VBScript从命令行运行Excel宏
- 2. 是否可以在IPython中通过调试来运行命令?
- 3. APK是否可以发送命令通过JNI运行dhcpcd?
- 4. 是否可以将外部命令行工具添加到Netbeans?
- 5. 通过Acces中的VBA运行Excel宏
- 6. 在Perl中运行外部命令
- 7. C#运行宏的外部Excel库
- 8. 如何通过执行外部命令来执行vim命令?
- 9. 是否可以通过命令行设置powershell opacity
- 10. 是否可以通过命令行编译“IDE-Managed Components”项目?
- 11. 是否可以通过命令行修改eclipse工作区?
- 12. Sysinternal命令是否可以在PowerShell中运行?
- 13. 是否可以在Rubymine中运行终端/命令提示符?
- 14. 是否可以在main中使用IntelliJ'run'命令运行卡尺?
- 15. 是否可以在宏中包含搜索命令?
- 16. 是否可以从刀执行命令中运行chef-client?
- 17. 是否可以通过编程方式运行“pm create-user name”命令?
- 18. Laravel 5.3 - 是否可以通过点击链接/按钮运行php artisan命令
- 19. 是否可以通过命令行构建和部署一个android eclipse项目
- 20. 运行Excel宏外练成
- 21. 可以通过paramiko的exec_command在后台运行命令吗?
- 22. 想要通过拖放运行Excel宏
- 23. 在bash中是否有命令检查程序是否可以运行
- 24. 通过Jenkins运行命令?
- 25. 可以通过ssis处理宏(Excel)吗?
- 26. Notepad ++在宏中使用“运行”命令
- 27. 是否可以运行列表中的命令?
- 28. 是否可以在每次安装gem后运行命令?
- 29. UI自动化仪器是否可以从命令行运行?
- 30. 是否可以从命令行运行Smalltalk脚本?
你知道什么类型/多少弹出窗口会出现? – Fionnuala 2010-07-06 08:49:51
嗨,你说“外部程序”是什么意思?它是Excel吗? – Trefex 2010-07-06 09:41:02