English 中文(简体)
绕过段落(绕过头三段)会带来超支。
原标题:Looping through paragraphs (skipping the first three) throws an overflow error
  • 时间:2023-10-02 23:45:51
  •  标签:
  • vba
  • ms-word

基本上,我有一些管理语言文件,其设置方式相同:

前三段为标题和两段。

然后,每个段落由三个部分组成,每个部分由表格分开:姓名代码对话

特征名称在中间没有任何空间(John Doe应为John Doe)。

我为这个小小小小小小小小小小小小小小小小小小小小小小小小节::

Sub RemoveSpacesFromCharacterNames()

  Counter variable to track the paragraph number
Dim paragraphCounter As Integer

  Loop through each paragraph in the document
For Each p In ActiveDocument.Paragraphs

  Increment the counter variable
paragraphCounter = paragraphCounter + 1

  Check if the paragraph is the first three paragraphs
If paragraphCounter > 3 Then

  Check if the paragraph contains three parts separated by tabs
If Split(p.Range.Text, vbTab).Count = 3 Then
    Dim charName As String, timeCode As String, dialogue As String
    charName = Split(p.Range.Text, vbTab)(0)
    timeCode = Split(p.Range.Text, vbTab)(1)
    dialogue = Split(p.Range.Text, vbTab)(2)

      Remove any spaces from the character name
    charName = Replace(charName, " ", "")

      Reconstruct the paragraph with the updated character name
    p.Range.Text = charName & vbTab & timeCode & vbTab & dialogue
End If
End If
Next p
End Sub

但是它只是坠毁了MS Word, 说第9行(第Counter = 第Counter+段) (1) 溢出量,从“charName = 替换(charName,“,”)上 st

It s weird because the test document only has 5 paragraphs (3 + 2)

什么可能造成无限的休息?

问题回答
  • The Split function yields an array with a base index of 0, so it doesn t possess a .Count property. In this case, you should use Ubound.

  • 改为p.Range。 整个段落的可能会扰乱。 每个的代号为替换该段的编号。 这可能会导致 lo。

Option Explicit
Sub RemoveSpacesFromCharacterNames()
      Counter variable to track the paragraph number
    Dim paragraphCounter As Integer
    Dim subPara, p As Paragraph
      Loop through each paragraph in the document
    For paragraphCounter = 1 To ActiveDocument.Paragraphs.Count
        If paragraphCounter > 3 Then
            Set p = ActiveDocument.Paragraphs(paragraphCounter)
              Check if the paragraph contains three parts separated by tabs
            subPara = Split(p.Range.Text, vbTab)
            If UBound(subPara) = 2 Then
                  Remove any spaces from the character name
                subPara(0) = Replace(subPara(0), " ", "")
                  Reconstruct the paragraph with the updated character name
                p.Range.Text = Join(subPara, vbTab)
            End If
        End If
    Next
End Sub

说明使用<条码>对每个格式更新段落所面临的挑战的代码。 产出包括第1段(ABC是第1段)<代码>,改为5倍,而不是包括前5段的内容。

Sub ForEachLoop()
    Dim sTxt As String, p As Paragraph, i As Integer
    For Each p In ActiveDocument.Paragraphs
        i = i + 1
        sTxt = p.Range.Text
        Debug.Print sTxt 
        p.Range.Text = sTxt
        If i > 5 Then Exit For
    Next
End Sub

Output: The result of

ABC

ABC

ABC

ABC

ABC

当然,如果愿意,可在使用<条码>时更新各段。

Sub ForEachLoop2()
    Dim sTxt As String, p As Paragraph, i As Integer
    Dim pRange As Range
    For Each p In ActiveDocument.Paragraphs
        Set pRange = ActiveDocument.Range(p.Range.Start, p.Range.End - 1)
        i = i + 1
        sTxt = pRange.Text
        Debug.Print sTxt
        pRange.Text = sTxt
        If i > 5 Then Exit For
    Next
End Sub




相关问题
Handling no results for docmd.applyfilter

I have an Access app where I use search functionality. I have a TextBox and a Search Button on the form, and it does a wildcard search of whatever the user enters in the TextBox, and displays the ...

Outlook 2007 CommandBarControl.Execute won t work

I recently switched to Outlook 2007 and noticed that my VBA-macros won t work. I use the following code to open a new appointment-item (and fill it automatically). It worked perfect in Outlook 2003, ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

MS Access: list macro from VBA

I have to deal with a few macros (not VBA) in an inherited Access application. In order to document them, I would like to print or list the actions in those macros, but I am very dissatisfied by ...

热门标签