Advertisement

01.29.2008 at 06:27AM PST, ID: 23119266
[x]
Attachment Details

GetDefaultPrinter Function from API why do I need to initialize the two values past into the Function?

Asked by dis1931 in Microsoft Excel Spreadsheet Software, Windows Programming, Microsoft Development

Tags: VBA Excel

How do the parameters work that are passed into the GetDefaultPrinter Function?

I have figured out that I have to initialize DefPrinter with at least the number of characters in the printer name...in my case 28 will work but I figured 255 is there just in case even though I should never need more than 50 characters I would imagine.

Also the SLen needs to be the minimum characters in my case again 28 but I am using 255 for the same reason as above.  It seems like the GetDefaultPrinter Function will make them shorter so my 255 spaces just becomes the printer name and my slen of 255 becomes the length of the printer string after the Function is done.  So why won't it just make them those same values when I don't initialize them.  I tried initializing them with a smaller value....such as just a word or something and still the same behavior....I guess I understand how to make it work and it is working...but I want to understand the logic and why I need to initialize those with larger values than what they will hold.  Why does the API function work that way?  I have written function and passed in string variables that were not initialized and they filled fine what is different about this scenario...I imagine I will be working with API at least occasionally and so I would like to learn why so that I can apply it in the future.

Obviously the below is just an Excel Macro under ThisWorkbook so feel free to try it out...I am using Excel 2003 but I imagine it functions the same on other versions.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
Private Declare Function SetDefaultPrinter Lib "winspool.drv" _
   Alias "SetDefaultPrinterA" _
  (ByVal pszPrinter As String) As Long
Private Declare Function GetDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" (ByVal sPrinterName As String, lPrinterNameBufferSize As Long) As Long
Dim DefPrinter As String, sLen As Long, hResult As Long
 
Private Sub Workbook_Open()
DefPrinter = Space$(255)
sLen = 255
hResult = GetDefaultPrinter(ByVal DefPrinter, sLen)
If hResult <> 0 Then DefPrinter = Left(DefPrinter, sLen - 1)
MsgBox DefPrinter & sLen
SetDefaultPrinter "\\server\printername"
'ThisWorkbook.ActiveSheet.PrintOut Copies:=1, Collate:=True
'ActiveWorkbook.Save
'Application.Quit
End Sub
 
Loading Advertisement...
 
[+][-]01.29.2008 at 06:40AM PST, ID: 20768073

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Excel Spreadsheet Software, Windows Programming, Microsoft Development
Tags: VBA Excel
Sign Up Now!
Solution Provided By: rorya
Participating Experts: 2
Solution Grade: A
 
 
[+][-]01.29.2008 at 07:21AM PST, ID: 20768459

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628