快速从外部文件读取宏
这是我的代码:
For Each pj In wdApp.Application.VBE.VBProjects
x = pj.FileName
y = pj.Protection
If x <> "" Then
If y <> "1" Then
For Each vbcomp In pj.VBComponents
For i = 1 To vbcomp.CodeModule.CountOfLines
newMacro = vbcomp.CodeModule.ProcOfLine(Line:=i, prockind:=vbext_pk_Proc)
If curMacro <> newMacro Then
curMacro = newMacro
cboMacros.AddItem (curMacro)
Debug.Print curMacro
Else: newMacro = Null
End If
Next
Next
End If
Selection.InsertAfter vbCr
End If
x = ""
Next
Selection.Collapse wdCollapseEnd
End Sub
问题是我可以返回关联文件中所有宏的名称,但效率非常低。 85 个宏名称需要 @2 分钟。这是因为程序读取每个模块的每一行 (CountOfLines)。我只是希望在我的 else 语句中可以施展一些魔法。如果当前宏与 newmacro 相同,则跳至它们不同时。我不确定这是否可能。如果没有,是否有比 CountOfLines 更好的方法?
Here is my code:
For Each pj In wdApp.Application.VBE.VBProjects
x = pj.FileName
y = pj.Protection
If x <> "" Then
If y <> "1" Then
For Each vbcomp In pj.VBComponents
For i = 1 To vbcomp.CodeModule.CountOfLines
newMacro = vbcomp.CodeModule.ProcOfLine(Line:=i, prockind:=vbext_pk_Proc)
If curMacro <> newMacro Then
curMacro = newMacro
cboMacros.AddItem (curMacro)
Debug.Print curMacro
Else: newMacro = Null
End If
Next
Next
End If
Selection.InsertAfter vbCr
End If
x = ""
Next
Selection.Collapse wdCollapseEnd
End Sub
The issue is that I can return the name of all macros in an associated file, but it is horribly inefficient. It takes @2 minutes for 85 macro names. This is because the program reads every line of every module (CountOfLines). I was just hoping there may be some magic that could be performed in my else statement. If the current macro is the same as newmacro then just skip to when they are different. I am not sure if this is possible. If not, is there a better method to use than CountOfLines?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你遇到的一个问题是 ProcKind 是一个输出,而不是输入,所以你首先需要创建一个像这样的变量:
然后你的语句将是
真正的答案是你不需要遍历每一行,你可以跳过从过程到过程:
One problem you have is that ProcKind is an output, not an input, so you first need to create a variable like this:
Then your statement would be
The real answer is that you don't need to go through every line, you can skip from proc to proc:
此处的代码提取文件 I 中的所有宏名称 (39)我几乎立即开始工作。能不能不去适应一下呢?
The code here pulls all the macro names (39) in the file I am working on pretty much instantly. Can you not adapt it instead?