【Wordマクロ】クオーテーションを一括で変換する | みんなのワードマクロ

みんなのワードマクロ

ワードマクロで、文書作成とオフィス事務を効率化!!

先日の記事で「【Word】クオーテーションを一括で変換する『オートフォーマット』 」を紹介しました。

やはり、Wordマクロでもう少し簡単に実行したいですよね。

「一括オートフォーマット」を利用するたびに、[オートフォーマット]タブの設定を変更したりするのは面倒ですし。

そこでマクロにしました。

このマクロでは、[オートフォーマット]タブの設定にかかわらず、クオーテーションの表記だけを変換します。


▼このマクロでできること

処理対象の範囲を選択してマクロを実行すると、選択範囲内のクオーテーションを一括で変換します。

変換のルールについては、前回の記事「【Word】クオーテーションを一括で変換する『オートフォーマット』 」をご覧ください。


▼マクロの解説

非常にシンプルなマクロなのですが、記述は長くなってしまいました。

考え方としては、

①[オートフォーマット]タブの現状の設定を一時保存

②[オートフォーマット]タブの設定を変更→クオーテーションの表記変換だけをオンにする

③一括オートフォーマットの実行

④元の設定に戻す

です。


上記の①で設定項目を一時保管して置く必要があるので、変数を16種類用意しました。


赤文字の部分で、一括オートフォーマットを実行しています。

このマクロでは、選択範囲に対して実施しています。

 Selection.Range.AutoFormat


クオーテーションの変換以外にも、必要な項目をオン(True)にすれば同時に実行できます。

ハイパーリンクへの変換などもすることもできるので、必要に応じて設定してみてください。


▼マクロ

Sub クオーテーョンの変換マクロ()

 '自動で適切なスタイルを設定する個所
 Dim blnAutoFormatApplyHeadings '規定の見出しスタイル
 Dim blnAutoFormatApplyLists '箇条書き(行頭文字)
 Dim blnAutoFormatApplyBulletedLists 'リストのスタイル
 Dim blnAutoFormatApplyOtherParas 'その他の段落スタイル

 '自動で変更する項目
 Dim blnAutoFormatReplaceQuotes ''’を’’に変更する
 Dim blnAutoFormatReplaceOrdinals '序数を上付き文字に変更する
 Dim blnAutoFormatReplaceFractions '分数を分数文字(組み文字)に変更する
 Dim blnAutoFormatReplaceSymbols 'ハイフンをダッシュに変更する
 Dim blnAutoFormatReplacePlainTextEmphasis ''*'、'_'で囲んだ文字列を太字、斜体に書式設定する
 Dim blnAutoFormatReplaceHyperlinks 'インターネットとネットワークのアドレスをハイパーリンクに変更する
 Dim blnAutoFormatApplyFirstIndents '行の始まりのスペースを字下げに変更する
 Dim blnAutoFormatMatchParentheses 'かっこを正しく組み合わせる
 Dim blnAutoFormatDeleteAutoSpaces '日本語と英数字の間の不要なスペースを削除する
 Dim blnAutoFormatReplaceFarEastDashes '長音とダッシュを正しく使い分ける

 '変更しない項目
 Dim blnAutoFormatPreserveStyles 'スタイル

 '常に自動で書式設定する項目
 Dim blnAutoFormatPlainTextWordMail '電子メール経由で受信したメッセージ


 '------------------------------------------
 '実行前の確認
 '------------------------------------------
 If Selection.Type = wdSelectionIP Then
  MsgBox "範囲を選択してください。"
  Exit Sub
 End If

 '------------------------------------------
 '現在の設定の保存
 '------------------------------------------
 With Application.Options
  '自動で適切なスタイルを設定する個所
  blnAutoFormatApplyHeadings = .AutoFormatApplyHeadings
  blnAutoFormatApplyLists = .AutoFormatApplyLists
  blnAutoFormatApplyBulletedLists = .AutoFormatApplyBulletedLists
  blnAutoFormatApplyOtherParas = .AutoFormatApplyOtherParas

  '自動で変更する項目
  blnAutoFormatReplaceQuotes = .AutoFormatReplaceQuotes
  blnAutoFormatReplaceOrdinals = .AutoFormatReplaceOrdinals
  blnAutoFormatReplaceFractions = .AutoFormatReplaceFractions
  blnAutoFormatReplaceSymbols = .AutoFormatReplaceSymbols
  blnAutoFormatReplacePlainTextEmphasis = .AutoFormatReplacePlainTextEmphasis
  blnAutoFormatReplaceHyperlinks = .AutoFormatReplaceHyperlinks
  blnAutoFormatApplyFirstIndents = .AutoFormatApplyFirstIndents
  blnAutoFormatMatchParentheses = .AutoFormatMatchParentheses
  blnAutoFormatDeleteAutoSpaces = .AutoFormatDeleteAutoSpaces
  blnAutoFormatReplaceFarEastDashes = .AutoFormatReplaceFarEastDashes

  '変更しない項目
  blnAutoFormatPreserveStyles = .AutoFormatPreserveStyles
 
  '常に自動で書式設定する項目
  blnAutoFormatPlainTextWordMail = .AutoFormatPlainTextWordMail
 End With

 '------------------------------------------
 '設定の変更
 '------------------------------------------
 With Application.Options
  '自動で適切なスタイルを設定する個所
  .AutoFormatApplyHeadings = False '規定の見出しスタイル
  .AutoFormatApplyLists = False '箇条書き(行頭文字)
  .AutoFormatApplyBulletedLists = False 'リストのスタイル
  .AutoFormatApplyOtherParas = False 'その他の段落スタイル

  '自動で変更する項目
  .AutoFormatReplaceQuotes = True ''’を’’に変更する
  .AutoFormatReplaceOrdinals = False '序数を上付き文字に変更する
  .AutoFormatReplaceFractions = False '分数を分数文字(組み文字)に変更する
  .AutoFormatReplaceSymbols = False 'ハイフンをダッシュに変更する
  .AutoFormatReplacePlainTextEmphasis = False ''*'、'_'で囲んだ文字列を太字、斜体に書式設定する
  .AutoFormatReplaceHyperlinks = False 'インターネットとネットワークのアドレスをハイパーリンクに変更する
  .AutoFormatApplyFirstIndents = False '行の始まりのスペースを字下げに変更する
  .AutoFormatMatchParentheses = False 'かっこを正しく組み合わせる
  .AutoFormatDeleteAutoSpaces = False '日本語と英数字の間の不要なスペースを削除する
  .AutoFormatReplaceFarEastDashes = False '長音とダッシュを正しく使い分ける

  '変更しない項目
  .AutoFormatPreserveStyles = False
 
  '常に自動で書式設定する項目
  .AutoFormatPlainTextWordMail = False
 End With

 '------------------------------------------
 '選択範囲に対してオートフォーマットを実行
 '------------------------------------------
 Selection.Range.AutoFormat

 '------------------------------------------
 '設定を元に戻す
 '------------------------------------------
 With Application.Options
  '自動で適切なスタイルを設定する個所
  .AutoFormatApplyHeadings = blnAutoFormatApplyHeadings
  .AutoFormatApplyLists = blnAutoFormatApplyLists
  .AutoFormatApplyBulletedLists = blnAutoFormatApplyBulletedLists
  .AutoFormatApplyOtherParas = blnAutoFormatApplyOtherParas

  '自動で変更する項目
  .AutoFormatReplaceQuotes = blnAutoFormatReplaceQuotes
  .AutoFormatReplaceOrdinals = blnAutoFormatReplaceOrdinals
  .AutoFormatReplaceFractions = blnAutoFormatReplaceFractions
  .AutoFormatReplaceSymbols = blnAutoFormatReplaceSymbols
  .AutoFormatReplacePlainTextEmphasis = blnAutoFormatReplacePlainTextEmphasis
  .AutoFormatReplaceHyperlinks = blnAutoFormatReplaceHyperlinks
  .AutoFormatApplyFirstIndents = blnAutoFormatApplyFirstIndents
  .AutoFormatMatchParentheses = blnAutoFormatMatchParentheses
  .AutoFormatDeleteAutoSpaces = blnAutoFormatDeleteAutoSpaces
  .AutoFormatReplaceFarEastDashes = blnAutoFormatReplaceFarEastDashes

  '変更しない項目
  .AutoFormatPreserveStyles = blnAutoFormatPreserveStyles
 
  '常に自動で書式設定する項目
  .AutoFormatPlainTextWordMail = blnAutoFormatPlainTextWordMail
 End With

End Sub



▼関連記事

【Word】クオーテーションを一括で変換する『オートフォーマット』