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. Windows 10

    SeleniumBasic(Selenium VBA)がMicrosoft Edgeに対応しました。…

    言わずと知れたWebブラウザーの自動制御ツール「Selenium」のV…

  2. Office関連

    PHPWordを使ってPHPからWordファイルを出力してみる。

    最近オトカドールやルミティアジュエルやらの記事ばかり書いていますが、今…

  3. Office関連

    [Office]アイコンの検索機能が超便利!

    ※ 下記情報はInsider版のOfficeを元にしています。バージョ…

  4. Office関連

    古い形式のWordテンプレートを新しい形式に一括変換するVBScript

    古い形式のWordテンプレート(dot)を新しい形式(dotx,dot…

  5. Office関連

    右クリックから図形の配置 for Office 2013

    HPの掲示板に"右クリックから「配置」を実行できないか?"という質問が…

コメント

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

Time limit is exhausted. Please reload CAPTCHA.

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

Translate

最近の記事

アーカイブ

PAGE TOP