如何變更 系統預設印表機

如何變更 系統預設印表機

如何改變 系統預設印表機

 

方法 1 :

使用 WMI 來設定預設印表機

 

' 呼叫設定

SetDefaultPrinter "HP LaserJet 1100 (MS)" ' 設定預設引表機 , 傳入印表機名稱

 

Private Sub SetDefaultPrinter(strDevice As String)

    Const strCls = "Win32_Printer" ' WMI Class

    GetObject("winmgmts:").InstancesOf(strCls)(strCls & ".DeviceID=""" & strDevice & """").SetDefaultPrinter

End Sub

 

 

================================================================

 

 

方法 2 :

透過 WSH 物件的 NetWork 類別的 SetDefaultPrinter 方法來改變預設印表機

 

' 呼叫設定

CreateObject("WScript.Network").SetDefaultPrinter "HP LaserJet 1100 (MS)" ' 設定預設印表機,傳入印表機名稱

 

 

================================================================

 

 

方法 3 :

透過改變登錄檔的方法來改變預設印表機

 

' 宣告

Private Const HWND_BROADCAST = &HFFFF&

Private Const WM_WININICHANGE = &H1A

 

Private Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" _

    (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long

 

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _

    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

 

' 呼叫設定

SetDefPrinter "HP LaserJet 1100 (MS)" ' 設定預設引表機 , 傳入印表機名稱

 

Private Sub SetDefPrinter(ByVal PrinterName As String)

    Dim objP As Printer

    ' Printers 集合物件中列舉出所有的印表機

    For Each objP In Printers

        If StrComp(objP.DeviceName, PrinterName, 1) = 0 Then ' 找到欲設定的印表機

            ' DriverName Printer 的驅動程式名稱 , Port Printer 的連接埠的名稱

            SetPrinter PrinterName & "," & objP.DriverName & "," & objP.Port

            Exit For

        End If

    Next

    Set objP = Nothing

End Sub

 

Private Sub SetPrinter(Device As String)

    Dim hKey As Long

    Dim strKey As String

    ' 透過登錄檔的改寫 , 來改變預設印表機

    strKey = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"

    If Len(Environ("OS")) > 0 Then

        ' Win NT , 2K , XP 等系列 OS 平台

        CreateObject("WScript.Shell").RegWrite strKey, Device

    Else

        ' Win 98 系列 OS 平台

        WriteProfileString "windows", "device", Device

        ' Call API 傳送訊息 , 廣播登錄檔資訊變更

        SendMessage HWND_BROADCAST, WM_WININICHANGE, 32767&, ByVal "windows"

    End If

End Sub

 

 

================================================================

 

 

VB.Net 寫法  :

 

加入參考 System.Drawing.dll

Imports System.Drawing.Printing

 

' 設定應用程式預設印表機

Private Sub SetAppDefPrinter(ByVal strPrinterName As String)

    Dim pd As New PrintDocument ' 宣告並初始化 PrintDocument 類別的新執行個體

    pd.PrinterSettings.PrinterName = strPrinterName ' 設定應用程式輸出印表機

End Sub

 

' 設定系統預設印表機

Private Function SetSysDefPrinter(ByVal strPrinter As String) As Boolean

    Dim objWshNet As Object

    Dim pd As New PrintDocument ' 宣告並初始化 PrintDocument 類別的新執行個體

    Dim strOldPrinter As String

    Try

        objWshNet = CreateObject("WScript.Network") ' 建立 WSH 物件的 NetWork 類別

        strOldPrinter = pd.PrinterSettings.PrinterName ' 記錄舊的系統預設印表機名稱

        objWshNet.SetDefaultPrinter (strPrinter) ' 設定新的系統預設印表機

        pd.PrinterSettings.PrinterName = strPrinter ' 設定應用程式輸出印表機

        SetSysDefPrinter = pd.PrinterSettings.IsValid ' 判斷是否為有效印表機

        ' 若為無效印表機 , 則還原為舊的系統預設印表機

        If Not SetSysDefPrinter Then objWshNet.SetDefaultPrinter (strOldPrinter)

    Catch exc As Exception ' 錯誤掌控 , 例外處理

        objWshNet.SetDefaultPrinter (strOldPrinter) ' 還原舊的系統預設印表機

        Return False

    Finally

        ' 釋放物件

        objWshNet = Nothing

        pd = Nothing

    End Try

End Function

 

' 呼叫設定

SetSysDefPrinter ("HP LaserJet 1100 (MS)") ' 設定系統預設印表機

'

SetAppDefPrinter ("HP LaserJet 1100 (MS)") ' 設定應用程式預設印表機

 

 

================================================================

 

 

另一種方法 , Word Application 物件改變 [ 系統預設印表機 ]

 

Private Sub cmdSetDefPrinter_Click()

    SetDefaultPrinter "HP LaserJet 1100 (MS)"

End Sub

 

Private Sub SetDefaultPrinter(strPrinterName As String)

    With CreateObject("Word.Application")

        .ActivePrinter = strPrinterName

         On Error Resume Next

        .ActiveDocument.Close False

        .Quit

    End With

End Sub