Добавьте и вызовите VBA Sub на Visio Macro, используя C #

1

Мне нужно включить серию подпроцессов VBA Macro в документ Visio с помощью С# и вызвать один из них после завершения.

В настоящее время я могу добавить макрос в файл Visio, но я не могу вызвать этот недавно вставленный Sub:

//...
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?

Теги:
visio

1 ответ

2
Лучший ответ

Visio не имеет метода Run на объекте Application, но имеет Document.ExecuteLine:

'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

Ещё вопросы

Сообщество Overcoder
Наверх
Меню