Word

リボンからプリンタを選択して簡単に印刷できるようにする(Word)

今回はdynamicMenu要素のgetContent属性のコールバックを利用して、リボンからプリンタを選択して簡単に印刷できるようにする方法を紹介します(Word)。

[リボンXML]

<?xml version="1.0" encoding="utf-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
  <ribbon>
    <tabs>
      <tab idMso="TabHome">
        <group id="grpPrint" label="印刷" insertBeforeMso="GroupClipboard">
          <dynamicMenu id="dmuPrint" label="印刷  " size="large" imageMso="FilePrint" screentip="プリンター選択" supertip="ここをクリックして印刷するプリンターを選択してください。" getContent="dmuPrint_getContent" />
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

[標準モジュール]

Option Explicit

Private Sub dmuPrint_getContent(control As IRibbonControl, ByRef returnedVal)
  Dim d As Object
  Dim elmMenu As Object
  Dim elmButton As Object
  Dim itm As Object
  Dim i As Long
  
  i = 1
  Set d = CreateObject("Msxml2.DOMDocument")
  Set elmMenu = d.createElement("menu")
  elmMenu.setAttribute "xmlns", "http://schemas.microsoft.com/office/2006/01/customui"
  elmMenu.setAttribute "itemSize", "large"
  With CreateObject("Shell.Application")
    For Each itm In .Namespace(4).Items
      Set elmButton = d.createElement("button")
      elmButton.setAttribute "id", "btnPrinter" & i
      elmButton.setAttribute "label", itm.Name
      elmButton.setAttribute "tag", itm.Name
      elmButton.setAttribute "imageMso", "FilePrint"
      elmButton.setAttribute "onAction", "btnPrinter_onAction"
      elmMenu.appendChild elmButton
      Set elmButton = Nothing
      i = i + 1
    Next
  End With
  d.appendChild elmMenu
  returnedVal = d.XML
  Set elmMenu = Nothing
  Set d = Nothing
End Sub

Private Sub btnPrinter_onAction(control As IRibbonControl)
  Dim tmp As String
  
  tmp = Application.ActivePrinter
  Application.ActivePrinter = control.Tag
  Application.Dialogs(wdDialogFilePrint).Show
  Application.ActivePrinter = tmp
End Sub

上記コードを設定したファイルを開くと、「ホーム」タブに「印刷」グループが追加されます。

印刷」メニューからプリンタを選択することで、プリンタを選択した状態で印刷ダイアログを表示することができます。

McAf.eeで短縮URLを取得するブックマークレット前のページ

まばたきするリボン次のページ

関連記事

  1. Office関連

    アラビア文字かどうかを判別するWordマクロ

    以前mougの質問用に書いたコードが出てきたので、一部修正しました。…

  2. Office関連

    クイックアクセスツールバーから履歴を表示するWordテンプレート

    Word MVPの新田さんのブログで「【Word 2013】クイックア…

  3. Office関連

    テスト用の文字列を挿入するWordマクロ

    文字列操作を行うマクロを書いているとき、テスト用に「あいうえおかきくけ…

  4. Office関連

    ファイルをBase64エンコード・デコードするVBAマクロ

    以前書いたファイルのBase64エンコード・デコード処理を行うVBAマ…

  5. Office関連

    空白文字を一括置換するWordマクロ

    様々なWord文書を扱っていると、下図のように“同じ空白のように見えて…

  6. Office関連

    テキストボックス等Shapeオブジェクトのテキストのみを置換するWordマクロ

    Word MVPの新田さんのブログで気になる記事がありました。…

コメント

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP