Word VBA:将文本字符串从段落末尾移动到段落开头

Word VBA:将文本字符串从段落末尾移动到段落开头,vba,ms-word,Vba,Ms Word,我是VBA新手。我有几个长文档,其中引用或文档编号出现在段落末尾。幸运的是,这些引文和文档都用括号括起来了,这样就很容易分离出来。我需要将这些括号的内容(包括括号本身)移动到每个段落的前面,然后在结束括号后添加两个空格 例如: 这是我在第1段中的案文。() 这是我在第2段中的案文。(1.b.3B) 应该是这样的: ()这是我在第1段中的案文 (1.b.3B)这是我在第2段中的案文 我发现以下链接中的答案很有用,但似乎无法将其应用到我的案例中: 非常感谢 以下是我目前的情况,但脚本似乎无法运行:

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

例如:

这是我在第1段中的案文。()

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

应该是这样的:

()这是我在第1段中的案文

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

我发现以下链接中的答案很有用,但似乎无法将其应用到我的案例中:

非常感谢

以下是我目前的情况,但脚本似乎无法运行:

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

您非常接近移动简单文本的正确解决方案。但是,我意识到,移动超链接是一个问题,因为语法
“\(*\)”
无法识别超链接。因此,我做了一些额外的小修改。这在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
编辑-页脚代码

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

您是否可以在问题中添加文档中的任何一段来说明>>应该是什么。可以更容易帮助你,绝对可以。我在上面加了它。这太棒了!非常感谢。还有一个问题:脚本似乎没有进入脚注。这是因为文档设置吗?有没有办法让脚本也在脚注中运行?是的,没错<代码>活动文档。内容是文档的主要文本。要更改文档的其他部分,您需要参考其他故事范围,例如,
Set docRng=ActiveDocument.StoryRanges(wdPrimaryFooterStory)
。请参阅新的代码集。