Word VBA:将文本字符串从段落末尾移动到段落开头 [英] Word VBA: Moving textstring from the end of a paragraph to the beginning of the paragraph

查看:237
本文介绍了Word VBA:将文本字符串从段落末尾移动到段落开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

例如:

<块引用>

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

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

应该是这样的:

<块引用>

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

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

我发现以下链接中的答案很有用,但似乎无法将其应用于我的案例:在找到txt的地方获取段落编号,并使用Word 2010 vba将文本移至段落末尾

非常感谢.

这是我目前所拥有的,但脚本似乎无法运行:

Sub Test1()Dim currDoc As Document设置 currDoc = ActiveDocumentDim docRng As Range、currRng As Range、strRng As Range设置 docRng = ActiveDocument.ContentDim currPara 作为段落Dim strText 作为字符串Selection.HomeKey Unit:=wdStory ' 从文档的开头开始.For Each currPara In docRng.Paragraphs ' 循环遍历活动文档中的段落.Set currRng = currDoc.Range(currPara.Range.Start, currPara.Range.End) ' 选择当前段落,以便逐段进行搜索.使用选择.查找.ClearFormatting.Text = "\(*\)".Replacement.Text = "".Forward = 真.Wrap = wdFindStop.Format = 假.MatchCase = 真.MatchWholeWord = 假.MatchAllWordForms = False.MatchSoundsLike = False.MatchWildcards = True结束于如果 currRng.Find.Execute 那么有选择.选择.切.StartOf 单位:=wdParagraph.粘贴.InsertAfter " "结束于万一下一个 currPara结束子

解决方案

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

Sub Test1_Tested_incl_Hyper()Dim currDoc As Document设置 currDoc = ActiveDocumentDim docRng As Range、currRng As Range、strRng As Range设置 docRng = ActiveDocument.ContentDim currPara 作为段落Dim strText 作为字符串Selection.HomeKey Unit:=wdStory ' 从文档的开头开始.For Each currPara In docRng.Paragraphs ' 循环遍历活动文档中的段落.Set currRng = currDoc.Range(currPara.Range.Start, currPara.Range.End) ' 选择当前段落,以便逐段进行搜索.currRng.Select使用选择.查找.ClearFormatting.Text = "\(".Replacement.Text = "".Forward = 真.Wrap = wdFindStop.Format = 假.MatchCase = 真.MatchWholeWord = 假.MatchAllWordForms = False.MatchSoundsLike = False.MatchWildcards = True.执行结束于如果 Selection.Find.Found 那么使用 currDoc.Range(Selection.Range.Start, currPara.Range.End - 1).选择.切.StartOf 单位:=wdParagraph.粘贴.InsertAfter " "结束于万一下一个 currPara结束子

编辑-页脚代码

Sub Test1_for_Footers()Dim currDoc As Document设置 currDoc = ActiveDocumentDim docRng As Range、currRng As Range、strRng As Range设置 docRng = ActiveDocument.StoryRanges(wdPrimaryFooterStory)Dim currPara 作为段落Dim strText 作为字符串对于 docRng.Paragraphs 中的每个 currParacurrPara.Range.Select使用选择.查找.ClearFormatting.Text = "\(".Replacement.Text = "".Forward = 真.Wrap = wdFindStop.Format = 假.MatchCase = 真.MatchWholeWord = 假.MatchAllWordForms = False.MatchSoundsLike = False.MatchWildcards = True.执行结束于如果 Selection.Find.Found 那么选择.扩展)"有选择.选择.切.StartOf 单位:=wdParagraph.粘贴.InsertAfter " "结束于万一下一个 currPara结束子

I'm new to VBA. I have several long documents where a citation or a document number appears at the end of a paragraph. Luckily, these citations and document are enclosed in parentheses, which should make it easy to isolate. I need to move the content of those parentheses (including the parentheses themselves) to the front of each paragraph and then add two spaces after the closing parenthesis.

For example:

This is my text in Paragraph 1. (http://nytimes.com)

This is my text in Paragraph 2. (1.b.3B)

Should look like:

(http://nytimes.com) This is my text in Paragraph 1.

(1.b.3B) This is my text in Paragraph 2.

I found the answer in the following link useful, but can't seem to apply it to my case: Get paragraph no where txt is found, and move text to end of paragraph using Word 2010 vba

Many thanks in advance.

Here's what I have up to now, but the script just doesn't seem to run:

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

解决方案

You were very close to correct solution to move simple text. But, what I realised, it was a problem to move hyperlinks as syntax "\(*\)" didn't recognise hyperlinks. Therefore I put some additional small modifications. That works for me in 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- code for footers

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

这篇关于Word VBA:将文本字符串从段落末尾移动到段落开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆