MS Office Forum / Excel / Programming / January 2008
Before Print Problem
|
|
Thread rating:  |
Karen53 - 25 Jan 2008 22:54 GMT 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
 Signature Thanks for your help. Karen53
Dave Peterson - 25 Jan 2008 23:41 GMT 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.
> Hi, > [quoted text clipped - 27 lines] > Thanks for your help. > Karen53
 Signature Dave Peterson
Karen53 - 26 Jan 2008 00:06 GMT 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?
 Signature Thanks for your help. Karen53
> Are you sure you have the correct addresses in your code? > [quoted text clipped - 37 lines] > > Thanks for your help. > > Karen53 Dave Peterson - 26 Jan 2008 01:00 GMT 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, > [quoted text clipped - 53 lines] > > > > Dave Peterson
 Signature Dave Peterson
Karen53 - 26 Jan 2008 01:14 GMT 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.
 Signature Thanks for your help. Karen53
> Without any testing at all... > [quoted text clipped - 63 lines] > > > > > > Dave Peterson Karen53 - 26 Jan 2008 00:24 GMT 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?
 Signature Thanks for your help. Karen53
> Are you sure you have the correct addresses in your code? > [quoted text clipped - 37 lines] > > Thanks for your help. > > Karen53 Dave Peterson - 26 Jan 2008 00:58 GMT The named ranges won't fix the problem. But it may avoid future problems.
> Hi Dave, > [quoted text clipped - 57 lines] > > > > Dave Peterson
 Signature Dave Peterson
Tim Zych - 26 Jan 2008 02:00 GMT 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.
 Signature Tim Zych SF, CA
> Hi, > [quoted text clipped - 27 lines] > > End Sub Karen53 - 26 Jan 2008 02:54 GMT 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
 Signature Thanks for your help. Karen53
> Try assigning it to a variable so the string can be fully resolved before it > goes into the header, e.g. [quoted text clipped - 38 lines] > > > > End Sub Karen53 - 26 Jan 2008 05:49 GMT 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.
 Signature Thanks for your help. Karen53
> Try assigning it to a variable so the string can be fully resolved before it > goes into the header, e.g. [quoted text clipped - 38 lines] > > > > End Sub Tim Zych - 26 Jan 2008 06:21 GMT 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.
 Signature Tim Zych SF, CA
> This is so frustrating! What is going on? > [quoted text clipped - 54 lines] >> > >> > End Sub Dave Peterson - 26 Jan 2008 15:33 GMT 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? > [quoted text clipped - 58 lines] > > > Thanks for your help. > > > Karen53
 Signature Dave Peterson
Karen53 - 28 Jan 2008 03:23 GMT 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. --- Thanks for your help. Karen53
> Just to muddy the waters... > [quoted text clipped - 67 lines] > > > > Thanks for your help. > > > > Karen53 Dave Peterson - 28 Jan 2008 11:42 GMT I still can't duplicate it. I don't have another guess.
> Hi, > [quoted text clipped - 92 lines] > > > > Dave Peterson
 Signature Dave Peterson
Karen53 - 28 Jan 2008 17:25 GMT 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
 Signature Thanks for your help. Karen53
> I still can't duplicate it. I don't have another guess. > [quoted text clipped - 94 lines] > > > > > > Dave Peterson Dave Peterson - 28 Jan 2008 17:41 GMT 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, > [quoted text clipped - 120 lines] > > > > Dave Peterson
 Signature Dave Peterson
Karen53 - 28 Jan 2008 18:01 GMT 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.
 Signature Thanks for your help. Karen53
> 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. [quoted text clipped - 131 lines] > > > > > > Dave Peterson Karen53 - 28 Jan 2008 17:34 GMT Hi,
Also it's the same for both workbooks. It was working because I was using the print button on the toolbar.
 Signature Thanks for your help. Karen53
> I still can't duplicate it. I don't have another guess. > [quoted text clipped - 94 lines] > > > > > > Dave Peterson Dave Peterson - 28 Jan 2008 17:57 GMT Does it work when you use: File|print or File|Print preview
> Hi, > [quoted text clipped - 106 lines] > > > > Dave Peterson
 Signature Dave Peterson
Karen53 - 28 Jan 2008 18:13 GMT Hi Dave,
Yes, it changes if I use either File/Print or Print Preview/Print. It works everywhere but for my code.
 Signature Thanks for your help. Karen53
> Does it work when you use: > File|print [quoted text clipped - 111 lines] > > > > > > Dave Peterson Karen53 - 28 Jan 2008 18:53 GMT Hi Dave,
It will not work if it is in ThisWorkbook's 'BeforePrint' routine. However, if I move the code into the individual print routines in the general modules, it works just fine so far.
 Signature Thanks for your help. Karen53
> Does it work when you use: > File|print [quoted text clipped - 111 lines] > > > > > > Dave Peterson Dave Peterson - 28 Jan 2008 20:03 GMT I still don't have a guess.
Sorry.
> Hi Dave, > [quoted text clipped - 124 lines] > > > > Dave Peterson
 Signature Dave Peterson
Karen53 - 28 Jan 2008 20:18 GMT HI Dave,
Since it's working in the general modules, I just dumped the 'BeforePrint'. Why fight city hall.
 Signature Thanks for your help. Karen53
> I still don't have a guess. > [quoted text clipped - 128 lines] > > > > > > Dave Peterson Dave Peterson - 28 Jan 2008 21:37 GMT Well, you lasted longer than I would have <vbg>.
> HI Dave, > [quoted text clipped - 140 lines] > > > > Dave Peterson
 Signature Dave Peterson
|
|
|