画像の囲み線を消すマクロ(Word)

画像の囲み線を消すマクロ(Word)

f:id:akashi_keirin:20210306183826j:plain

前回

akashi-keirin.hatenablog.com

最後のところで、

逆に、画像に施した枠線を除去するマクロの書き方がわからない。

誰か知っている人がいたら教えろ教えてください。

などと言っていたが、とりあえずやり方はわかった。

画像の枠線を消すマクロ

InlineShapeオブジェクトのLineプロパティ

InlineShapeオブジェクトには、Lineプロパティというものがあるらしい。

『Word 2013 developer docs』(Word2013のオフライン・ヘルプ)によると、

InlineShape.Line Property (Word)

Returns a LineFormat object that contains line formatting properties for the specified shape. Read-only.

Syntax

expression .Line

expression A variable that represents an InlineShape object.

ということらしい。

Lineプロパティという名前だが、Lineオブジェクトを返すわけではなく、あくまでもLineFormatオブジェクトを返す、ということ。

……となると、LineFormatオブジェクトについても調べておかざるを得ない。

LineFormatオブジェクトとは

これまた、『Word 2013 developer docs』によると、

LineFormat Object (Word)

Represents line and arrowhead formatting. For a line, the LineFormat object contains formatting information for the line itself; for a shape with a border, this object contains formatting information for the shape's border.

とのこと。

「a shape with a border」というのは、まさにドキュメント内に挿入した画像のことなので、こいつに含まれている「formatting information for the shape's border」をいじくってやれば、囲み線を消すことができるはずだ。

ちなみに、LineFormatオブジェクトのメンバは、

f:id:akashi_keirin:20210306183542j:plain

この通り。めっちゃ多い。

境界線を消すには

境界線を消すためには、LineFormatオブジェクトのVisibleプロパティをどうにかしたらよさげ。

みたび『Word 2013 developer docs』によると、

LineFormat.Visible Property (Word)

True if the specified object, or the formatting applied to it, is visible. Read/write MsoTriState.

とのこと。

単にTrue / Falseの二値なのではなく、MsoTriStateという列挙体で設定するらしい。

『オブジェクト ブラウザー』で調べてみると、MsoTriState列挙体のメンバは、

  • msoCTrue
  • msoFalse
  • msoTriStateMixed
  • msoTriStateToggle
  • msoTrue

となっている。

なんで、「Tri」なのに五つもの状態が決められているのかよくわからん(三つじゃねえのかよ。)し、「CTrue」っていうのもなんのことかようわからん。

それでも、VisibleプロパティをmsoFalseにしたら、囲み線を非表示にすることはできそうだ。

画像の囲み線を消すマクロのコード

……というわけで、コードをば。

リスト1
Public Sub RemoveBordersOfFigure()
  Dim ilShp As InlineShape
  If Selection.Type = wdSelectionInlineShape Then
    Set ilShp = Selection.InlineShapes(1)
  Else
    Exit Sub
  End If
  ilShp.Line.Visible = msoFalse
End Sub

ふむ。まっ たく 簡 単 だ

使ってみる

f:id:akashi_keirin:20210306183548j:plain

この状態で、画像を選択して、リスト1を実行する。

ちなみに、「図の書式設定」は、

f:id:akashi_keirin:20210306183552j:plain

この状態。

f:id:akashi_keirin:20210306183557g:plain

ほれ、この通り。囲み線が消えている。

「図の書式設定」も

f:id:akashi_keirin:20210306183610j:plain

万全。バッチリ。

おわりに

これで、画像入りのWordドキュメントの作成がはかどることでしょう。

めでたしめでたし。