Office関連

ページ番号を取得するWordマクロ

Wordマクロで選択位置のページ番号を取得する場合、簡単なのはSelectionオブジェクトのInformationプロパティを使う方法です。

Public Sub Sample01()
  With Selection
    MsgBox "指定した選択範囲または指定範囲の終了位置のページ番号:" & _
           .Information(wdActiveEndPageNumber) & vbNewLine & _
           "手動で変更したページ番号を反映:" & _
           .Information(wdActiveEndAdjustedPageNumber) & vbNewLine & _
           "総ページ数:" & _
           .Information(wdNumberOfPagesInDocument)
  End With
End Sub

Informationプロパティの引数(WdInformation)による違いは下記の通りで、状況に応じて使い分けをします。

  • wdActiveEndPageNumber:指定した選択範囲または指定範囲の終了位置のページ番号を取得します。
  • wdActiveEndAdjustedPageNumber:指定した選択範囲または指定範囲の終了位置のページ番号を取得します。セクションを区切って手動でページ番号を振り直した場合は、変更後のページ番号を取得します。
  • wdNumberOfPagesInDocument:総ページ数を取得します。

先日、MSDNフォーラムに「Word VBAで、カーソル位置とは関係なくステータスバーに表示されているページ番号を取得するにはどうすれば良いか?」という質問がありました。

たしかに、ステータスバーをよく見てみると、スクロール位置によっては微妙に上記マクロで取得できる番号とは異なるページ番号が表示されています。

この“ステータスバーに表示されているページ番号”の需要がどこまであるのかは分かりませんが、UIAutomationを使って無理やり取得するコードを考えてみました。

'UIAutomationClient(UIAutomationCore.dll)要参照
Option Explicit

Public Sub Sample02()
'ステータス バーからページ番号取得
'※「ステータス バー」と「ページ番号」が表示されていることが前提
  Dim uiAuto As UIAutomationClient.CUIAutomation
  Dim elm As UIAutomationClient.IUIAutomationElement
  Dim ary As IUIAutomationElementArray
  Dim acc As Office.IAccessible
  Dim num As String
  Dim v As Variant
  Dim i As Long
  
  'ステータス バーからIAccessible経由でIUIAutomationElement取得
  Set acc = Application.CommandBars("Status Bar")
  Set uiAuto = New UIAutomationClient.CUIAutomation
  Set elm = uiAuto.ElementFromIAccessible(acc, 0)
  
  'ステータス バー(NetUInetpane)取得
  Set elm = GetElement(uiAuto, elm, UIA_ClassNamePropertyId, "NetUInetpane")
  '子要素からCurrentNameに[ページ番号]が含まれるものを取得
  Set ary = elm.FindAll(TreeScope_Subtree, uiAuto.CreateTrueCondition)
  For i = 0 To ary.Length - 1
    num = ary.GetElement(i).CurrentName
    If InStr(num, "ページ番号") Then Exit For
  Next
  
  '取得したページ番号整形
  num = Replace(num, "ページ番号", "")
  num = Replace(num, "ページ", "")
  num = Trim(num)
  v = Split(num, "/")
  MsgBox "ページ番号:" & v(LBound(v)) & vbNewLine & _
         "総ページ数:" & v(UBound(v)), vbInformation + vbSystemModal
End Sub

Private Function GetElement(ByVal uiAuto As CUIAutomation, _
                            ByVal elmParent As IUIAutomationElement, _
                            ByVal propertyId As Long, _
                            ByVal propertyValue As Variant, _
                            Optional ByVal ctrlType As Long = 0) As IUIAutomationElement
  Dim cndFirst As IUIAutomationCondition
  Dim cndSecond As IUIAutomationCondition
    
  Set cndFirst = uiAuto.CreatePropertyCondition( _
                   propertyId, _
                   propertyValue _
                 )
  If ctrlType <> 0 Then
    Set cndSecond = uiAuto.CreatePropertyCondition( _
                      UIA_ControlTypePropertyId, _
                      ctrlType _
                    )
    Set cndFirst = uiAuto.CreateAndCondition( _
                     cndFirst, _
                     cndSecond _
                   )
  End If
  Set GetElement = elmParent.FindFirst(TreeScope_Subtree, cndFirst)
End Function

ステータスバーに「ページ番号」が表示されていることが前提となりますが、これで一応取得できるようになりました。

もしかしたら、もっと簡単な方法があるかもしれませんが、とりあえず今回はこれで良しとします。

2017年6月の人気記事前のページ

Outlook REST APIに会議室情報を取得するAPIが追加されました。次のページ

関連記事

  1. Office関連

    ClosedXMLやEPPlusでExcelファイルを読み書きしてみた。

    今回の記事の発端は下記のQiita投稿。・Excelファイ…

  2. Office関連

    [Office 2013]SkyDriveを無効(非表示)にする。

    「Office 2013 SkyDrive 無効」というキーワードで検…

  3. Office関連

    Data Explorerのフォーラム&ブログ

    前回の記事で紹介した「Data Explorer」ですが、すでにフォー…

  4. Office関連

    WordやExcelでミニ ツール バーを非表示(無効)にする。

    WordやExcel、PowerPointといったOffice製品で文…

  5. Office アドイン

    [Office用アプリ]Excel 2013の操作を動画で学べるアプリ「Excel video tu…

    Excel 2013の操作を動画で学べるアプリがMicrosoftから…

コメント

  • コメント (0)

  • トラックバックは利用できません。

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

Time limit is exhausted. Please reload CAPTCHA.

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

Translate

最近の記事

アーカイブ

PAGE TOP