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

    Word マクロ参考本の私的感想&評価まとめ

    私は趣味でWord マクロを書くことが多く、関連書籍も何冊か所持してい…

  2. Office関連

    文書内の単語を単語ごとにカウントするWordマクロ

    Wordsコレクションを使って文書内の単語を列挙し、各単語がそれぞれい…

  3. Office関連

    ExcelとPowerPointに自動保存機能が追加されました。

    Excel 2016を使っていて、ふと気が付いたのが画面左上にある「自…

  4. Office関連

    表示モードを変更するPowerPointマクロ

    PowerPointには様々な表示モードがありますが、私のお気に入りは…

  5. Office アドイン

    [Office用アプリ]User Agent他を調べてみました。

    ふと気になったので、Office 用アプリをローカル環境にインストール…

  6. Office関連

    A1セルを選択してから保存するExcelアドイン

    @yu_tang_さんのツイートで面白いものがありました。…

コメント

  • コメント (0)

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

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

Time limit is exhausted. Please reload CAPTCHA.

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

Translate

最近の記事

アーカイブ

PAGE TOP