Word中的VBA导致无限循环,没有消息框

问题描述 投票:0回答:1

代码的愿望是当一个“=?”在Word文档中遇到一个字符串,在这种情况下将添加“Result”。 我需要将 Word 文档从上到下移动,以便“结果”字符串对于文档中的行来说是正确的(为简单起见,未显示用于确定“结果”字符串的代码)。 在这种情况下,简单的查找和替换将不起作用。

如果我取出

msgbox "Calculating"
,代码就会进入无限循环。 我相信这可能与 Word 的刷新有关,因为我尝试将“结果”添加到
ParaRange.Text
。 通过阅读该网站上的其他帖子,我认为
wdCollapseEnd
可以解决问题,但事实并非如此。

Public Sub TrialParse()
    Dim singleLine As Paragraph
    Dim t As String
    Dim s As String
    Dim lineText As String
    Dim lineNumber As Long
    Dim ParaCol As Long
    Dim ParaRange As Range
    Dim ReplaceText As String
    
    Application.ScreenUpdating = False
    Set Evaluator = New VBAexpressions
    
               'Seperate by lline
            For Each singleLine In ActiveDocument.Paragraphs
                Set ParaRange = singleLine.Range
                lineText = singleLine.Range.Text
                lineNumber = ParaRange.Information(wdFirstCharacterLineNumber)
                ParaCol = ParaRange.Information(wdFirstCharacterColumnNumber)
                
                'seperate by tabs
                tstring = Split(lineText, vbTab)
                For i = LBound(tstring, 1) To UBound(tstring, 1)
                    'find the variable definitions
                    If (tstring(i) <> "") Then
                        If (InStr(tstring(i), "=") > 0 And InStr(tstring(i), "=?") < 1) Then
                              tstring(i) = Replace(tstring(i), " ", "")     'Remove spaces
                              tstring(i) = Replace(tstring(i), vbCr, "")     'Remove Line Return
                              seq = Split(tstring(i), "=")
                              'Call docvariables(CStr(seq(0)), Val(seq(1)))
                        ElseIf (InStr(tstring(i), "=?") > 0) Then   'This is an evaluation point
                            cstring = Split(tstring(i), "=?")
                            
                               ReplaceText = ParaRange.Text & "Result"
                                                            
                                MsgBox "Calculating"
                               
                                ParaRange.Text = ReplaceText
                                
                                   ' Move the range to the next character after the replacement
                               ParaRange.Collapse wdCollapseEnd
               
                        End If
                    End If
                Next i
            Next singleLine
            Application.ScreenUpdating = True
End Sub

我尝试在特定时间点将

Application.Updating
更改为 False 以及
Application.Refresh
。 任何帮助将不胜感激。

vba ms-word
1个回答
0
投票

我认为你把一个相当简单的任务搞得太复杂了:

Sub FindAndReplaceWithResult()
    Dim para As Paragraph
    Dim rng As Range
    Dim searchText As String
    Dim replaceText As String
    
    searchText = "=?"
    replaceText = "Result"
    
    ' Loop through each paragraph in the document
    For Each para In ActiveDocument.Paragraphs
        Set rng = para.Range
        
        ' Search for "=?" within the paragraph
        With rng.Find
            .Text = searchText
            .Replacement.Text = searchText & " " & replaceText
            .Forward = True
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll
        End With
    Next para
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.