2

我是 VBA 新手。我有几个长文档,其中引文或文档编号出现在段落的末尾。幸运的是,这些引文和文件都用括号括起来,这应该很容易隔离。我需要将这些括号的内容(包括括号本身)移动到每个段落的前面,然后在右括号后添加两个空格。

例如:

这是我在第 1 段中的文字。(http://nytimes.com

这是我在第 2 段中的文字。(1.b.3B)

应该看起来像:

( http://nytimes.com ) 这是我在第 1 段中的文字。

(1.b.3B) 这是我在第 2 段中的文字。

我发现以下链接中的答案很有用,但似乎无法将其应用于我的案例:Get paragraph no where txt is found, and move text to end of paragraph using Word 2010 vba

提前谢谢了。

这是我到目前为止所拥有的,但脚本似乎没有运行:

Sub Test1()

Dim currDoc As Document
Set currDoc = ActiveDocument
Dim docRng As Range, currRng As Range, strRng As Range
Set docRng = ActiveDocument.Content
Dim currPara As Paragraph
Dim strText As String

Selection.HomeKey Unit:=wdStory ' Start from the beginning of the doc.

For Each currPara In docRng.Paragraphs ' Loop through the paragraphs in the active document.

Set currRng = currDoc.Range(currPara.Range.Start, currPara.Range.End) ' Selects the current paragraph, so that the search is conducted paragraph by paragraph.

With Selection.Find
        .ClearFormatting
        .Text = "\(*\)"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
End With

If currRng.Find.Execute Then

With Selection
    .Select
    .Cut
    .StartOf Unit:=wdParagraph
    .Paste
    .InsertAfter "  "
End With

End If

Next currPara

End Sub
4

1 回答 1

1

您非常接近移动简单文本的正确解决方案。但是,我意识到,移动超链接是个问题,因为语法"\(*\)"无法识别超链接。因此,我进行了一些额外的小修改。这在 Word 2010 中对我有用:

Sub Test1_Tested_incl_Hyper()

Dim currDoc As Document
Set currDoc = ActiveDocument
Dim docRng As Range, currRng As Range, strRng As Range
Set docRng = ActiveDocument.Content
Dim currPara As Paragraph
Dim strText As String

Selection.HomeKey Unit:=wdStory ' Start from the beginning of the doc.

For Each currPara In docRng.Paragraphs ' Loop through the paragraphs in the active document.

Set currRng = currDoc.Range(currPara.Range.Start, currPara.Range.End) ' Selects the current paragraph, so that the search is conducted paragraph by paragraph.
currRng.Select
With Selection.Find
    .ClearFormatting
    .Text = "\("
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    .Execute
End With

If Selection.Find.Found Then

With currDoc.Range(Selection.Range.Start, currPara.Range.End - 1)
    .Select
    .Cut
    .StartOf Unit:=wdParagraph
    .Paste
    .InsertAfter "  "
End With

End If

Next currPara

End Sub

EDIT-页脚代码

Sub Test1_for_Footers()

Dim currDoc As Document
Set currDoc = ActiveDocument
Dim docRng As Range, currRng As Range, strRng As Range
Set docRng = ActiveDocument.StoryRanges(wdPrimaryFooterStory)
Dim currPara As Paragraph
Dim strText As String

For Each currPara In docRng.Paragraphs

currPara.Range.Select
With Selection.Find
    .ClearFormatting
    .Text = "\("
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    .Execute
End With

If Selection.Find.Found Then
Selection.Extend ")"

With Selection
    .Select
    .Cut
    .StartOf Unit:=wdParagraph
    .Paste
    .InsertAfter "  "
End With

End If

Next currPara

End Sub
于 2013-04-10T22:13:25.697 回答