MudTools.OfficeInterop.Vbe
1.1.3
See the version list below for details.
dotnet add package MudTools.OfficeInterop.Vbe --version 1.1.3
NuGet\Install-Package MudTools.OfficeInterop.Vbe -Version 1.1.3
<PackageReference Include="MudTools.OfficeInterop.Vbe" Version="1.1.3" />
<PackageVersion Include="MudTools.OfficeInterop.Vbe" Version="1.1.3" />
<PackageReference Include="MudTools.OfficeInterop.Vbe" />
paket add MudTools.OfficeInterop.Vbe --version 1.1.3
#r "nuget: MudTools.OfficeInterop.Vbe, 1.1.3"
#:package MudTools.OfficeInterop.Vbe@1.1.3
#addin nuget:?package=MudTools.OfficeInterop.Vbe&version=1.1.3
#tool nuget:?package=MudTools.OfficeInterop.Vbe&version=1.1.3
MudTools.OfficeInterop.Vbe
VBE (Visual Basic Editor) 操作模块,提供对 Office 应用程序中 VBA 项目和代码的编程访问。
项目概述
MudTools.OfficeInterop.Vbe 是专门用于操作 Microsoft Visual Basic Editor (VBE) 的 .NET 封装库。该模块提供了对 VBE 对象模型的完整封装,包括 VBProject、VBComponent、CodeModule 等核心对象的管理。
通过使用本模块,开发者可以编程方式访问和操作 Office 应用程序中的 VBA 项目、组件和代码模块,实现自动化代码生成、宏管理等功能。
主要功能
- VBE 应用程序对象操作接口
- VBProject 项目管理功能
- VBComponent 组件管理功能
- CodeModule 代码模块操作功能
- VBA 引用管理功能
支持的框架
- .NET Framework 4.6.2
- .NET Framework 4.7
- .NET Framework 4.8
- .NET Standard 2.1
- .NET 6.0-windows
- .NET 7.0-windows
- .NET 8.0-windows
- .NET 9.0-windows
安装
<PackageReference Include="MudTools.OfficeInterop.Vbe" Version="1.1.2" />
核心组件
VBE 对象模型接口
VBE 模块提供以下核心接口:
- IVbeApplication - VBE 应用程序对象接口
- IVbeVBProject - VB 项目对象接口
- IVbeVBProjects - VB 项目集合接口
- IVbeVBComponent - VB 组件对象接口
- IVbeVBComponents - VB 组件集合接口
- IVbeCodeModule - 代码模块对象接口
- IVbeReference - 引用对象接口
- IVbeReferences - 引用集合接口
使用示例
访问 VBA 项目
// 通过 Excel 应用程序访问 VBE
using var excelApp = ExcelFactory.CreateApplication();
var vbeApp = excelApp.Vbe;
// 获取所有 VBA 项目
var vbProjects = vbeApp.VBProjects;
foreach (var project in vbProjects)
{
Console.WriteLine($"Project: {project.Name}");
Console.WriteLine($"Type: {project.Type}");
Console.WriteLine($"Description: {project.Description}");
}
操作 VBA 组件
// 获取活动的 VBA 项目
var activeProject = vbeApp.ActiveVBProject;
// 遍历项目中的所有组件
var components = activeProject.VBComponents;
foreach (var component in components)
{
Console.WriteLine($"Component: {component.Name}");
Console.WriteLine($"Type: {component.Type}");
// 获取组件的代码模块
var codeModule = component.CodeModule;
Console.WriteLine($"Lines of code: {codeModule.CountOfLines}");
}
添加和修改代码
// 获取或添加一个标准模块
var components = activeProject.VBComponents;
var module = components.Add(vbext_ComponentType.vbext_ct_StdModule);
module.Name = "GeneratedModule";
// 在模块中添加代码
var codeModule = module.CodeModule;
codeModule.AddFromString(@"
Sub HelloWorld()
MsgBox ""Hello, World!""
End Sub
Function AddNumbers(a As Integer, b As Integer) As Integer
AddNumbers = a + b
End Function
");
管理引用
// 获取项目引用
var references = activeProject.References;
// 添加新引用
try
{
references.AddFromGuid("{000204EF-0000-0000-C000-000000000046}", 4, 1); // VBA 扩展库
Console.WriteLine("Reference added successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to add reference: {ex.Message}");
}
// 列出所有引用
foreach (var reference in references)
{
Console.WriteLine($"Reference: {reference.Name}");
Console.WriteLine($"Description: {reference.Description}");
Console.WriteLine($"FullPath: {reference.FullPath}");
}
常见应用场景
- 自动化代码生成 - 动态生成 VBA 代码以实现特定功能
- 宏管理 - 管理和维护 Office 文档中的宏代码
- 插件开发 - 为 Office 应用程序开发 VBA 插件
- 代码分析 - 分析现有 VBA 项目的代码结构和内容
- 批量操作 - 批量修改多个 Office 文档中的 VBA 代码
注意事项
- 使用 VBE 功能需要启用 Office 应用程序的程序化访问权限
- 某些操作可能需要管理员权限
- 操作 VBA 代码时应格外小心,避免破坏现有功能
- 建议在操作前备份相关文件
许可证
本项目采用双重许可证模式:
免责声明
本项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任。
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net6.0-windows7.0 is compatible. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net8.0-windows7.0 is compatible. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- log4net (>= 3.2.0)
- MudTools.OfficeInterop (>= 1.1.3)
- System.ValueTuple (>= 4.6.1)
- WonderCircuits.Microsoft.Vbe.Interop (>= 15.0.0)
-
.NETStandard 2.0
- log4net (>= 3.2.0)
- MudTools.OfficeInterop (>= 1.1.3)
- System.Drawing.Common (>= 4.7.3)
- System.ValueTuple (>= 4.6.1)
- WonderCircuits.Microsoft.Vbe.Interop (>= 15.0.0)
-
net6.0-windows7.0
- log4net (>= 3.2.0)
- MudTools.OfficeInterop (>= 1.1.3)
- WonderCircuits.Microsoft.Vbe.Interop (>= 15.0.0)
-
net8.0-windows7.0
- log4net (>= 3.2.0)
- MudTools.OfficeInterop (>= 1.1.3)
- WonderCircuits.Microsoft.Vbe.Interop (>= 15.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on MudTools.OfficeInterop.Vbe:
Package | Downloads |
---|---|
MudTools.OfficeInterop.Excel
Excel COM组件通用功能二次封装 |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated | |
---|---|---|---|
1.1.6 | 106 | 9/19/2025 | |
1.1.5 | 154 | 9/17/2025 | |
1.1.4 | 114 | 9/14/2025 | |
1.1.3 | 86 | 9/12/2025 | |
1.1.2 | 252 | 9/11/2025 | |
1.1.1 | 246 | 9/10/2025 | |
1.1.0 | 247 | 9/8/2025 | |
1.0.7 | 321 | 8/26/2025 | |
1.0.6 | 180 | 8/23/2025 | |
1.0.5 | 249 | 8/21/2025 | |
1.0.4 | 219 | 8/21/2025 | |
1.0.3 | 224 | 8/19/2025 | |
1.0.2 | 215 | 8/18/2025 | |
1.0.1 | 226 | 8/18/2025 |
Initial release of MudTools.OfficeInterop.Vbe library.