快速从外部文件读取宏

发布于 2024-09-27 17:42:48 字数 1037 浏览 3 评论 0原文

这是我的代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-黛色若梦 2024-10-04 17:42:48

你遇到的一个问题是 ProcKind 是一个输出,而不是输入,所以你首先需要创建一个像这样的变量:

Dim ProcKind As VBIDE.vbext_ProcKind

然后你的语句将是

newMacro = vbcomp.CodeModule.ProcOfLine(Line:=i, prockind)

真正的答案是你不需要遍历每一行,你可以跳过从过程到过程:

             For Each vbcomp In pj.VBComponents
                With vbcomp.CodeModule
                myStartLine = .CountOfDeclarationLines + 1
                While myStartLine < .CountOfLines
                  newMacro = .ProcOfLine(i,prockind)
                  numLines = .ProcCountLines(newMacro,vbext_pk_Proc)   
                  If curMacro <> newMacro Then
                      curMacro = newMacro
                      cboMacros.AddItem (curMacro)
                      Debug.Print curMacro
                  Else: newMacro = Null
                  End If
                  myStartLine = myStartLine + numLines
                Wend
                End With
             Next

One problem you have is that ProcKind is an output, not an input, so you first need to create a variable like this:

Dim ProcKind As VBIDE.vbext_ProcKind

Then your statement would be

newMacro = vbcomp.CodeModule.ProcOfLine(Line:=i, prockind)

The real answer is that you don't need to go through every line, you can skip from proc to proc:

             For Each vbcomp In pj.VBComponents
                With vbcomp.CodeModule
                myStartLine = .CountOfDeclarationLines + 1
                While myStartLine < .CountOfLines
                  newMacro = .ProcOfLine(i,prockind)
                  numLines = .ProcCountLines(newMacro,vbext_pk_Proc)   
                  If curMacro <> newMacro Then
                      curMacro = newMacro
                      cboMacros.AddItem (curMacro)
                      Debug.Print curMacro
                  Else: newMacro = Null
                  End If
                  myStartLine = myStartLine + numLines
                Wend
                End With
             Next
冷月断魂刀 2024-10-04 17:42:48

此处的代码提取文件 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文