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. Excel

    起動中のMicrosoft EdgeからタイトルとURLを取得するVBAマクロ(DOM編)

    前回の記事で、UI Automationを使って起動中のMicroso…

  2. Office関連

    変更履歴をオンにしたままで文字列の置換を行うWordマクロ

    MSDNフォーラムに、“変更履歴をオンにした状態で文字列を置換するマク…

  3. Office関連

    Office クリップボードをマクロで操作する(MSAA)

    MSDNフォーラムに質問がありましたが、Office クリップボードを…

  4. Office関連

    コンテンツコントロールに外部XMLのデータをマップするWordマクロ

    Word 2007で追加された機能「コンテンツコントロール」を使うと外…

  5. リボン関連

    複数のtoggleButton要素の中から1つだけしかオンにできないようにする(2)

    「複数のtoggleButton要素の中から1つだけしかオンにできない…

  6. Office関連

    2つの文書を比較するWordマクロ

    先日テキスト比較ソフトの「ちゃうちゃう!」がバージョンアップされたこと…

コメント

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

Time limit is exhausted. Please reload CAPTCHA.

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

Translate

最近の記事

アーカイブ

PAGE TOP