LoginSignup
0
0

More than 5 years have passed since last update.

[Publisher VBA] コンマを全消しするマクロ (Replaceの使用例)

Posted at

Publisher VBA 全世界初数字をコンマ付き数字に変えるマクロができたので、こんどはコンマを全消しするマクロを作りました。
これは全消しだけでなく、変換もできます。


Sub pbDeleteCommas()
'For Publisher VBA
Dim pbDoc As Publisher.Document
Dim sh As Publisher.Shape, shs As Publisher.Shapes
Dim pbPage As Publisher.Page, pbPages As Publisher.Pages
Dim tFrame As Publisher.TextFrame, tRng As Publisher.TextRange
Dim FoundRng As TextRange
Set pbDoc = ActiveDocument 'アクティブドキュメントをセット
Set pbPages = pbDoc.Pages 'ページのコレクションをセット
For Each pbPage In pbPages 'ページごとに処理をする
Set shs = pbPage.Shapes 'ページのシェイプコレクションをセット
For Each sh In shs 'シェイプごとに処理をする
If sh.HasTextFrame Then 'シェイプがテキストフレームを持っているか、シェイプがテキストボックスだったら
Set tFrame = sh.TextFrame 'テキストフレームをセット
Set tRng = tFrame.TextRange 'テキストレンジをセット
With tRng.Find 'With tRng.Find の書き方がポイント
.Clear
.FindText = "," '検索文字
.ReplaceWithText = "" '置換文字
.ReplaceScope = pbReplaceScopeAll
.Forward = True
.Execute 'Clear と Executeで必ず挟む
End With
End If
Next
Next
End Sub

ポイント

  • PublisherのReplaceだけを使用しているので、汎用性が高い
  • 全消しになる理由はReplaceScopeの変数
  • Forwardは順方向にするか逆方向にするか
  • ワイルドカード、正規表現は使えない
  • テキストレンジが全文になっているため一部だけ適用したいときはテキストレンジをさらに行単位、単語単位、文字単位で絞り込んだテキストレンジを作ってテキストレンジファインドに持ち込む。
  • すべてのページのすべてのシェイプのすべてのテキストボックスにアクセスするのはこちらが確実かも。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0