Xin hỏi về thiết lập Print one side only (1 người xem)

  • Thread starter Thread starter kyo
  • Ngày gửi Ngày gửi
Liên hệ QC

Người dùng đang xem chủ đề này

kyo

Nguyễn Khắc Duy
Thành viên danh dự
Tham gia
4/6/06
Bài viết
901
Được thích
2,717
kyo có một vấn đề thắc mắc rất mong được mọi người giúp đỡ về vấn đề làm cách nào để thiết lập Print one side only (nghĩa là in một mặt giấy) bằng VBA đối với máy in có khả năng in hai mặt và đã được thiết lập mặc định là in hai mặt. Vì đây là option của máy in nên không thể record macro, và kyo có search thử thì hình như sử dụng API.

Xin chân thành cảm ơn mọi người,
kyo.
 
Cám ơn bạn rất nhiều nhưng phương pháp này kyo xài không được. Và kyo có tham khảo qua các diễn đàn nước ngoài thì được biết cách trên chỉ có thể ứng dụng trên VB, còn VBA thì không dùng được. Không biết là còn cách nào khác không?
 
Upvote 0
Cám ơn bạn rất nhiều nhưng phương pháp này kyo xài không được. Và kyo có tham khảo qua các diễn đàn nước ngoài thì được biết cách trên chỉ có thể ứng dụng trên VB, còn VBA thì không dùng được. Không biết là còn cách nào khác không?
Mình không có máy in để test. Khi bạn test chương trình báo lỗi cú pháp hay chạy mà không ra kết quả? Trong bài đó sử dụng object printer không có trong VBA. Để lấy name của default printer có thể đọc trực tiếp hoặc dùng API. Sau đây là code mình đã bổ sung hàm DefaultPrinterName, bạn thử xem được không, máy mình thì chạy được ra tên máy in nhưng không biết in thế nào.
Mã:
Option Explicit


Public Type PRINTER_DEFAULTS


       pDatatype As Long
       pDevmode As Long
       DesiredAccess As Long
   End Type


   Public Type PRINTER_INFO_2
       pServerName As Long
       pPrinterName As Long
       pShareName As Long
       pPortName As Long
       pDriverName As Long
       pComment As Long
       pLocation As Long
       pDevmode As Long       ' Pointer to DEVMODE
       pSepFile As Long
       pPrintProcessor As Long
       pDatatype As Long
       pParameters As Long
       pSecurityDescriptor As Long  ' Pointer to SECURITY_DESCRIPTOR
       Attributes As Long




       Priority As Long
       DefaultPriority As Long
       StartTime As Long
       UntilTime As Long
       Status As Long
       cJobs As Long
       AveragePPM As Long
   End Type


   Public Type DEVMODE
       dmDeviceName As String * 32


       dmSpecVersion As Integer
       dmDriverVersion As Integer
       dmSize As Integer
       dmDriverExtra As Integer
       dmFields As Long
       dmOrientation As Integer
       dmPaperSize As Integer
       dmPaperLength As Integer
       dmPaperWidth As Integer
       dmScale As Integer
       dmCopies As Integer
       dmDefaultSource As Integer
       dmPrintQuality As Integer
       dmColor As Integer
       dmDuplex As Integer
       dmYResolution As Integer
       dmTTOption As Integer
       dmCollate As Integer
       dmFormName As String * 32
       dmUnusedPadding As Integer
       dmBitsPerPel As Integer
       dmPelsWidth As Long
       dmPelsHeight As Long
       dmDisplayFlags As Long
       dmDisplayFrequency As Long
       dmICMMethod As Long
       dmICMIntent As Long
       dmMediaType As Long
       dmDitherType As Long
       dmReserved1 As Long
       dmReserved2 As Long
   End Type


   Public Const DM_DUPLEX = &H1000&
   Public Const DM_IN_BUFFER = 8


   Public Const DM_OUT_BUFFER = 2
   Public Const PRINTER_ACCESS_ADMINISTER = &H4
   Public Const PRINTER_ACCESS_USE = &H8
   Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
   Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)


Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Public Declare Function DocumentProperties Lib "winspool.drv" Alias "DocumentPropertiesA" (ByVal hwnd As Long, ByVal hPrinter As Long, ByVal pDeviceName As String, ByVal pDevModeOutput As Long, ByVal pDevModeInput As Long, ByVal fMode As Long) As Long
Public Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Byte, ByVal cbBuf As Long, pcbNeeded As Long) As Long
Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long
Public Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Byte, ByVal Command As Long) As Long


Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal cbLength As Long)
 
Private Declare Function GetProfileStringA Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long


Function DefaultPrinterName() As String
    Dim strLPT As String * 255
    Dim Result As String, i&
    Call GetProfileStringA("Windows", "Device", "", strLPT, 254)
    Result = Application.Trim(strLPT)
    i = Application.Find(",", Result, 1)
    DefaultPrinterName = Left(Result, i - 1)
End Function


 
   ' ==================================================================
   ' SetPrinterDuplex
   '
   '  Programmatically set the Duplex flag for the specified printer
   '  driver's default properties.
   '
   '  Returns: True on success, False on error. (An error will also


   '  display a message box. This is done for informational value
   '  only. You should modify the code to support better error
   '  handling in your production application.)
   '
   '  Parameters:
   '    sPrinterName - The name of the printer to be used.
   '
   '    nDuplexSetting - One of the following standard settings:
   '       1 = None
   '       2 = Duplex on long edge (book)
   '       3 = Duplex on short edge (legal)
   '
   ' ==================================================================
Public Function SetPrinterDuplex(ByVal sPrinterName As String, ByVal nDuplexSetting As Long) As Boolean


      Dim hPrinter As Long
      Dim pd As PRINTER_DEFAULTS
      Dim pinfo As PRINTER_INFO_2
      Dim dm As DEVMODE
   
      Dim yDevModeData() As Byte
      Dim yPInfoMemory() As Byte
      Dim nBytesNeeded As Long
      Dim nRet As Long, nJunk As Long
   
      On Error GoTo cleanup
   
      If (nDuplexSetting < 1) Or (nDuplexSetting > 3) Then
         MsgBox "Error: dwDuplexSetting is incorrect."
         Exit Function
      End If
      
      pd.DesiredAccess = PRINTER_ALL_ACCESS
      nRet = OpenPrinter(sPrinterName, hPrinter, pd)
      If (nRet = 0) Or (hPrinter = 0) Then
         If Err.LastDllError = 5 Then
            MsgBox "Access denied -- See the article for more info."
         Else
            MsgBox "Cannot open the printer specified " & _
              "(make sure the printer name is correct)."
         End If
         Exit Function
      End If
   
      nRet = DocumentProperties(0, hPrinter, sPrinterName, 0, 0, 0)
      If (nRet < 0) Then
         MsgBox "Cannot get the size of the DEVMODE structure."
         GoTo cleanup
      End If
   
      ReDim yDevModeData(nRet + 100) As Byte
      nRet = DocumentProperties(0, hPrinter, sPrinterName, _
                  VarPtr(yDevModeData(0)), 0, DM_OUT_BUFFER)
      If (nRet < 0) Then
         MsgBox "Cannot get the DEVMODE structure."
         GoTo cleanup
      End If
   
      Call CopyMemory(dm, yDevModeData(0), Len(dm))
   
      If Not CBool(dm.dmFields And DM_DUPLEX) Then
        MsgBox "You cannot modify the duplex flag for this printer " & _
               "because it does not support duplex or the driver " & _
               "does not support setting it from the Windows API."
         GoTo cleanup
      End If
   
      dm.dmDuplex = nDuplexSetting
      Call CopyMemory(yDevModeData(0), dm, Len(dm))
   
      nRet = DocumentProperties(0, hPrinter, sPrinterName, _
        VarPtr(yDevModeData(0)), VarPtr(yDevModeData(0)), _
        DM_IN_BUFFER Or DM_OUT_BUFFER)


      If (nRet < 0) Then
        MsgBox "Unable to set duplex setting to this printer."
        GoTo cleanup
      End If
   
      Call GetPrinter(hPrinter, 2, 0, 0, nBytesNeeded)
      If (nBytesNeeded = 0) Then GoTo cleanup
   
      ReDim yPInfoMemory(nBytesNeeded + 100) As Byte


      nRet = GetPrinter(hPrinter, 2, yPInfoMemory(0), nBytesNeeded, nJunk)
      If (nRet = 0) Then
         MsgBox "Unable to get shared printer settings."
         GoTo cleanup
      End If
   
      Call CopyMemory(pinfo, yPInfoMemory(0), Len(pinfo))
      pinfo.pDevmode = VarPtr(yDevModeData(0))
      pinfo.pSecurityDescriptor = 0
      Call CopyMemory(yPInfoMemory(0), pinfo, Len(pinfo))
   
      nRet = SetPrinter(hPrinter, 2, yPInfoMemory(0), 0)
      If (nRet = 0) Then
         MsgBox "Unable to set shared printer settings."
      End If
   
      SetPrinterDuplex = CBool(nRet)


cleanup:
      If (hPrinter <> 0) Then Call ClosePrinter(hPrinter)


End Function
''''''''''''''''''''''''
''''''''''''''''''''''''
Sub Test()
    Dim n As Boolean
    n = SetPrinterDuplex(DefaultPrinterName(), 2)
    Sheet1.PrintOut
    n = SetPrinterDuplex(DefaultPrinterName(), 1)
    Sheet1.PrintOut
End Sub
 
Lần chỉnh sửa cuối:
Upvote 0
Cám ơn bạn, nhưng ngay từ dòng code đầu tiên thì mình đã bị bào là Access Denied rồi. Không hiểu sao mà code này không thể kết nối được với máy in dù cho nếu xài phương thức PrintOut thì vẫn in được bình thường.
 
Upvote 0
Cám ơn bạn, nhưng ngay từ dòng code đầu tiên thì mình đã bị bào là Access Denied rồi. Không hiểu sao mà code này không thể kết nối được với máy in dù cho nếu xài phương thức PrintOut thì vẫn in được bình thường.
Mình chạy thử code trên máy in ảo "Microsoft XPS Document Writer" thì chạy bình thường không có lỗi, máy bạn có lỗi không trên máy in ảo không? Mình không test được trên máy thật, nếu máy ảo bình thường mà máy thật có lỗi thì đầu hàng.
 
Upvote 0
Mình thường tạo 2 máy in trên cùng 1 máy in có chức năng in 2 mặt
Ví dụ: Mình dùng máy in HP LaserJet 400 M401 PCL 6. Mình add thành 2 -->
1. HP LaserJet 400 M401 PCL 6_1
2. HP LaserJet 400 M401 PCL 6_2

và khi chuyển trong VBA như sau:
Application.ActivePrinter = "HP LaserJet 400 M401 PCL 6_1 on Ne01:"
hoặc:
Application.ActivePrinter = "HP LaserJet 400 M401 PCL 6_2 on Ne02:"
 
Lần chỉnh sửa cuối:
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom