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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Give focus to existing IE window and print out.

Status
Not open for further replies.

petrosky

Technical User
Aug 1, 2001
512
0
0
AU
Hi,

I have a user that browses to an intranet pricelist (the page is print ready) My problem is that he needs to print various quantities of the page to various printer locations.

I thought I would try to whip up a script for him to save mouse clicks & time.

Here is what I have got from much googling.

Code:
Option Explicit

'[Determine the current default printer]***
'(works only on WinXP and newer versions of Windows)
Dim objWMIService, colPrinters, Iexplore
Dim objPrinter, strPreDefault
Set objWMIService = GetObject _
("winmgmts:\\" & "." & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Default = TRUE")
For Each objPrinter in colPrinters
strPreDefault = objPrinter.Name
Next
'wscript.echo chr(34) & strPreDefault & chr(34) & " is the default 

printer now"

'[Set the temporarily default printer]***
On Error Resume Next
Dim strLocal, objNetwork
strLocal = "Canon iR C3200 PCL5c"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.SetDefaultPrinter strLocal
Set objNetwork = Nothing

wscript.echo chr(34) & strLocal & chr(34) & " is the default printer 

now"

Set WshShell = WScript.CreateObject("Wscript.Shell")

wshShell.AppActivate ("Internet Explorer")

If ret = False Then
Msgbox "Internet Explorer is not running."
WScript.Quit
end if

WScript.Sleep 2000
For copies = 1 to 2
  WshShell.SendKeys "%^P"
  WScript.Sleep 520
  WshShell.SendKeys "{ENTER}"
WScript.sleep 10000

Next

'[Restore the previous default printer]***
On Error GoTo 0
Set objNetwork = CreateObject("WScript.Network")
objNetwork.SetDefaultPrinter strPreDefault
wscript.echo chr(34) & strPreDefault & chr(34) & " is the 

default printer again"

Set objNetwork = Nothing

Wscript.quit 0

As you can tell it should first find the existing default printer, set another printer, print x copies etc and then restore the original default printer.

Basically the Appactivate method always returns false whether IE is open or not.

I also looked at the ExecWB method but that seems to involve creating a new instance of IE.

Any help is very appreciated.

Peter.



Remember- It's nice to be important,
but it's important to be nice :)
 
>wshShell.AppActivate ("Internet Explorer")
[tt]dim ret 'you are using option explicit
[red]ret=[/red]wshShell.AppActivate ("Internet Explorer")[/tt]
 
Thanks tsuji.

I have now declared it and changed the line you mentioned...however it still returns false even when IE is open.
Do you have any other ideas for me to try?

Peter.

Remember- It's nice to be important,
but it's important to be nice :)
 
You can pass the "title" of the page in the appactivate(). Thereafter, you may or may not "steel" the focus, but the ret should be true. Focus is guarded against this kind of manipulation in winxp up by default. Sendkeys is not robust in the ms implementation of vbs (vbscript.dll). You may try alternative implementation such as autoit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top