在文本中间更改跨引用文本,以使用ShowFieldCodes进行小写

发布于 2025-02-09 13:16:09 字数 1754 浏览 2 评论 0原文

我想将文本中间的所有交叉引用文本更改为小写,而不是句子开始时的文本。

问题是pactivedocument.Paragraphs(row).range.text正在浏览原始文本,而不是ShowfieldCodes(或Alt+F9视图),该文本生成更长的段落和行。

Sub SetLowerCase()
Dim bBig As Boolean
Dim txt As String, row As String, pos As Integer
ActiveWindow.View.ShowFieldCodes = True
Selection.HomeKey unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Text = "^d REF"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = False
        Do While .Execute
            pos = Selection.Information(wdFirstCharacterColumnNumber)
            row = Selection.Information(wdFirstCharacterLineNumber)
            'The problem is that "ShowFieldCodes" generates longer paragraphs and more rows, which the next line does not take into account
            txt = ActiveDocument.Paragraphs(row).Range.Text
            If pos = 1 Then
                bBig = True
            ElseIf Mid(txt, pos - 2, 2) = ". " Then
                bBig = True
            ElseIf Mid(txt, pos - 1, 1) = "." Then
                bBig = True
            End If
            If bBig = False Then
                If Not Selection.Text Like "*Lower*" Then
                    With Selection
                        .MoveRight unit:=wdCharacter, Count:=1
                        .MoveLeft unit:=wdCharacter, Count:=1
                        .TypeText Text:="\*Lower "
                        .Fields.Update
                    End With
                End If
            Else
                bBig = False
            End If
            Selection.Collapse wdCollapseEnd
        Loop
    End With
ActiveWindow.View.ShowFieldCodes = False
End Sub

I want to change all the cross-reference text that are in middle of the text to lowercase, but not the ones at the beginning of a sentence.

The problem is that ActiveDocument.Paragraphs(row).Range.Text is going through the original text, not the ShowFieldCodes (or Alt+F9 view), which generates longer paragraphs and rows.

Sub SetLowerCase()
Dim bBig As Boolean
Dim txt As String, row As String, pos As Integer
ActiveWindow.View.ShowFieldCodes = True
Selection.HomeKey unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Text = "^d REF"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = False
        Do While .Execute
            pos = Selection.Information(wdFirstCharacterColumnNumber)
            row = Selection.Information(wdFirstCharacterLineNumber)
            'The problem is that "ShowFieldCodes" generates longer paragraphs and more rows, which the next line does not take into account
            txt = ActiveDocument.Paragraphs(row).Range.Text
            If pos = 1 Then
                bBig = True
            ElseIf Mid(txt, pos - 2, 2) = ". " Then
                bBig = True
            ElseIf Mid(txt, pos - 1, 1) = "." Then
                bBig = True
            End If
            If bBig = False Then
                If Not Selection.Text Like "*Lower*" Then
                    With Selection
                        .MoveRight unit:=wdCharacter, Count:=1
                        .MoveLeft unit:=wdCharacter, Count:=1
                        .TypeText Text:="\*Lower "
                        .Fields.Update
                    End With
                End If
            Else
                bBig = False
            End If
            Selection.Collapse wdCollapseEnd
        Loop
    End With
ActiveWindow.View.ShowFieldCodes = False
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

椵侞 2025-02-16 13:16:09

例如:

Sub Demo()
Application.ScreenUpdating = False
Dim Fld As Field, Rng As Range, StrRef As String
With ActiveDocument
  .ActiveWindow.View.ShowFieldCodes = False
  For Each Fld In .Range.Fields
    With Fld
      If .Type = wdFieldRef Then
        Set Rng = .Result: StrRef = Split(Trim(.Code.Text), " ")(1)
        Rng.MoveStart wdSentence, -1
        Rng.MoveEnd wdSentence, 1
        If Rng.Sentences.Count = 1 Then
          .Code.Text = "REF " & StrRef & " \* Lower \h"
        Else
          .Code.Text = "REF " & StrRef & " \* FirstCap \h"
        End If
      End If
    End With
  Next
  .Fields.Update
End With
Application.ScreenUpdating = True
End Sub

无论您采用哪种方法,您都会反对VBA对语法句子的无知。例如,请考虑以下内容:

MR。史密斯在约翰博士的杂货店花费了$ 1,234.56,购买:10.25公斤土豆; 10公斤鳄梨;和15.1公斤格林太太的宜人的澳洲坚果。

为您和我来说,这算是一句话对于VBA,它算作5 ...

For example:

Sub Demo()
Application.ScreenUpdating = False
Dim Fld As Field, Rng As Range, StrRef As String
With ActiveDocument
  .ActiveWindow.View.ShowFieldCodes = False
  For Each Fld In .Range.Fields
    With Fld
      If .Type = wdFieldRef Then
        Set Rng = .Result: StrRef = Split(Trim(.Code.Text), " ")(1)
        Rng.MoveStart wdSentence, -1
        Rng.MoveEnd wdSentence, 1
        If Rng.Sentences.Count = 1 Then
          .Code.Text = "REF " & StrRef & " \* Lower \h"
        Else
          .Code.Text = "REF " & StrRef & " \* FirstCap \h"
        End If
      End If
    End With
  Next
  .Fields.Update
End With
Application.ScreenUpdating = True
End Sub

Whichever approach you take, you'll run up against VBA's ignorance of what a grammatical sentence is. For example, consider the following:

Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.

For you and me, that would count as one sentence; for VBA it counts as 5...

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