Microsoft Word に関する情報

ワイルドカード(正規表現)を使った検索と置換

<アルファベットに続くカンマの置換>
検索:([A-Z]),
置換:\1,

<数字に続くピリオドの置換>
検索:([0-9]).
置換:\1.
<英字、数字に続く読点、カンマを半角カンマに置換>
検索:([A-ZA-Z0-90-9])([、,])
置換:\1,

<英字、数字に続く句点、ピリオドを半角ピリオドに置換>
検索:([A-ZA-Z0-90-9])([。.])
置換:\1.
<変換前>
あいうえお、かきくけこ。さしすせそ,たちつてと.
ABC、DEF。GHI,JKL.
ABC、DEF。GHI,JKL.
123、456。789,012.
123、456。789,012.

<変換後>
あいうえお、かきくけこ。さしすせそ,たちつてと.
ABC,DEF.GHI,JKL.
ABC,DEF.GHI,JKL.
123,456.789,012.
123,456.789,012.
<参考情報>
Word 2002 のヘルプより。
-----------------------------------------------
[Microsoft Word のヘルプ]
 +[文書の基本概念]
  +[検索と置換]
   +[文字列やその他の項目を検索して置換する]
-----------------------------------------------
で「すべて表示」を選択。

サンプルコード

ページ単位でファイルに保存するマクロ

' ページ単位でファイルに保存するマクロ
Sub PageToDoc()
    Dim strFilePath
    Dim strPageList
    Dim strPageArray
    Dim strPageFromTo
    Dim nPageFrom
    Dim nPageTo
    
    strFilePath = "D:\home\edu\word\test\page_" ' 保存先
    strPageList = "1,2-3,4,5-6,7"   ' ページ数の指定。
    strPageArray = Split(strPageList, ",")
    For Each strPageFromTo In strPageArray
        GetPageFromTo strPageFromTo, nPageFrom, nPageTo
        Debug.Print "nPageFrom = " & nPageFrom & ", " & "nPageTo = " & nPageTo
        SaveAsPageFromTo strFilePath, nPageFrom, nPageTo
    Next
    
End Sub

' ページの開始と終了を求める
Function GetPageFromTo(ByVal strPageFromTo, ByRef nPageFrom, ByRef nPageTo)
    Dim strPageArray
    strPageArray = Split(strPageFromTo, "-")
    
    nPageFrom = -1
    nPageTo = -1
    
    ' 要素数を求める
    nCount = UBound(strPageArray, 1) - LBound(strPageArray, 1) + 1
    If nCount = 1 Then
        nPageFrom = CInt(strPageArray(0))
        nPageTo = strPageArray(0)
    ElseIf nCount = 2 Then
        nPageFrom = CInt(strPageArray(0))
        nPageTo = strPageArray(1)
    End If
    
End Function

' 指定ページをファイルに保存
Function SaveAsPageFromTo(ByVal strFilePath, ByVal nPageFrom, ByVal nPageTo)
    For i = nPageFrom To nPageTo
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=i ' 指定ページに移動
        Selection.GoTo What:=wdGoToBookmark, Name:="\page"          ' ページを選択
        Selection.Copy                                              ' ページをコピー
        If i <> nPageFrom Then
            Documents.Open strFilePath & nPageFrom                  ' 既存の文書をオープン
            Selection.EndKey Unit:=wdStory                          ' 文書の最後に移動
        Else
            Documents.Add DocumentType:=wdNewBlankDocument          ' 新規に作成
        End If
        Selection.PasteAndFormat (wdPasteDefault)                   ' ページを貼り付け
        Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend ' 選択を解除
        If i <> nPageFrom Then
            ActiveDocument.SaveAs FileName:=strFilePath & nPageFrom ' 既存の文書を更新
        Else
            ActiveDocument.SaveAs FileName:=strFilePath & i         ' 新規に保存
        End If
        ActiveWindow.Close
    Next i
End Function

リンク集

はてな


#access


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS