Microsoft KB Archive/105662: Difference between revisions
(importing KB archive) |
m (Text replacement - "&" to "&") |
||
(One intermediate revision by the same user not shown) | |||
Line 50: | Line 50: | ||
In Microsoft Access, there is no standard way of sending text to the default printer without printing a form or report. This article demonstrates a method of using Windows API functions to send text to the printer.<br /> | In Microsoft Access, there is no standard way of sending text to the default printer without printing a form or report. This article demonstrates a method of using Windows API functions to send text to the printer.<br /> | ||
<br /> | <br /> | ||
This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the | This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x, or the "Building Applications" manual in version 2.0. This article also assumes that you are familiar with programming in the Windows environment. For more information on programming in the Windows environment using API functions, please refer to the Microsoft Windows Software Development Kit manuals. | ||
</div> | </div> | ||
Line 64: | Line 64: | ||
<ul> | <ul> | ||
<li>This article does not describe the API function arguments. For details on the API functions, please refer to the Microsoft Windows Software Development Kit manuals.</li> | <li>This article does not describe the API function arguments. For details on the API functions, please refer to the Microsoft Windows Software Development Kit manuals.</li> | ||
<li>You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive a duplicate procedure name error message, remove or comment out the declarations statement in your code. You should also alias any API function declarations. For more information on using aliases, search for | <li>You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive a duplicate procedure name error message, remove or comment out the declarations statement in your code. You should also alias any API function declarations. For more information on using aliases, search for "alias" then "declare statement" using the Microsoft Access Help menu.</li> | ||
<li><p>In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore when re-creating this code in Access Basic.<br /> | <li><p>In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore when re-creating this code in Access Basic.<br /> | ||
</p> | </p> | ||
Line 79: | Line 79: | ||
End Type | End Type | ||
Declare Function GetProfileString% Lib | Declare Function GetProfileString% Lib "Kernel" (ByVal lpAppName$, _ | ||
ByVal lpkeyname$, ByVal lpDefault$, _ | ByVal lpkeyname$, ByVal lpDefault$, _ | ||
ByVal lpReturnedString$, ByVal nsize%) | ByVal lpReturnedString$, ByVal nsize%) | ||
Declare Function CreateDC% Lib | Declare Function CreateDC% Lib "GDI" (ByVal lpDriverName$, _ | ||
ByVal lpDeviceName$, ByVal lpOutput$, _ | ByVal lpDeviceName$, ByVal lpOutput$, _ | ||
lpInitData As Any) | lpInitData As Any) | ||
Declare Function DeleteDC% Lib | Declare Function DeleteDC% Lib "GDI" (ByVal hDC%) | ||
Declare Function TextOut% Lib | Declare Function TextOut% Lib "GDI" (ByVal hDC%, ByVal X%, ByVal _ | ||
Y%, ByVal lpString$, ByVal nCount%) | Y%, ByVal lpString$, ByVal nCount%) | ||
Declare Function Mylstrcpy& | Declare Function Mylstrcpy& Lib "Kernel" Alias "lstrcpy" ( _ | ||
ByVal lpString1 As Any, ByVal lpString2 As Any) | ByVal lpString1 As Any, ByVal lpString2 As Any) | ||
Declare Function StartDoc% Lib | Declare Function StartDoc% Lib "GDI" (ByVal hDC%, lpdi As DOCINFO) | ||
Declare Function StartPage% Lib | Declare Function StartPage% Lib "GDI" (ByVal hDC%) | ||
Declare Function EndPage% Lib | Declare Function EndPage% Lib "GDI" (ByVal hDC%) | ||
Declare Function EndDocAPI% Lib | Declare Function EndDocAPI% Lib "GDI" Alias "EndDoc" (ByVal hDC%) | ||
Declare Function Rectangle% Lib | Declare Function Rectangle% Lib "GDI" (ByVal hDC%, ByVal X1%, _ | ||
ByVal Y1%, ByVal X2%, ByVal Y2%) | ByVal Y1%, ByVal X2%, ByVal Y2%) | ||
Line 110: | Line 110: | ||
MyDocumentname$ = | MyDocumentname$ = "My Document" | ||
'------------------------------------------ | '------------------------------------------ | ||
Line 120: | Line 120: | ||
'------------------------------------------ | '------------------------------------------ | ||
lpReturnedString$ = Space$(128) | lpReturnedString$ = Space$(128) | ||
nPrinter = GetProfileString( | nPrinter = GetProfileString("windows", "device", ",,,", _ | ||
lpReturnedString$, Len(lpReturnedString$)) | lpReturnedString$, Len(lpReturnedString$)) | ||
Line 126: | Line 126: | ||
' Parse the string of its three components | ' Parse the string of its three components | ||
'----------------------------------------- | '----------------------------------------- | ||
nDevice = InStr(lpReturnedString$, | nDevice = InStr(lpReturnedString$, ",") | ||
nDriver = InStr(nDevice + 2, lpReturnedString$, | nDriver = InStr(nDevice + 2, lpReturnedString$, ",") | ||
szDevice = Mid$(lpReturnedString$, 1, nDevice - 1) | szDevice = Mid$(lpReturnedString$, 1, nDevice - 1) | ||
szDriver = Mid$(lpReturnedString$, nDevice + 1, nDriver - _ | szDriver = Mid$(lpReturnedString$, nDevice + 1, nDriver - _ | ||
Line 140: | Line 140: | ||
MyDoc.cbSize = Len(MyDoc) | MyDoc.cbSize = Len(MyDoc) | ||
MyDoc.lpszDocname = Mylstrcpy(MyDocumentname$, MyDocumentname$) | MyDoc.lpszDocname = Mylstrcpy(MyDocumentname$, MyDocumentname$) | ||
MyDoc.lpszOutPut = 0& | MyDoc.lpszOutPut = 0& | ||
'------------------------------------------ | '------------------------------------------ | ||
' Create the device context | ' Create the device context | ||
'------------------------------------------ | '------------------------------------------ | ||
hDC% = CreateDC(szDriver, szDevice, szOutPut, 0& | hDC% = CreateDC(szDriver, szDevice, szOutPut, 0&) | ||
X% = StartDoc(hDC%, MyDoc) | X% = StartDoc(hDC%, MyDoc) | ||
Line 154: | Line 154: | ||
'------------------------------------------ | '------------------------------------------ | ||
X% = Rectangle(hDC%, 10, 10, 1000, 150) | X% = Rectangle(hDC%, 10, 10, 1000, 150) | ||
MyString$ = | MyString$ = "Derek and Robyn are brother and sister!" | ||
'------------------------------------------ | '------------------------------------------ | ||
Line 183: | Line 183: | ||
== REFERENCES == | == REFERENCES == | ||
Microsoft Windows Software Development Kit | Microsoft Windows Software Development Kit "Guide to Programming," version 3.1, page 254-255<br /> | ||
<br /> | <br /> | ||
Microsoft Windows Software Development Kit | Microsoft Windows Software Development Kit "Programmer's Reference Volume 2: Functions," version 3.1 | ||
</div> | </div> |
Latest revision as of 12:25, 21 July 2020
Article ID: 105662
Article Last Modified on 5/6/2003
APPLIES TO
- Microsoft Access 1.0 Standard Edition
- Microsoft Access 1.1 Standard Edition
- Microsoft Access 2.0 Standard Edition
This article was previously published under Q105662
SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.
In Microsoft Access, there is no standard way of sending text to the default printer without printing a form or report. This article demonstrates a method of using Windows API functions to send text to the printer.
This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x, or the "Building Applications" manual in version 2.0. This article also assumes that you are familiar with programming in the Windows environment. For more information on programming in the Windows environment using API functions, please refer to the Microsoft Windows Software Development Kit manuals.
MORE INFORMATION
Unlike Microsoft Visual Basic, Microsoft Access does not have a printer object for sending text to the printer. The following example demonstrates how to use Windows API functions to send text to the printer.
Notes
- This article does not describe the API function arguments. For details on the API functions, please refer to the Microsoft Windows Software Development Kit manuals.
- You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive a duplicate procedure name error message, remove or comment out the declarations statement in your code. You should also alias any API function declarations. For more information on using aliases, search for "alias" then "declare statement" using the Microsoft Access Help menu.
In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore when re-creating this code in Access Basic.
'------------------------------------------------------------------- 'Declarations Section '------------------------------------------------------------------- Option Compare Database 'Use database order for string comparisons Option Explicit Type DOCINFO cbSize As Integer lpszDocname As Long lpszOutPut As Long End Type Declare Function GetProfileString% Lib "Kernel" (ByVal lpAppName$, _ ByVal lpkeyname$, ByVal lpDefault$, _ ByVal lpReturnedString$, ByVal nsize%) Declare Function CreateDC% Lib "GDI" (ByVal lpDriverName$, _ ByVal lpDeviceName$, ByVal lpOutput$, _ lpInitData As Any) Declare Function DeleteDC% Lib "GDI" (ByVal hDC%) Declare Function TextOut% Lib "GDI" (ByVal hDC%, ByVal X%, ByVal _ Y%, ByVal lpString$, ByVal nCount%) Declare Function Mylstrcpy& Lib "Kernel" Alias "lstrcpy" ( _ ByVal lpString1 As Any, ByVal lpString2 As Any) Declare Function StartDoc% Lib "GDI" (ByVal hDC%, lpdi As DOCINFO) Declare Function StartPage% Lib "GDI" (ByVal hDC%) Declare Function EndPage% Lib "GDI" (ByVal hDC%) Declare Function EndDocAPI% Lib "GDI" Alias "EndDoc" (ByVal hDC%) Declare Function Rectangle% Lib "GDI" (ByVal hDC%, ByVal X1%, _ ByVal Y1%, ByVal X2%, ByVal Y2%) '------------------------------------------------------------------- 'Start of Function '------------------------------------------------------------------- Function Printer () Dim lpReturnedString$ Dim MyDoc As DOCINFO Dim MyDocumentname$ Dim nPrinter, nDriver, nDevice Dim szDevice, szDriver, szOutPut Dim hDC%, X%, MyString$ MyDocumentname$ = "My Document" '------------------------------------------ ' Retrieve the currently selected printer as ' establish with the Control panel. ' Sample string as returned by lpReturnedString$: ' ' HP LaserJet IIISi PostScript,pscript,LPT1: '------------------------------------------ lpReturnedString$ = Space$(128) nPrinter = GetProfileString("windows", "device", ",,,", _ lpReturnedString$, Len(lpReturnedString$)) '----------------------------------------- ' Parse the string of its three components '----------------------------------------- nDevice = InStr(lpReturnedString$, ",") nDriver = InStr(nDevice + 2, lpReturnedString$, ",") szDevice = Mid$(lpReturnedString$, 1, nDevice - 1) szDriver = Mid$(lpReturnedString$, nDevice + 1, nDriver - _ nDevice - 1) szOutPut = Mid$(lpReturnedString$, nDriver + 1) '------------------------------------------ ' Create the DOCINFO structure for StartDoc() ' - lpszDocname is name displayed in PRINTMAN ' - lpszOutPut is not used and set to NULL '------------------------------------------ MyDoc.cbSize = Len(MyDoc) MyDoc.lpszDocname = Mylstrcpy(MyDocumentname$, MyDocumentname$) MyDoc.lpszOutPut = 0& '------------------------------------------ ' Create the device context '------------------------------------------ hDC% = CreateDC(szDriver, szDevice, szOutPut, 0&) X% = StartDoc(hDC%, MyDoc) X% = StartPage(hDC%) '------------------------------------------ ' Rectangle arguments are X, Y, cX, cY '------------------------------------------ X% = Rectangle(hDC%, 10, 10, 1000, 150) MyString$ = "Derek and Robyn are brother and sister!" '------------------------------------------ ' Second and third arguments are the X and Y ' coordinates on paper. '------------------------------------------ X% = TextOut(hDC%, 30, 20, MyString$, Len(MyString$)) X% = EndPage(hDC%) X% = EndDocAPI(hDC%) '------------------------------------------ ' Release the device context when done '------------------------------------------ hDC% = DeleteDC(hDC%) End Function '------------------------------------------------------------------- 'End of Function '-------------------------------------------------------------------
REFERENCES
Microsoft Windows Software Development Kit "Guide to Programming," version 3.1, page 254-255
Microsoft Windows Software Development Kit "Programmer's Reference Volume 2: Functions," version 3.1
Keywords: kbhowto kbprint KB105662