首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用C#在Visio上添加和调用VBA

使用C#在Visio上添加和调用VBA
EN

Stack Overflow用户
提问于 2014-12-18 20:24:21
回答 1查看 955关注 0票数 1

我需要使用C#将一系列VBA子进程包含到Visio文档中,并在完成后调用其中一个。

我目前能够将宏添加到Visio文件中,但无法调用最近插入的Sub

代码语言:javascript
复制
//...
using Office = Microsoft.Office.Core;
using VBA = Microsoft.Vbe.Interop;
using Visio = Microsoft.Office.Interop.Visio;
//...
Visio.Application vsApp  vsApp = new Visio.Application();
Visio.Document doc = vsApp.Documents.Open("filepath");
vsApp.Visible = true;
VBA.VBComponent oModule = doc.VBProject.VBComponents.Add(VBA.vbext_ComponentType.vbext_ct_StdModule);
String sCode =
    "sub VBAMacro()\r\n" +
    "   msgbox \"VBA Macro called\"\r\n" +
    "end sub";
    // Add the VBA macro to the new code module.
oModule.CodeModule.AddFromString(sCode);

//HOW TO INVOKE VBAMacro ??

doc.Close();
vsApp.Quit();

为了调用VBAMacro,我需要做什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-18 21:49:18

Visio在应用程序对象上没有Run方法,但是它有Document.ExecuteLine

代码语言:javascript
复制
'Executes the macro (procedure without an argument) named "SomeMacro" 
 'that is in some module of the Visual Basic project of ThisDocument. 
 ThisDocument.ExecuteLine("SomeMacro") 

 'Executes the procedure named SomeProcedure and passes it 3 arguments. 
 ThisDocument.ExecuteLine("SomeProcedure 1, 2, 3") 

 'Same as previous example, but procedure name qualified 
 'with module name. 
 ThisDocument.ExecuteLine("Module1.SomeProcedure 1, 2, 3") 

 'Shows the form UserForm1. 
 ThisDocument.ExecuteLine("UserForm1.Show") 

 'Prints "some string" to the Immediate window. 
 ThisDocument.ExecuteLine("Debug.Print ""some string""") 

 'Prints number of open documents to the Immediate window. 
 ThisDocument.ExecuteLine("Debug.Print Documents.Count") 

 'Tells ThisDocument to save itself. 
 ThisDocument.ExecuteLine("ThisDocument.Save") 

来自:http://msdn.microsoft.com/en-us/library/office/ff765100(v=office.15).aspx

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27555059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档