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関連

    フッターにページ番号と総ページ数を挿入するWordマクロ

    以前書いた、フッターに「ページ番号 / 総ページ数」を挿入するWord…

  2. Office関連

    漢字かな交じり文をひらがなにするマクロ

    Yahoo!のテキスト解析Web API(ルビ振り)を使用して、漢字か…

  3. Office関連

    モヤさまのショウ君にいろいろ喋らせるVBAマクロ(2)

    前回に引き続き、HOYAサービス株式会社様が公開されている「Voice…

  4. Office関連

    類似した書式の文字列を選択するWordマクロ三種

    Wordには、選択中の文字列と似た書式の文字列を一括選択する「類似した…

コメント

  • コメント (0)

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

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

Time limit is exhausted. Please reload CAPTCHA.

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

Translate

最近の記事

アーカイブ

PAGE TOP