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

suppress default header & footer when printing 4

Status
Not open for further replies.

OhioSteve

MIS
Mar 12, 2002
1,352
US
I am printing a bunch of form letters to a web page. Then the user can then inspect the output and print it. I have already included a div to force pagebreaks. Here is the div~

<div style="page-break-before:always">
text text text
</div>


I did this WITHOUT defining a css in the page head. I was pleasantly surprised when it worked without that.

Now, when I actually print the web page, the output has a page number in the header. It also has the url and a date in the footer. I want to suppress those things. Do I need to make a real CSS to do that? If I do need a CSS, can you give me some hints on syntax?
 
The header and footer information you're describing comes from the browser print configuration. I don't believe you have any control over them from the web page.

Ken
 
Actually, your information was very helpful. I did not know what caused the header & footer to appear. Only a couple of users will be printing the form letters. I will just tell them to clear the default header & footer settings.

Problem solved :)
 
As long as the users are browsing with IE, you can use the following code:

Code:
<script Language="vbscript">
	Dim WSHShell
	Dim myHeader
	Dim myFooter
	Set WSHShell = CreateObject("WScript.Shell")
	myHeader = WSHShell.RegRead("HKCU\Software\Microsoft\Internet Explorer\PageSetup\header")
	myFooter = WSHShell.RegRead("HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer")

	WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\header", ""
	WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer", ""

	Sub ResetHeader()
		Dim WSHShell
		Set WSHShell = CreateObject("WScript.Shell")
		WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\header", myHeader
		WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer", myFooter
	End Sub
</script>

The following is a breakdown of the code above.
After declaring the variables, the code first captures the current settings of browsers header and footer:
Code:
myHeader = WSHShell.RegRead("HKCU\Software\Microsoft\Internet Explorer\PageSetup\header")
myFooter = WSHShell.RegRead ("HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer")

It then proceeds to delete the current settings:
Code:
WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\header", ""
WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer", ""

The last part needs to be called by an event - a link on the page would work
Code:
<A HREF="reports.html" OnClick=ResetHeader()>Return To Reports</A>
- and when this event is called, it resets the header and footer back to what the user had to begin with:
Code:
Sub ResetHeader()
Dim WSHShell
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\header", myHeader
WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer", myFooter
End Sub

One last side note, in the browsers security section, make sure the ActiveX controls are set to at least prompt. If they are set to disable, the above code will not work.

You can check out the following thread for more information and other possible solutions: thread215-131150

Hope this helps,

TwoOdd
--------------
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner
 
TwoOdd,
This works perfectly! We'll be using it on an intranet application, so we can set activeX controls to prompt (or enable them) for intranet sites.
Thanks!!!!!!!
 
be aware that the continual exploitation of active-x and other wizardry means that Microsoft are in a constant state of patch....
the script above is great (have another star [smile]), but don't rely on it: one of these days a microsoft patch may well stop it working.

<marc> i wonder what will happen if i press this...[ul][li]please tell us if our suggestion has helped[/li][li]need some help? faq581-3339[/li][/ul]
 
Okay, maybe you guys could help me with my other thread. I need to print in asp. When the page loads, I want it sent directly to the printer, without any user discretion.
 
You can use this for your body tag:
Code:
<BODY onload="window.print()">

That will bring up the print window as soon as the page loads. Trying to program it so it automatically prints without the user doing anything may not be worth the effort for the amount of time it would save the user by him/her not clicking 1 or 2 buttons. At least with this code, the user is prompted to print the page immediately.

Good Luck,

TwoOdd
--------------
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner
 
I've had the same problem which OhioSteve had, until I realized I could use PDF to gain ‘absolute’ control on the page layout for printing. And yes, you can create PDF on-the-fly using ASP. You'll just need a third party software like ABCPDF or PDFCreatorPilot. The only downside is these softwares are not free.
 
The records in my table have a date/time field. Whenever a batch of records print, I must add the date/time into that field for each record which printed. When the page loads I run an "update..." sql statement to perform this task.

Consequently, I DO NOT want to give the user a chance to change their mind. If they don't print the letters, my batch printing times will be bogus, and that is unacceptable. Once they initiate a batch print job, I want to send that job directly to the printer. The print job will be a bunch of form letters. I have already created a looping structure to generate the letters as a web page. In addition, I learned that I can use "page break:after always" to insert page breaks. Ideally, I would like to load the page, send it to the printer, and then redirect them. So they would never even see the web page containing the form letters.

I posted another thread on this, but I could not generate much interest.
 
Okay, I finally have found something that might work for you, keeping in mind manarth's comment that this script may one day be considered unsecure and a patch will keep it from working.

In the meantime, this does seem to work, even though I don't fully understand why or how, but I have tested it and gotten it to work.

Here it is:
Code:
<script Language="vbscript">
Sub PrintPage()
	Dim objIEA
	Set objIEA = CreateObject("InternetExplorer.Application")
	objIEA.Visible = True
	objIEA.Navigate "[URL unfurl="true"]http://www.myserver.com/myreports.asp?var1=A&var2=B&var3=C"[/URL]
	While objIEA.Busy
	Wend
	objIEA.ExecWB 6,2
	Set objIEA = Nothing
End Sub
</script>

Some side notes

The .Navigate line must have the full URL or it wont work. You'll want to change it to an actual URL before you test it.

.Visible must equal True, I tried setting it to false and all it does is put the new browser window behind the others and therefore is not active and therefore can't make the call to ExecWB which is what prints the web page.

If you change 6,2 to 6,1 -- you will get the print window, the 2 bypasses the print window.

The While loop keeps the page from printing until it has loaded.

Yes, this does open a new window (haven't figured out how to get this to work in the current window) and outside of some javascript that tells the window to close which then asks the user if he/she wants to close it, I haven't figured out any other way to close it. You could do a redirect in the new window to a page that tells the user to close it, or possibly just hide the window by adding a .Visible = False before you set the object to nothing.

Don't forget you have to make a call to this script, a submit button would work. I'm assuming you are sending variables to the report page so it knows what to print, so you may want to put this script in an intermediate page to build the URL in ASP for the navigation line.

If anyone has found something better (preferably free) let me(us) know. :)

If you have any questions, I'll see if I can help, but most of what I learned is in this post.

In the meantime, good luck and hope this helps,


TwoOdd
--------------
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner
 
I've been trying to get some of the above code to work, but I can't seem to get any results. I've never worked with vbscript before, so I'm not really sure what I'm doing. Basically I've copied and pasted the above examples into my page and I still get the headers and footers when I print. I've checked my IE Active X settings and they're all good.

Does anyone have any idea as to why I can't get any of this to work? Thanks for any help you can offer!




Hope This Helps!

Ecobb

&quot;My work is a game, a very serious game.&quot; - M.C. Escher
 
Make sure the script is between your header tags:
Code:
<head>
<script>
.....
</script>
</head>

Hope this helps,

TwoOdd
--------------
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner
 
I tried the script and it did not work. Perhaps your security settings are more liberal than mine.

I still think that the best approach would be sendkeys(). Make a print button, then feed it a carriage return using sendkeys(). Unfortunately, I don't know the exact syntax for that in asp.
 
OhioSteve,

I used the window.print function to open the print window, but can't get it to accept the sendkey function for the the carriage return. Tried it in the wshshell and because I'm not using the wshshell to open the print window, I think it doesn't know where to send the return. Haven't figured out how to open the print window in wshshell. :(

Anyway, the above script worked the first time for me without having to change any security settings. Try creating a new html page with just the following code:

Code:
<HTML>
<HEAD>
<script Language="vbscript">
Sub PrintPage()
	Dim objIEA
	Set objIEA = CreateObject("InternetExplorer.Application")
	objIEA.Toolbar = False
	objIEA.Visible = True
	objIEA.Navigate "[URL unfurl="true"]http://www.google.com"[/URL]
	While objIEA.Busy
	Wend
	objIEA.ExecWB 6,2
	Set objIEA = Nothing
End Sub
</script>
</HEAD>
<BODY onload="PrintPage">
This page will open Google's homepage in a new browser window and print it on the default printer.
</BODY>
</HTML>

Open the new html page and it should print Google's homepage on your default printer.

Whether you use this script or the wshshell with sendkeys, the users have to enable or prompt for activex controls in the security settings (I recommend setting them to prompt).
When the prompt comes up, make sure you accept it.

Hope this helps,




TwoOdd
--------------
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner
 
I don't have the administrative authority to control our intranet security settings. I suspect that the network guys have implemented settings that don't allow your script.

More later.
 
It's actually a setting on the browser itself. Open IE and go to Tools/Internet Options/Security and click on Custom Level.

The first section should be ActiveX Controls and Plugins. Make sure all of the settings under this section are either enabled or set to prompt. (Prompt highly recommended)

TwoOdd
--------------
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner
 
TwoOdd, I've got the following working great! (It was my Active Directory settings that was giving me problems.)
Code:
<script Language="vbscript">
    Dim WSHShell
    Dim myHeader
    Dim myFooter
    Set WSHShell = CreateObject("WScript.Shell")
    myHeader = WSHShell.RegRead("HKCU\Software\Microsoft\Internet Explorer\PageSetup\header")
    myFooter = WSHShell.RegRead("HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer")

    WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\header", ""
    WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer", ""

    Sub ResetHeader()
        Dim WSHShell
        Set WSHShell = CreateObject("WScript.Shell")
        WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\header", myHeader
        WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer", myFooter
    End Sub
</script>
But I just have one question. Is there any way I can make the window close after it resets the header/footer? Basically, I have a link that opens a new window, clears the headers/footers, prompts user to print, and now I want them to be able to click on the link to reset headers/footers and also have it close the window. Since I know absolutely nothing about vbscript, I don't have a clue how to make this happen.

Thanks for all of your help so far, it's been great!



Hope This Helps!

Ecobb

&quot;My work is a game, a very serious game.&quot; - M.C. Escher
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top