Macroの杜(OpenOffice.org/LibreOffice Basic編)

Macroの記録 / Record Macro
(魔法のCommand 「 DispachHelper 」編 )

はじめに

LibreOfficeでは
「ツール」→「オプション」→「LibreOffice」→「全般」→「実験的(不安定)な機能を有効にする」にCheckを付ける必要があります。


「OpenOffice.orgのMacroは難しい」と思っている方の多くは「Macroの記録」で躓いたのではないでしょうか? 一例としてCalcにてA1セルに「OpenOffice.org」と入力した場合の「Macroの記録」は以下の様になります。
[ Macroの記録 ] sub Main rem ---------------------------------------------------------------------- rem define variables dim document as object dim dispatcher as object rem ---------------------------------------------------------------------- rem get access to the document document = ThisComponent.CurrentController.Frame dispatcher = createUnoService("com.sun.star.frame.DispatchHelper") rem ---------------------------------------------------------------------- dim args1(0) as new com.sun.star.beans.PropertyValue args1(0).Name = "ToPoint" args1(0).Value = "$A$1" dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1()) rem ---------------------------------------------------------------------- dim args2(0) as new com.sun.star.beans.PropertyValue args2(0).Name = "StringName" args2(0).Value = "OpenOffice.org" dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args2()) rem ---------------------------------------------------------------------- dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array()) end sub
上記作業を別の記述をすると以下の様になります。
[ 一例 ] Sub Main ThisComponent.Sheets(0).getCellByPosition(0,1).String="OpenOffice.org" End Sub
です。 なぜ、こんなにも違うのかと思う方もいるかもしれませんが、その理由はOpenOffice.orgのMacroの記録が
「 処置の忠実な記録 」
だからです。具体的には 1)作業を1手づつ区切って記録される。 2)コメント文で作業の説明が記される。
論より証拠ですので上記Macroの説明を以下に記します。
[ Macroの記録の説明 ] Macro名「Main」の始まり     : sub Main Comment             : rem ---------------------------------------------------------------------- Comment             : rem define variables 宣言文             :  dim document as object 宣言文             :  dim dispatcher as object Comment             : rem ---------------------------------------------------------------------- Comment             : rem get access to the document ActiveDocumentを使用      :  document = ThisComponent.CurrentController.Frame Uno Service DispatchHelper作成 :  dispatcher = createUnoService("com.sun.star.frame.DispatchHelper") Comment             : rem ---------------------------------------------------------------------- 作業内容の宣言文        :  dim args1(0) as new com.sun.star.beans.PropertyValue Cellの種類(1つのCell)      :   args1(0).Name = "ToPoint" Cellの場所           :   args1(0).Value = "$A$1" Active Cellの移動        :  dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1()) Comment             : rem ---------------------------------------------------------------------- 作業内容の宣言文        :  dim args2(0) as new com.sun.star.beans.PropertyValue Dataの種類           :   args2(0).Name = "StringName" Dataの値            :   args2(0).Value = "OpenOffice.org" 文字列を入力          :  dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args2()) Comment             : rem ---------------------------------------------------------------------- 次のCellへ移動         :  dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array()) Macroの終了           : end sub
作業そのものがStep By Stepで記録され、使っている主Commandは「DispatchHelper」のみです。 因みに別記したMacroの説明は以下です。
Active DocumentのSheet1のA1 Cellに文字列OpenOffice.orgを入力 => ThisComponent.Sheets(0).getCellByPosition(0,1).String="OpenOffice.org"
と文法を知らないと取っ付きづらいですね。
まさに「DispatchHelper」「 魔法のCommand 」ですが以下の欠点がありますので、 非常に簡単で少ない処置の再現処置には有効ですが少し複雑や処理量が多いものは「マクロの記録」のコードは不向きです。
[ 欠点 ] 1)コマンド(メニューに割り付けてある操作、UNOコマンドとも呼ばれるもの)のみの記録である。 2)コマンドで実現できるもの以外は記録できない。 3)Macroの実行時間が長くなる。










*******【 Index 】*******


【 General 】

Window

Help




【 Writer 】

Document

[ Cursor移動 ]

[ 削除 ]

[ 挿入 ]

[ 選択 ]

[ 編集 ]




【 Calc 】( Calc編参照 )

General


File


Edit


View


Insert


Format


Tools


Data


Window





【 Impress 】

Shape






*********** 【 Macro Code 】 ************



【 Writer 】

Document

[ Cursor移動 ]

DHW-)[Writer]Next Wordに移動

rem (2) Press Ctrl+Right Arrow to move the cursor to the start of “specifies”. dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())


DHW-)[Writer]Previous Wordに移動

rem (2) Press Ctrl+Left Arrow to move the cursor to the start of “specifies”. dispatcher.executeDispatch(document, ".uno:GoToPrevWord", "", 0, Array())


DHW-)[Writer]最終行へ移動

rem (9) Press End to move the cursor to the end of the line. dispatcher.executeDispatch(document, ".uno:GoToEndOfLine", "", 0, Array())


DHW-)[Writer]先頭行へ移動

rem (11) Press Home to move the cursor to the start of the line. dispatcher.executeDispatch(document, ".uno:GoToStartOfLine", "", 0, Array())


DHW-)[Writer]任意の次行へ移動

rem (16) Press down arrow to move to the next line. dim args19(1) as new com.sun.star.beans.PropertyValue args19(0).Name = "Count" args19(0).Value = 1 args19(1).Name = "Select" args19(1).Value = false dispatcher.executeDispatch(document, ".uno:GoDown", "", 0, args19())


DHW-)[Writer]任意の前行へ移動

Sub main Dim oDoc Dim dispatcher as object Dim Dummy() oDoc = StarDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, Dummy()) oDocument = oDoc.CurrentController.Frame oString = "ここはParagraph1 " & Chr$(13) & _ "ここはParagraph2 " & Chr$(13) &_ "ここはParagraph3 " & Chr$(13) &_ "ここはParagraph4 " oText = oDoc.getText() oText.insertString(oText.getEnd(), oString, false) oCur = oText.createTextCursor ' dispatcher = createUnoService("com.sun.star.frame.DispatchHelper") Dim args1(1) as new com.sun.star.beans.PropertyValue args1(0).Name = "Count" args1(0).Value = 2 args1(1).Name = "Select" args1(1).Value = false dispatcher.executeDispatch(oDocument, ".uno:GoUp", "", 0, args1()) End Sub


[ 削除 ]

DHW-)[Writer]BackSpace Key

rem (3) Press Backspace twice to remove the tab and the space. dispatcher.executeDispatch(document, ".uno:SwBackspace", "", 0, Array())


DHW-)[Writer]Delete key

rem (5) Press Delete to delete the lower case s .... dispatcher.executeDispatch(document, ".uno:Delete", "", 0, Array())


[ 挿入 ]

DHW-)[Writer]Text挿入

rem (4) Press Tab to add the tab without the space after the constant name. dim args4(0) as new com.sun.star.beans.PropertyValue args4(0).Name = "Text" args4(0).Value = CHR$(9) dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args4()) rem (5) ... and then press S to add an upper case S. dim args6(0) as new com.sun.star.beans.PropertyValue args6(0).Name = "Text" args6(0).Value = "S" dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args6())


[ 選択 ]

DHW-)[Writer]次の文字先頭までの範囲を選択

rem (7) Press Ctrl+Shift+Right Arrow to select the number. dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())


DHW-)[Writer]前の文字語尾までの範囲を選択

rem (7) Press Ctrl+Shift+Left Arrow to select the number. dispatcher.executeDispatch(document, ".uno:WordLeftSel", "", 0, Array())


[ 編集 ]

DHW-)[Writer]Copy

rem (8) Press Ctrl+C to copy the selected text to the clipboard. dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())


DHW-)[Writer]Paste

rem (12) Press Ctrl+V to paste the selected number to the start of the line. dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())







【 Impress 】

Shape

ISp-)[Writer]OutlinerShape形式選択する。

Sub oImpressShape Dim oDoc Dim dispatcher as object Dim Dummy() oDoc = StarDesktop.loadComponentFromURL("private:factory/simpress", "_blank", 0, Dummy()) oDocument = oDoc.CurrentController.Frame dispatcher = createUnoService("com.sun.star.frame.DispatchHelper") dispatcher.executeDispatch(oDocument, ".uno:SendOutlineToStarImpress", "", 0, Array()) End Sub






Top of Page

inserted by FC2 system