Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting default Printer

Status
Not open for further replies.

petermeachem

Programmer
Aug 26, 2000
2,270
GB
This is long and a bit complex, and I'm very stuck.
1) VB5
2) Can't use common dialog ocx (don't worry why)
3) Found code to display Print Setup via dll. All fine.
4) Stupidly, VB uses only default printer and code found doesn't change same.
5) After a bit more hunting about, I found this.

retvalue = GetVersionExA(osinfo)
If osinfo.dwMajorVersion = 3 And osinfo.dwMinorVersion = 51 And osinfo.dwBuildNumber = 1057 And osinfo.dwPlatformId = 2 Then
Call WinNTSetDefaultPrinter(PrinterName)
ElseIf osinfo.dwMajorVersion = 4 And osinfo.dwMinorVersion = 0 And osinfo.dwBuildNumber = 67109814 And osinfo.dwPlatformId = 1
Then
Call Win95SetDefaultPrinter(PrinterName)
ElseIf osinfo.dwMajorVersion = 4 And osinfo.dwMinorVersion = 0 And osinfo.dwBuildNumber = 1381 And osinfo.dwPlatformId = 2 Then
Call WinNTSetDefaultPrinter(PrinterName)
End If

This works for nt, but not for win98 (major is 4 and minor is 10!). No idea about win2k.
So I put a default in.
6) The code for the subs is long, I'll mail it to you if you like.
7)The winnt sub does a sendmessage to tell all programmes that the default has changed, the win95 doesnt.

8) The problem is that on win98, I can change the default printer ok, (seen in start/settings/printers) but if I have already printed from my programme, it won't change the printer the programme uses. So I can change the default everywhere, apart from in my prog.

9) Well stuck!
 
Peter,

VB itself supports a printers collection. You can do a loop For MyPtr = Printers.Count ...

for each 'MyPtr', uou can check/set most properties. Somewhere in this you can also "set" the printer (to select the default). The only really bad side effect is that this SETS the default printer for the Machine (OS?). So if you need to change for a specific purpose, you need to get the original default printers name, save it, do your printiing, then restore the default.

This was fairly well documented as an example in the VB Help file. Unfortunatly, I've been "Upgraded" to MSDN Library and can't seem to JUST use the old help file, otherwise I'd just send you the sample code.


Actually, I was able to find the relevant info:


The Printers collection enables you to query the available printers so you can specify a default printer for your application. For example, you may want to find out which of the available printers uses a specific printer driver. The following code searches all available printers to locate the first printer with its page orientation set to Portrait, then sets it as the default printer:

Dim X As Printer
For Each X In Printers
If X.Orientation = vbPRORPortrait Then
' Set printer as system default.
Set Printer = X
' Stop looking for a printer.
Exit For
End If
Next

You designate one of the printers in the Printers collection as the default printer by using the Set statement. The preceding example designates the printer identified by the object variable X, the default printer for the application.
[/color blue]

Hope this helps

MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Doesn't work. I've seen this code before, and I couldn't get it to work before either.

All the signs are that it does work, i.e. the printer.devicename changes ok, but when I print, off it goes to the old one.

 
Strange. Weird. Wonderful? (NOT). I have used it on a few occassions, and it worked on those occassions - for win 95 & 98.

If I see anything else, I'll try to rember to let you know. Meanwhile, my thought is that this SHOULD work, so why doesn't it?


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
I've put a really horrible bodge in, so horrid I won't even say what it is.

All I need to do now is get the customer to pay up!
 
Get Default Printer Name

'Add a module to your project (In the menu choose Project -> Add Module, Then click Open)
'Insert this code to the module :

Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey _
As Long, ByVal lpSubKey As String, ByVal dwReserved As Long, ByVal samDesired _
As Long, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" _
(ByVal hKey As Long, ByVal lpValueName$, ByVal lpdwReserved As Long, _
lpdwType As Long, lpData As Any, lpcbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Public Const HKEY_CURRENT_CONFIG As Long = &H80000005

'Insert the following code to your form:

Function RegGetString$(hInKey As Long, ByVal subkey$, ByVal valname$)
Dim RetVal$, hSubKey As Long, dwType As Long, SZ As Long
Dim R As Long
RetVal$ = ""
Const KEY_ALL_ACCESS As Long = &HF0063
Const ERROR_SUCCESS As Long = 0
Const REG_SZ As Long = 1
R = RegOpenKeyEx(hInKey, subkey$, 0, KEY_ALL_ACCESS, hSubKey)
If R <> ERROR_SUCCESS Then GoTo Quit_Now
SZ = 256: v$ = String$(SZ, 0)
R = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ)
If R = ERROR_SUCCESS And dwType = REG_SZ Then
RetVal$ = Left$(v$, SZ - 1)
Else
RetVal$ = &quot;--Not String--&quot;
End If
If hInKey = 0 Then
R = RegCloseKey(hSubKey)
End If
Quit_Now:
RegGetString$ = RetVal$
End Function


Private Sub Form_Load()
Dim GetCurrPrinter As String
GetCurrPrinter = RegGetString$(HKEY_CURRENT_CONFIG, _
&quot;System\CurrentControlSet\Control\Print\Printers&quot;, &quot;Default&quot;)
MsgBox GetCurrPrinter
End Sub

Go Eric De Decker
vbg.be@vbgroup.nl

License And Copy Protection AxtiveX.

Download Demo version on my Site:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top