ช่วยลบกวนดู code หน่อยค่ะ
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 350   3
URL.หัวข้อ / URL
ช่วยลบกวนดู code หน่อยค่ะ

ลอง Code ข้างล่างเพื่อกำหนดขนาดกระดาษได้แล้วค่ะ แต่จะทำอย่างไรให้เวลาเลือก
ปุ่ม page setup อีกครั้งให้แสดงค่าที่ตั้งไว้ก่อนหน้านี้ได้ค่ะ
เช่น เมื่อ RUN Code ตั้งรายงานที่เป็นกระดาษ 15" * 11" แล้ว
แต่ถ้าลองกดปุ่ม page setup จะขึ้นคำว่า The Printer could not be found. แล้วจะตั้งค่าเป็นกระดาษ A4 ตลอดค่ะ

Option Compare Database
Type str_DEVMODE
RGB As String * 94
End Type
Type str_PRTMIP
strRGB As String * 28
End Type

Type type_DEVMODE
strDeviceName As String * 16
intSpecVersion As Integer
intDriverVersion As Integer
intSize As Integer
intDriverExtra As Integer
lngFields As Long
intOrientation As Integer
intPaperSize As Integer
intPaperLength As Integer
intPaperWidth As Integer
intScale As Integer
intCopies As Integer
intDefaultSource As Integer
intPrintQuality As Integer
intColor As Integer
intDuplex As Integer
intResolution As Integer
intTTOption As Integer
intCollate As Integer
strFormName As String * 16
lngPad As Long
lngBits As Long
lngPW As Long
lngPH As Long
lngDFI As Long
lngDFr As Long
End Type
Type type_PRTMIP
xLeftMargin As Long
yTopMargin As Long
xRightMargin As Long
yBotMargin As Long
fDataOnly As Long
xWidth As Long
yHeight As Long
fDefaultSize As Long
cxColumns As Long
yColumnSpacing As Long
xRowSpacing As Long
rItemLayout As Long
fFastPrint As Long
fDatasheet As Long
End Type

Sub SetPaperRpt(RptName As String, PaperSize As String, PaperWidth As String, PaperLength As String, Orientation As String, _
DefaultSource As String, DefaultSize As Boolean, ItemSizeWidth As String, ItemSizeHeight As String, _
TopMargin As String, BotMargin As String, LeftMargin As String, RightMargin As String, PrtName As String)
Dim PrtMipString As str_PRTMIP
Dim PM As type_PRTMIP
Dim DevString As str_DEVMODE
Dim DM As type_DEVMODE
Dim strDevModeExtra As String
Dim intResponse As Integer
Dim rpt As Report

DoCmd.OpenReport RptName, acDesign
Set rpt = Reports(RptName)
If Not IsNull(rpt.PrtDevMode) Then
strDevModeExtra = rpt.PrtDevMode
DevString.RGB = strDevModeExtra
LSet DM = DevString
DM.lngFields = DM.lngFields Or DM.intPaperSize Or DM.intPaperLength Or DM.intPaperWidth
Select Case PaperSize
Case 256
DM.intPaperSize = 256 ' Set custom page. 256 (User -defined)
DM.intPaperWidth = PaperWidth * 254 ' 254 =1 inch
DM.intPaperLength = PaperLength * 254
Case 8 To 10
DM.intPaperSize = PaperSize ' Set custom page. 10 (A4 Small [210 x 297 mm]) ,9 (A4 [210 x 297 mm]) , 8 (A3 [297 x 420 mm])
Case 1 To 41
DM.intPaperLength = ""
Case Else
DM.intPaperSize = 9
End Select
If Not IsNull(PrtName) Or PrtName <> "" Then
DM.strDeviceName = PrtName
End If
If IsNull(Orientation) Or Orientation = 1 Then ' 1 (portrait) or 2 (landscape)
DM.intOrientation = 1
Else
DM.intOrientation = 2
End If
If IsNull(DefaultSource) Or DefaultSource = 4 Then ' 4 (Manual bin ) , 8 (Tractor bin)
DM.intDefaultSource = 4
Else
DM.intDefaultSource = 8
End If
LSet DevString = DM ' Update property.
Mid(strDevModeExtra, 1, 94) = DevString.RGB
rpt.PrtDevMode = strDevModeExtra

PrtMipString.strRGB = rpt.PrtMip
LSet PM = PrtMipString ' Set margins. 567=1 cm
If IsNull(TopMargin) Or IsNull(BotMargin) Or IsNull(LeftMargin) Or IsNull(RightMargin) Then
PM.yTopMargin = 0 * 567 / 10
PM.yBotMargin = 4.8 * 567 / 10
PM.xLeftMargin = 0 * 567 / 10
PM.xRightMargin = 3.1 * 567 / 10
Else
PM.yTopMargin = TopMargin * 567 / 10
PM.yBotMargin = BotMargin * 567 / 10
PM.xLeftMargin = LeftMargin * 567 / 10
PM.xRightMargin = RightMargin * 567 / 10
End If
If IsNull(DefaultSize) Or DefaultSize = -1 Or IsNull(ItemSizeWidth) Or IsNull(ItemSizeHeight) Then
PM.fDefaultSize = -1
Else
PM.fDefaultSize = 0
PM.xWidth = ItemSizeWidth * 567
PM.yHeight = ItemSizeHeight * 567
End If
LSet PrtMipString = PM ' Update property.
rpt.PrtMip = PrtMipString.strRGB
DoCmd.Close acReport, RptName, acSaveYes
End If
Set rpt = Nothing
End Sub


Sub TestRpt()
Call SetPaperRpt("A4P", 9, , , 1, 8, True, , , 20, 10, 20, 3.1, "") 'ขนาดกระดาษ A4 ตั้ง
Call SetPaperRpt("A4L", 9, , , 2, 4, True, , , 20, 10, 20, 3.1, "") 'ขนาดกระดาษ A4 นอน

Call SetPaperRpt("9_5x11P", 256, 11, 11, 1, 8, True, , , 20, 7.9, 3.1, 3.1, "EPSON LQ-2080 ESC/P 2") 'ขนาดกระดาษต่อเนื่อง 9.5"x11" ตั้ง
Call SetPaperRpt("15x11P", 256, 15, 11, 1, 8, False, 35, 0.5, 20, 6.9, 4, 3.1, "EPSON LQ-2080 ESC/P 2") 'ขนาดกระด*11"าษต่อเนื่อง 15" x11" ตั้ง
End Sub

ปล.ขอลบกวนดูกระทู้ 107 อีกครั้งด้วยค่ะ ขอบคุณค่ะ

3 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R00102
ก่อนอื่นต้องมาดูก่อนว่าได้เขียน Procedure ไว้อย่างไรบ้าง Sub SetPaperRpt(RptName As String, PaperSize As String, PaperWidth As String, PaperLength As String, Orientation As String, _ DefaultSource As String, DefaultSize As Boolean, ItemSizeWidth As String, ItemSizeHeight As String, _ TopMargin As String, BotMargin As String, LeftMargin As String, RightMargin As String, PrtName As String) จากโค้ดที่ให้มาจะมีการกำหนด Type ของตัวแปรมาให้เรียบร้อยแล้ว เช่น Integer เช่น PaperWidth, PaperLength, Orientation ดูจาก intOrientation As Integer intPaperSize As Integer intPaperLength As Integer intPaperWidth As Integer ไม่ใช่ String ผมอยากจะแนะนำให้ดูตัวอย่างโค้ดพร้อมคำอธิบายที่ http://www.microsoft.com/AccessDev/Articles/GetzCh10.HTM และถ้าต้องการตัวอย่างประกอบบทความข้างต้น ให้ฝากอีเมล์ไว้ ผมจะส่งไปให้ ขอขอบคุณ Paul Litwin, Ken Getz, With Mike Gilbert and Greg Reddick ถ้ามีอะไรก็ถามมาได้อีกครับ และนี่คือโค้ดที่ผมปรับปรุงให้ครับ Option Compare Database Option Explicit Type str_DEVMODE RGB As String * 94 End Type Type str_PRTMIP strRGB As String * 28 End Type Type type_DEVMODE strDeviceName As String * 16 intSpecVersion As Integer intDriverVersion As Integer intSize As Integer intDriverExtra As Integer lngFields As Long intOrientation As Integer intPaperSize As Integer intPaperLength As Integer intPaperWidth As Integer intScale As Integer intCopies As Integer intDefaultSource As Integer intPrintQuality As Integer intColor As Integer intDuplex As Integer intResolution As Integer intTTOption As Integer intCollate As Integer strFormName As String * 16 strReportName As String * 16 lngPad As Long lngBits As Long lngPW As Long lngPH As Long lngDFI As Long lngDFr As Long End Type Type type_PRTMIP xLeftMargin As Long yTopMargin As Long xRightMargin As Long yBotMargin As Long fDataOnly As Long xWidth As Long yHeight As Long fDefaultSize As Long cxColumns As Long yColumnSpacing As Long xRowSpacing As Long rItemLayout As Long fFastPrint As Long fDatasheet As Long End Type Sub SetPaperRpt(strReportName, intPaperSize, intPaperWidth, intPaperLength, intOrientation, _ intDefaultSource, fDefaultSize, ItemSizeWidth As Long, ItemSizeHeight As Long, _ yTopMargin, yBotMargin, xLeftMargin, xRightMargin, strDeviceName) Dim PrtMipString As str_PRTMIP Dim PM As type_PRTMIP Dim DevString As str_DEVMODE Dim DM As type_DEVMODE Dim strDevModeExtra As String Dim intResponse As Integer Dim rpt As Report DoCmd.OpenReport strReportName, acDesign Set rpt = Reports(strReportName) If Not IsNull(rpt.PrtDevMode) Then strDevModeExtra = rpt.PrtDevMode DevString.RGB = strDevModeExtra LSet DM = DevString DM.lngFields = DM.lngFields Or DM.intPaperSize Or DM.intPaperLength Or DM.intPaperWidth Select Case intPaperSize Case 256 DM.intPaperSize = 256 ' Set custom page. 256 (User -defined) DM.intPaperWidth = intPaperWidth * 254 ' 254 =1 inch DM.intPaperLength = intPaperLength * 254 Case 8 To 10 DM.intPaperSize = intPaperSize ' Set custom page. 10 (A4 Small [210 x 297 mm]) ,9 (A4 [210 x 297 mm]) , 8 (A3 [297 x 420 mm]) Case 1 To 41 DM.intPaperLength = "" Case Else DM.intPaperSize = 9 End Select If Not IsNull(strDeviceName) Or strDeviceName <> "" Then DM.strDeviceName = strDeviceName End If If IsNull(intOrientation) Or intOrientation = 1 Then ' 1 (portrait) or 2 (landscape) DM.intOrientation = 1 Else DM.intOrientation = 2 End If If IsNull(intDefaultSource) Or intDefaultSource = 4 Then ' 4 (Manual bin ) , 8 (Tractor bin) DM.intDefaultSource = 4 Else DM.intDefaultSource = 8 End If LSet DevString = DM ' Update property. Mid(strDevModeExtra, 1, 94) = DevString.RGB rpt.PrtDevMode = strDevModeExtra PrtMipString.strRGB = rpt.PrtMip LSet PM = PrtMipString ' Set margins. 567=1 cm If IsNull(yTopMargin) Or IsNull(yBotMargin) Or IsNull(xLeftMargin) Or IsNull(xRightMargin) Then PM.yTopMargin = 0 * 567 / 10 PM.yBotMargin = 4.8 * 567 / 10 PM.xLeftMargin = 0 * 567 / 10 PM.xRightMargin = 3.1 * 567 / 10 Else PM.yTopMargin = yTopMargin * 567 / 10 PM.yBotMargin = yBotMargin * 567 / 10 PM.xLeftMargin = xLeftMargin * 567 / 10 PM.xRightMargin = xRightMargin * 567 / 10 End If If IsNull(fDefaultSize) Or fDefaultSize = -1 Or IsNull(ItemSizeWidth) Or IsNull(ItemSizeHeight) Then PM.fDefaultSize = -1 Else PM.fDefaultSize = 0 PM.xWidth = ItemSizeWidth * 567 PM.yHeight = ItemSizeHeight * 567 End If LSet PrtMipString = PM ' Update property. rpt.PrtMip = PrtMipString.strRGB DoCmd.Close acReport, strReportName, acSaveYes End If Set rpt = Nothing End Sub Sub TestRpt() 'Call SetPaperRpt("A4P", 9, , , 1, 8, True, , , 20, 10, 20, 3.1, "") '¢¹Ò´¡ÃдÒÉ A4 µÑé§ 'Call SetPaperRpt("A4L", 9, , , 2, 4, True, , , 20, 10, 20, 3.1, "") '¢¹Ò´¡ÃдÒÉ A4 ¹Í¹ 'Call SetPaperRpt("9_5x11P", 256, 11, 11, 1, 8, True, , , 20, 7.9, 3.1, 3.1, "EPSON LQ-2080 ESC/P 2") '¢¹Ò´¡ÃдÒɵèÍà¹×èͧ 9.5"x11" µÑé§ 'Call SetPaperRpt("15x11P", 256, 15, 11, 1, 8, False, 35, 0.5, 20, 6.9, 4, 3.1, "EPSON LQ-2080 ESC/P 2") '¢¹Ò´¡Ãд*11"ÒɵèÍà¹×èͧ 15" x11" µÑé§ Call SetPaperRpt("MyReport", 8, 11, 15, 1, 4, False, 10, 10, 1, 1, 1, 1, "Acrobat PDFWriter") End Sub
2 @R00104
ขอบคุณค่ะ เดี่ยวลองดูก่อนนะค่ะ
3 @R00108
ยังไม่ได้ค่ะ ลองอ่านและดูในตย. code (PrinterDevice.mdb) ของ From Access 97 Developer's Handbook by Litwin, Getz, and Gilbert (Sybex) แล้ว แต่ก็ยังไม่เข้าใจว่าจะกำหนดเครื่องพิมพ์อย่างไรดีค่ะ ตรง DM.strDeviceName = เครื่องพิมพ์เริ่มต้นหรือ เครื่องพิมพ์ที่ระบุ ทั้งที่กำหนดชื่อเครื่องพิมพ์แล้ว และจะขึ้นข้อความเมื่อกดปุ่ม page setup ว่า ก่อนหน้านี้เอกสารนี้ได้ถูกจัดรูปแบบสำหรับเครื่องพิมพ์ 'EPSON LQ-2080 ESC/P 2 บน LPT1:' แต่เครื่องพิมพ์นั้นใช้การไม่ได้ การตั้งค่าหน้ากระดาษที่แสดงในกล่องโต้ตอบการตั้งค่าการพิมพ์ขณะนี้ ถูกตั้งไว้สำหรับเครื่องพิมพ์เริ่มต้น 'EPSON LQ-2080 ESC/P 2 บน LPT1:' คุณต้องการที่จะดำเนินการอีกต่อไปหรือไม่
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0732s