2

私はVBAが初めてです。パラグラフの最後に引用または文書番号が表示される長い文書がいくつかあります。幸いなことに、これらの引用とドキュメントは括弧で囲まれているため、簡単に分離できます。これらの括弧の内容 (括弧自体を含む) を各段落の先頭に移動し、閉じ括弧の後に 2 つのスペースを追加する必要があります。

例えば:

これは段落 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 に答える