LibreOffice Calc Basic - アクティブなプリンターを取得する方法

◆Excelの場合

 Excelでは、ApplicationオブジェクトのActivePrinterプロパティを使用すると、現在使用中のプリンタの名前を取得する事ができます。
プリンタ名の後に「on Ne01:」のようにポート番号も表示されます。
 なお、Applicationオブジェクトは、Excelそのものを表します。

サンプルコード(Excel)
 rem ***********************************************************
Private Sub ShowCurrentPrinterName()
'現在使用しているプリンタ名をメッセージボックスで表示
 rem ***********************************************************
    MsgBox "現在使用しているプリンターは、" & _
    Application.ActivePrinter _
    & " です。", Title:="現在使用しているプリンターの表示"
End Sub
実行結果

◆LibreOffice Basicの場合

com.sun.star.beans インターフェースのPropertyValueプロパティ値の配列を取得し、PropertyValueを見つけるこどで現在アクティブなプリンターを取得することができます。

サンプルコード(LibreOffice)
rem ***********************************************************
 rem  FindProperty関数を使って、MsgBoxにアクティブなプリンターを表示します。
 rem ***********************************************************
Sub GetCurrentPrinterName
    dim oDoc as object
    dim aProperties as object
    dim oPrinterName as object
    dim ret
    
     ' 現在のドキュメントのモデルを取得
     oDoc = ThisComponent

      ' プリンタのロパティの配列を取得します。
      aProperties = oDoc.getPrinter()

      ' Name プロパティを検索
      oPrinterName = FindProperty( aProperties, "Name" )

      If IsNull( oPrinterName ) Then
         MsgBox( "プリンターが見つかりません。" )
      Else
         ret=MsgBox( "現在使用しているプリターは " & _
         oPrinterName.Value & "です。",0,"現在使用しているプリンターの表示")
      EndIf
 End Sub
 
 rem ***********************************************************
' プロパティの配列がから特定の名前付きプロパティを見つけます。
' パラメータ:
' aArrayOfProperties   -   com.sun.star.beans.PropertyValueプロパティ値の配列
' cPropName   -  プロパティ名
' PropertyValueを見つけ、それを返します。見つからない場合はnullを返します。
rem ************************************************************
Function FindProperty( aArrayOfProperties, cPropName As String ) _
    As com.sun.star.beans.PropertyValue
  
  dim i%
  dim oProp
  
  For i = LBound( aArrayOfProperties ) To UBound( aArrayOfProperties )
     oProp = aArrayOfProperties(i)
     If oProp.Name = cPropName Then
        FindProperty() = oProp
        Exit Function
     EndIf
  Next
End Function
実行結果
LibreOfficeのMsgBox

▼ページトップへ