Ms word 统计MS Word中自定义序列字段的已用出现次数

Ms word 统计MS Word中自定义序列字段的已用出现次数,ms-word,seq,Ms Word,Seq,我已经为公式编号创建了一个自定义序列字段: ({STYLEREF“Heading 1”\s}.{SEQ Formula\*ARABIC\s 1})(生成以下内容:(3.1)) 我需要计算当前文档中的所有公式才能抽象使用它。有没有一种自动执行的方法?这方面的代码实际上相当复杂。尝试: Sub DemoA() Application.ScreenUpdating = False Dim Fld As Field, Rng As Range, i As Long For Each Fld In Act

我已经为公式编号创建了一个自定义序列字段:

({STYLEREF“Heading 1”\s}.{SEQ Formula\*ARABIC\s 1})
(生成以下内容:
(3.1)


我需要计算当前文档中的所有公式才能抽象使用它。有没有一种自动执行的方法?

这方面的代码实际上相当复杂。尝试:

Sub DemoA()
Application.ScreenUpdating = False
Dim Fld As Field, Rng As Range, i As Long
For Each Fld In ActiveDocument.Fields
  With Fld
    If .Type = wdFieldStyleRef Then
      If Trim(.Code.Text) = "STYLEREF ""Heading 1"" \s" Then
        If .Result.Characters.First.Previous = "(" Then
          If .Result.Characters.Last.Next = "." Then
            Set Rng = .Result
            With Rng
              .End = .End + 3
              If .Fields.Count = 2 Then
                If .Fields(2).Type = wdFieldSequence Then
                  If Trim(.Fields(2).Code.Text) = "SEQ Formula \* ARABIC \s 1" Then
                    If .Fields(2).Result.Characters.Last.Next = ")" Then
                      i = i + 1
                    End If
                  End If
                End If
              End If
            End With
          End If
        End If
      End If
    End If
  End With
Next
MsgBox "Count: " & i
Application.ScreenUpdating = True
End Sub
或:


在这种情况下,您可以在希望输出显示的任何位置向文档添加DOCPROPERTY字段。DOCPROPERTY字段将编码为{DOCPROPERTY“SEQ#”}。此外,您还需要替换:

MsgBox "Count: " & i
Application.ScreenUpdating = True
与:

或替换:

ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
MsgBox i & " instances found."
与:


多亏了@macrood,当他发布第二个答案时,我得到了一个类似的答案。 因此,我需要计算文档中公式、图片和表格的数量

所有图片都分组在一个带有标题的形状中,这就是为什么我迭代ActiveDocument.Shapes来查找所需图片的原因

我使用以下宏:

Sub Pictures()
Application.ScreenUpdating = False
Dim i As Long
ActiveWindow.View.ShowFieldCodes = True
For Each shp In ActiveDocument.Shapes
    If shp.GroupItems(2).TextFrame.TextRange.Text Like "*Picture*" Then i = i + 1
Next
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
ActiveDocument.Variables("PicturesCount") = i
ActiveDocument.Fields.Update
Application.StatusBar = i & " pictures found."
End Sub

Sub Formulas()
Application.ScreenUpdating = False
Dim i As Long
ActiveWindow.View.ShowFieldCodes = True
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "(^d STYLEREF ""Heading 1 Formula"" \s"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    .MoveEndUntil ")", wdForward
    If .Text = "(" & Chr(19) & " STYLEREF ""Heading 1"" \s " & Chr(21) & "." & Chr(19) & " SEQ Formula \* ARABIC \s 1 " & Chr(21) Then i = i + 1
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
ActiveDocument.Variables("FormulasCount") = i
ActiveDocument.Fields.Update
Application.StatusBar = i & " formulas found."
End Sub

Sub Tables()
Application.ScreenUpdating = False
Dim i As Long
ActiveWindow.View.ShowFieldCodes = True
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "SEQ"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    .MoveEndUntil Chr(21), wdForward
    If .Text Like "*Table*" Then i = i + 1
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
ActiveDocument.Variables("TablesCount") = i
ActiveDocument.Fields.Update
Application.StatusBar = i & " tables found."
End Sub

Sub All()
    Pictures
    Formulas
    Tables
End Sub
然后我在文档中使用这些值:

本文档中有{NUMPAGES\*阿拉伯语\*MERGEFORMAT}页面、{DOCVARIABLE PicturesCount\*MERGEFORMAT}图片、{DOCVARIABLE formulascont\*MERGEFORMAT}公式和{DOCVARIABLE tablecount\*MERGEFORMAT}表格。

现在应该调用宏来更新文档中的值


再次感谢@macropod,他为我指明了正确的方向。

StackOverflow不是一个免费的编码论坛。请发布您自己的代码并解释问题所在。人们还想知道为什么不使用Word的多级列表编号或标题工具进行编号。答案就在你的问题中。@Freeflow。虽然可以使用通配符查找来计算(3.1)格式中的字符串数量,但这并不是说所有此类字符串都适用于公式引用。(3.1)格式的字符串可能适用于任何交叉引用,例如。@macrood。seq字段包含名称“Formula”,因此可以在文档字段中迭代测试代码中包含文本“Formula”的序列字段。Simples。如果按照规定,SEQ字段前面需要有一个特定的STYLEREF字段,然后是一个句点,那么就不那么简单了。看起来很棒!不过有一个小问题:我们是否可以将宏生成的值输出到文档中的一个字段中,这样它就可以自动更新(或者按F9键,就像TOC被更新一样)?
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
MsgBox i & " instances found."
With ActiveDocument
  On Error Resume Next
  .CustomDocumentProperties.Add Name:="SEQ#", LinkToContent:=False, Value:=0, Type:=msoPropertyTypeNumber
  On Error GoTo 0
  .CustomDocumentProperties("SEQ#").Value = 1
  .Fields.Update
End With
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
Sub Pictures()
Application.ScreenUpdating = False
Dim i As Long
ActiveWindow.View.ShowFieldCodes = True
For Each shp In ActiveDocument.Shapes
    If shp.GroupItems(2).TextFrame.TextRange.Text Like "*Picture*" Then i = i + 1
Next
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
ActiveDocument.Variables("PicturesCount") = i
ActiveDocument.Fields.Update
Application.StatusBar = i & " pictures found."
End Sub

Sub Formulas()
Application.ScreenUpdating = False
Dim i As Long
ActiveWindow.View.ShowFieldCodes = True
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "(^d STYLEREF ""Heading 1 Formula"" \s"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    .MoveEndUntil ")", wdForward
    If .Text = "(" & Chr(19) & " STYLEREF ""Heading 1"" \s " & Chr(21) & "." & Chr(19) & " SEQ Formula \* ARABIC \s 1 " & Chr(21) Then i = i + 1
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
ActiveDocument.Variables("FormulasCount") = i
ActiveDocument.Fields.Update
Application.StatusBar = i & " formulas found."
End Sub

Sub Tables()
Application.ScreenUpdating = False
Dim i As Long
ActiveWindow.View.ShowFieldCodes = True
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "SEQ"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    .MoveEndUntil Chr(21), wdForward
    If .Text Like "*Table*" Then i = i + 1
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
ActiveDocument.Variables("TablesCount") = i
ActiveDocument.Fields.Update
Application.StatusBar = i & " tables found."
End Sub

Sub All()
    Pictures
    Formulas
    Tables
End Sub