计算MS Word中自定义序列字段的使用次数

时间:2019-05-08 11:21:36

标签: ms-word seq

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

({STYLEREF "Heading 1" \s}.{SEQ Formula \* ARABIC \s 1})(产生以下内容:(3.1))。

我需要计算当前文档中的所有公式才能在Abstract中使用它。有没有办法自动做到?

3 个答案:

答案 0 :(得分:0)

有关此代码实际上涉及很多。试试:

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

或:

Sub DemoB()
Application.ScreenUpdating = False
Dim i As Long
ActiveWindow.View.ShowFieldCodes = True
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "(^d STYLEREF ""Heading 1"" \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
MsgBox i & " instances found."
End Sub

答案 1 :(得分:0)

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

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

具有:

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
Application.ScreenUpdating = True

或替换:

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

答案 2 :(得分:0)

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

所有图片都按带有标题的形状分组,这就是为什么我要遍历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

然后我在文档中使用这些值:

In this document there are { NUMPAGES \* Arabic \* MERGEFORMAT } pages, { DOCVARIABLE PicturesCount \* MERGEFORMAT } pictures, { DOCVARIABLE FormulasCount \* MERGEFORMAT } formulas and { DOCVARIABLE TablesCount \* MERGEFORMAT } tables.

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

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