gpt4 book ai didi

.net - 以编程方式从 Word 2007 文档中提取宏 (VBA) 代码

转载 作者:行者123 更新时间:2023-12-03 15:16:33 26 4
gpt4 key购买 nike

是否可以使用 API 从 Word 2007“docm”文档中提取所有 VBA 代码?

我已经找到了如何在运行时插入 VBA 代码,以及如何删除所有 VBA 代码,但没有将实际代码提取到我可以存储的流或字符串中(并在将来插入到其他文档中)。

任何提示或资源将不胜感激。

编辑 : 感谢大家,Aardvark 's answer正是我正在寻找的。我已将他的代码转换为 C#,并且能够使用 Visual Studio 2008 从类库中调用它。

using Microsoft.Office.Interop.Word;
using Microsoft.Vbe.Interop;

...

public List<string> GetMacrosFromDoc()
{
Document doc = GetWordDoc(@"C:\Temp\test.docm");

List<string> macros = new List<string>();

VBProject prj;
CodeModule code;
string composedFile;

prj = doc.VBProject;
foreach (VBComponent comp in prj.VBComponents)
{
code = comp.CodeModule;

// Put the name of the code module at the top
composedFile = comp.Name + Environment.NewLine;

// Loop through the (1-indexed) lines
for (int i = 0; i < code.CountOfLines; i++)
{
composedFile += code.get_Lines(i + 1, 1) + Environment.NewLine;
}

// Add the macro to the list
macros.Add(composedFile);
}

CloseDoc(doc);

return macros;
}

最佳答案

您可以将代码导出到文件中,然后将它们读回。

我一直在使用下面的代码来帮助我将一些 Excel 宏置于源代码管理之下(使用 Subversion 和 TortoiseSVN)。每当我在 VBA 编辑器打开的情况下保存时,它基本上都会将所有代码导出到文本文件。我把文本文件放在 subversion 中,以便我可以做差异。您应该能够改编/窃取其中的一些内容以在 Word 中工作。

CanAccessVBOM() 中的注册表检查对应于安全设置中的“信任对 Visual Basic 项目的访问”。

Sub ExportCode()

If Not CanAccessVBOM Then Exit Sub ' Exit if access to VB object model is not allowed
If (ThisWorkbook.VBProject.VBE.ActiveWindow Is Nothing) Then
Exit Sub ' Exit if VBA window is not open
End If
Dim comp As VBComponent
Dim codeFolder As String

codeFolder = CombinePaths(GetWorkbookPath, "Code")
On Error Resume Next
MkDir codeFolder
On Error GoTo 0
Dim FileName As String

For Each comp In ThisWorkbook.VBProject.VBComponents
Select Case comp.Type
Case vbext_ct_ClassModule
FileName = CombinePaths(codeFolder, comp.Name & ".cls")
DeleteFile FileName
comp.Export FileName
Case vbext_ct_StdModule
FileName = CombinePaths(codeFolder, comp.Name & ".bas")
DeleteFile FileName
comp.Export FileName
Case vbext_ct_MSForm
FileName = CombinePaths(codeFolder, comp.Name & ".frm")
DeleteFile FileName
comp.Export FileName
Case vbext_ct_Document
FileName = CombinePaths(codeFolder, comp.Name & ".cls")
DeleteFile FileName
comp.Export FileName
End Select
Next

End Sub
Function CanAccessVBOM() As Boolean
' Check resgistry to see if we can access the VB object model
Dim wsh As Object
Dim str1 As String
Dim AccessVBOM As Long

Set wsh = CreateObject("WScript.Shell")
str1 = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & _
Application.Version & "\Excel\Security\AccessVBOM"
On Error Resume Next
AccessVBOM = wsh.RegRead(str1)
Set wsh = Nothing
CanAccessVBOM = (AccessVBOM = 1)
End Function


Sub DeleteFile(FileName As String)
On Error Resume Next
Kill FileName
End Sub

Function GetWorkbookPath() As String
Dim fullName As String
Dim wrkbookName As String
Dim pos As Long

wrkbookName = ThisWorkbook.Name
fullName = ThisWorkbook.fullName

pos = InStr(1, fullName, wrkbookName, vbTextCompare)

GetWorkbookPath = Left$(fullName, pos - 1)
End Function

Function CombinePaths(ByVal Path1 As String, ByVal Path2 As String) As String
If Not EndsWith(Path1, "\") Then
Path1 = Path1 & "\"
End If
CombinePaths = Path1 & Path2
End Function

Function EndsWith(ByVal InString As String, ByVal TestString As String) As Boolean
EndsWith = (Right$(InString, Len(TestString)) = TestString)
End Function

关于.net - 以编程方式从 Word 2007 文档中提取宏 (VBA) 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com