Before Print Problem

K

Karen53

Hi,

I have the following BeforePrint routine in my ThisWorkbook module which
places a custom header on pages based on the value of cells in the sheet. It
worked except now if the value of the cell changes, it is not picking up the
change and stays with the old value in the header. This there something I
am missing?

Private Sub Workbook_BeforePrint(Cancel As Boolean)

'add custom header to pages before printing

With SumbyLineItempg.PageSetup
.CenterHeader = "&C&14&B" & SumbyLineItempg.Range("E2").Value & _
"&4" & vbCrLf & "&12&A" & vbCrLf & " "
End With

With LineItemspg.PageSetup
.CenterHeader = "&C&14&B" & LineItemspg.Range("C2").Value & _
"&4" & vbCrLf & "&12&A" & vbCrLf & " "
End With

With MainPagepg.PageSetup
.CenterHeader = "&C&14&B" & MainPagepg.Range("B1").Value & _
"&4" & vbCrLf & "&12&A" & vbCrLf & " "
End With

End Sub
 
D

Dave Peterson

Are you sure you have the correct addresses in your code?

(You may want to give them nice range names and use those instead--it may be
safer if rows/columns can be inserted/deleted.)

Are you sure you still have .enableevents set to true before you print/print
preview?

It could be other code that turns that off--and doesn't turn it back on.
 
K

Karen53

Hi Dave,

Thanks for your help.

Yes, enable events is on and it is the correct address. I have found that
if I select 'Print Preview' it corrects the problem and the page prints
correctly. If I change the cell value and do not 'Print Preview' first, the
header does not update with the new value. What is this? Do you know?
--
Thanks for your help.
Karen53


Dave Peterson said:
Are you sure you have the correct addresses in your code?

(You may want to give them nice range names and use those instead--it may be
safer if rows/columns can be inserted/deleted.)

Are you sure you still have .enableevents set to true before you print/print
preview?

It could be other code that turns that off--and doesn't turn it back on.
 
K

Karen53

Hi Dave,

I have added these two statements right before my print command and I am
still having the same problem..

Application.EnableEvents = True
Application.ScreenUpdating = True

I've named the range as you suggested and am using that instead. So, it is
the correct location.

Do you have any other suggestions?
--
Thanks for your help.
Karen53


Dave Peterson said:
Are you sure you have the correct addresses in your code?

(You may want to give them nice range names and use those instead--it may be
safer if rows/columns can be inserted/deleted.)

Are you sure you still have .enableevents set to true before you print/print
preview?

It could be other code that turns that off--and doesn't turn it back on.
 
D

Dave Peterson

The named ranges won't fix the problem. But it may avoid future problems.


Hi Dave,

I have added these two statements right before my print command and I am
still having the same problem..

Application.EnableEvents = True
Application.ScreenUpdating = True

I've named the range as you suggested and am using that instead. So, it is
the correct location.

Do you have any other suggestions?
 
D

Dave Peterson

Without any testing at all...

How about adding this right before the End Sub

DoEvents

Maybe it'll allow some time for excel to catch up before the actually printing.

But I really don't have a good reason why print preview works and print doesn't.
Hi Dave,

Thanks for your help.

Yes, enable events is on and it is the correct address. I have found that
if I select 'Print Preview' it corrects the problem and the page prints
correctly. If I change the cell value and do not 'Print Preview' first, the
header does not update with the new value. What is this? Do you know?
 
K

Karen53

Hi Dave,

I added DoEvents right before the End Sub of my BeforePrint routine but it
didn't make any difference. This is really frustrating.

--
Thanks for your help.
Karen53


Dave Peterson said:
Without any testing at all...

How about adding this right before the End Sub

DoEvents

Maybe it'll allow some time for excel to catch up before the actually printing.

But I really don't have a good reason why print preview works and print doesn't.
 
T

Tim Zych

Try assigning it to a variable so the string can be fully resolved before it
goes into the header, e.g.

Dim HdrVal As String
HdrVal = "&C&14&B" & SumbyLineItempg.Range("E2").Value & _
"&4" & vbCrLf & "&12&A" & vbCrLf & " "
Debug.Print HdrVal
.CenterHeader = HdrVal

Then you can inspect it too to help narrow down the problem.
 
K

Karen53

Hi,

Thank you for the suggestion, Tim. It was a great idea. My immediate
window shows the code executing and returns the correct Header, but the
printed copy still shows the old header.

Here are the results from my immediate window...

Starting BeforePrint, HdrVal = &C&14&BDayna's Mall&4
&12&A

&C&14&BDayna's Mall&4
&12&A

--
Thanks for your help.
Karen53


Tim Zych said:
Try assigning it to a variable so the string can be fully resolved before it
goes into the header, e.g.

Dim HdrVal As String
HdrVal = "&C&14&B" & SumbyLineItempg.Range("E2").Value & _
"&4" & vbCrLf & "&12&A" & vbCrLf & " "
Debug.Print HdrVal
.CenterHeader = HdrVal

Then you can inspect it too to help narrow down the problem.
 
K

Karen53

This is so frustrating! What is going on?

The immediate window shows the code executing with the correct value for the
header variable, yet the header on the worksheet does not change.

If I do a print preview, the worksheet header changes.

I've run out of things to try.

--
Thanks for your help.
Karen53


Tim Zych said:
Try assigning it to a variable so the string can be fully resolved before it
goes into the header, e.g.

Dim HdrVal As String
HdrVal = "&C&14&B" & SumbyLineItempg.Range("E2").Value & _
"&4" & vbCrLf & "&12&A" & vbCrLf & " "
Debug.Print HdrVal
.CenterHeader = HdrVal

Then you can inspect it too to help narrow down the problem.
 
T

Tim Zych

Hmm, it works for me. If you try this in a new workbook, does the same
behavior occur? Does this occur for a different printer? I'm wondering if
there's a page setup configuration / zoom factor / page breaks / preview
mode / which sheet is active / how is printing being invoked or some other
page-setup issue that is causing the code to be buggy.


--
Tim Zych
SF, CA

Karen53 said:
This is so frustrating! What is going on?

The immediate window shows the code executing with the correct value for
the
header variable, yet the header on the worksheet does not change.

If I do a print preview, the worksheet header changes.

I've run out of things to try.
 
D

Dave Peterson

Just to muddy the waters...

What version of excel are you using?
What type of view are you using: Normal, page break preview, print layout?
If you create a new workbook with just enough data for testing and plop the code
in there, does that look ok?

I don't have a guess why it's not working for you.
This is so frustrating! What is going on?

The immediate window shows the code executing with the correct value for the
header variable, yet the header on the worksheet does not change.

If I do a print preview, the worksheet header changes.

I've run out of things to try.
 
K

Karen53

Hi,

So far I have tried this at home and at work with the same result. I have
created a new workbook and the code works. I thought perhaps it was because
the workbook was protected. So, I tried it and it still worked. There has
to be something different with this workbook. I am going through what I can
think of one at a time to see if it makes a difference.

It executes the code but doesn't make the change. So far I haven't fouind
the difference. I'll continue tying difference things tomorrow. There are
only so many things I know of to try, though.

I am using Excel 2003 SP2 at home. At work I believe it is version 2003 as
well. I'll check tomorrow.

This is so strange. I don't have a lot of experience with this. Your help
is very appreciated.
 
K

Karen53

Hi,

Ok, it works as long as I use the print button on the toolbar. As soon as
I use code to print the page it stops working. I have tried the printing
code two ways.

With Sheets(ShName)
.Range(Startcol & StartRow & ":" & EndCol & RptEndRow).PrintOut _


Copies:=1, Collate:=True
End With

With Sheets(ShName)
.Range(Startcol & StartRow & ":" & EndCol & RptEndRow).Select
.PageSetup.PrintArea = Startcol & StartRow & ":" & EndCol & RptEndRow
Selection.PrintOut Copies:=1, Collate:=True
End With
 
K

Karen53

Hi,

Also it's the same for both workbooks. It was working because I was using
the print button on the toolbar.
 
D

Dave Peterson

I wouldn't use the .select and Selection lines. To select a range, the sheet
that owns that range has to be active--and that may not be true.

But I'm not sure what you mean by stops working. Does this mean that the code
doesn't even run--or it runs, but the headers don't change (or printareas don't
change).

And if you're running this stuff using code, is there any chance that you've
disabled events before doing the printout. If you did, then the
workbook_beforeprint won't even fire.
Hi,

Ok, it works as long as I use the print button on the toolbar. As soon as
I use code to print the page it stops working. I have tried the printing
code two ways.

With Sheets(ShName)
.Range(Startcol & StartRow & ":" & EndCol & RptEndRow).PrintOut _


Copies:=1, Collate:=True
End With

With Sheets(ShName)
.Range(Startcol & StartRow & ":" & EndCol & RptEndRow).Select
.PageSetup.PrintArea = Startcol & StartRow & ":" & EndCol & RptEndRow
Selection.PrintOut Copies:=1, Collate:=True
End With
 
D

Dave Peterson

Does it work when you use:
File|print
or
File|Print preview


Hi,

Also it's the same for both workbooks. It was working because I was using
the print button on the toolbar.
 
K

Karen53

Hi Dave,

Yes, what I mean is the code executes but the header does not change. The
immediate window shows the new header but the page's header does not change.

I tried adding an Application.EnableEvents = True before it but it still
did not change.
 
K

Karen53

Hi Dave,

Yes, it changes if I use either File/Print or Print Preview/Print. It works
everywhere but for my code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top