Office関連

ディスプレイのサイズを取得するVBAマクロ

「VBA ディスプレイ 幅 高さ」といったキーワード検索でのアクセスがありました。
マクロでディスプレイの解像度を取得する方法を探している方だろうと思います。

マクロでディスプレイの高さや幅を取得する場合、手軽なのはWMIの「Win32_DesktopMonitor」クラスや「Win32_VideoController」クラスを利用する方法です。

Public Sub Sample1()
'Win32_DesktopMonitorクラス利用
  Dim items As Object
  Dim item As Object

  Set items = CreateObject("WbemScripting.SWbemLocator") _
              .ConnectServer.ExecQuery("Select * From Win32_DesktopMonitor")
  For Each item In items
    Debug.Print "Name:" & item.Name, _
                "DeviceID:" & item.DeviceID, _
                "ScreenWidth:" & item.ScreenWidth, _
                "ScreenHeight:" & item.ScreenHeight
  Next
End Sub

Public Sub Sample2()
'Win32_VideoControllerクラス利用
  Dim items As Object
  Dim item As Object
  
  Set items = CreateObject("WbemScripting.SWbemLocator") _
              .ConnectServer.ExecQuery("Select * From Win32_VideoController")
  For Each item In items
    Debug.Print "Caption:" & item.Caption, _
                "DeviceID:" & item.DeviceID, _
                "CurrentHorizontalResolution:" & item.CurrentHorizontalResolution, _
                "CurrentVerticalResolution:" & item.CurrentVerticalResolution
  Next
End Sub

ただ、こちらの記事によると、正しくサイズを取得できない場合もあるようなので、PowerShell経由で.NETの「Screen.AllScreens」プロパティから値を取得するコードも考えてみました。

Public Sub Sample3()
'System.Windows.Forms.Screen.AllScreensプロパティ利用
  Dim cmd As String
  Dim ret As String
  Dim v As Variant, vv As Variant
  Dim i As Long
   
  'PowerShellの実行確認
  On Error Resume Next
  Shell "PowerShell -Version", vbHide
  If Err.Number <> 0 Then Exit Sub
  On Error GoTo 0
  
  cmd = "PowerShell -Command Add-Type -AssemblyName System.Windows.Forms;$str='';foreach($item in [Windows.Forms.Screen]::AllScreens){$str+=$item.DeviceName+','+$item.BitsPerPixel+','+$item.Primary+','+$item.Bounds.Width+','+$item.Bounds.Height+':'};$str=$str.Remove($str.Length-1,1);Write-Host $str;"
  ret = CreateObject("WScript.Shell").Exec(cmd).StdOut.ReadLine
  v = Split(ret, ":") 'PowerShellの実行結果を指定した区切り文字で分割
  For i = LBound(v) To UBound(v)
    vv = Split(v(i), ",")
    Debug.Print "DeviceName:" & vv(0), _
                "BitsPerPixel:" & vv(1), _
                "Primary:" & vv(2), _
                "Width:" & vv(3), _
                "Height:" & vv(4)
  Next
End Sub

PowerShellが実行できる環境が前提となりますが、WMIで上手くいかない場合は、こちらのコードもお試しください。

参考Webページ

PDFを他のファイル形式に変換するVBAマクロ前のページ

【まほうのルミティア】ルミティアパクトが発売開始されたよ。次のページ

関連記事

  1. Office関連

    プロダクトIDを取得するWordマクロ

    下記質問用に書いたコードです。「バージョン情報」で表示されるプロダ…

  2. アイコン一覧

    Office 365アイコン(imageMso)一覧(X,Y,Z)

    Office 365のデスクトップ版Officeアプリケーション(Wo…

  3. Office関連

    PowerPointスライドショー終了後ファイルを閉じるVBAマクロ

    「Excel VBA PowerPoint スライドショー後閉じる」と…

  4. Office関連

    ZIP形式で圧縮・解凍を行うVBAマクロ

    この記事のように、処理の中でZIP形式のファイルを扱うことはありました…

  5. Office関連

    アクティブなページ上のシェイプを一括でグループ化するWordマクロ

    Msdn フォーラムにあった質問「複数図形のグループ化」の回答用に書い…

  6. Office アドイン

    [Office用アプリ]法人登録する際の参考資料

    法人としてMicrosoft Seller DashboardでOff…

コメント

  • コメント (0)

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

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

Time limit is exhausted. Please reload CAPTCHA.

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

Translate

最近の記事

アーカイブ

PAGE TOP