MudTools.OfficeInterop.Vbe 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package MudTools.OfficeInterop.Vbe --version 1.0.6
                    
NuGet\Install-Package MudTools.OfficeInterop.Vbe -Version 1.0.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MudTools.OfficeInterop.Vbe" Version="1.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MudTools.OfficeInterop.Vbe" Version="1.0.6" />
                    
Directory.Packages.props
<PackageReference Include="MudTools.OfficeInterop.Vbe" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MudTools.OfficeInterop.Vbe --version 1.0.6
                    
#r "nuget: MudTools.OfficeInterop.Vbe, 1.0.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MudTools.OfficeInterop.Vbe@1.0.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MudTools.OfficeInterop.Vbe&version=1.0.6
                    
Install as a Cake Addin
#tool nuget:?package=MudTools.OfficeInterop.Vbe&version=1.0.6
                    
Install as a Cake Tool

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

安装

<PackageReference Include="MudTools.OfficeInterop.Vbe" Version="1.0.1" />

核心组件

VBE 对象模型接口

VBE 模块提供以下核心接口:

使用示例

访问 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}");
}

常见应用场景

  1. 自动化代码生成 - 动态生成 VBA 代码以实现特定功能
  2. 宏管理 - 管理和维护 Office 文档中的宏代码
  3. 插件开发 - 为 Office 应用程序开发 VBA 插件
  4. 代码分析 - 分析现有 VBA 项目的代码结构和内容
  5. 批量操作 - 批量修改多个 Office 文档中的 VBA 代码

注意事项

  1. 使用 VBE 功能需要启用 Office 应用程序的程序化访问权限
  2. 某些操作可能需要管理员权限
  3. 操作 VBA 代码时应格外小心,避免破坏现有功能
  4. 建议在操作前备份相关文件

许可证

本项目采用双重许可证模式:

免责声明

本项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。

不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任。

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Initial release of MudTools.OfficeInterop.Vbe library.