Results 1 to 3 of 3
2013-03-26, 15:00 #1
- Join Date
- Sep 2007
- Thanked 1 Time in 1 Post
Find empty table end of row marker and add a space in front
This is driving me crazy and I'm really hoping that somone can help me. We have a macro that creates our letterhead in this office. With this letterhead, there are several other macros that run with it, one of which will append a label to the end of the letter. All of this was originally created in Word 97 and then modified as we upgraded to different versions. Recently, however, we upgraded a few printers in the office and discovered that HP no longer treats manual feed documents as manual feeds. What that means is that where the printer used to stop when it encountered a label (which is set in Word as a manual feed) and then wait for the label to be fed into it, it now grabs the second page letterhead that is in Tray 1 and pulls that through, then gives the user a size mismatch error and waits for the label to be fed. This is wasting a lot of paper. The only time it does not do this is when the label is the first thing (or only thing) sent to the printer.
My idea was to create a macro that will intercept the print command. It looks to see what template is attached (has to be the letterhead template) and then checks to see if there is a label. All labels (even the ones created from day 1 in Word 97) are formatted in the "Label" style and are in a two-column, one-row table. On the left side is the attn line in bold and the rest of the address in regular font (no bold, no italic). On the right, we have the bar code of the file number for billing purposes and then how the letter is to be delivered under that. So the macro that intercepts the print command checks to see if the letterhead template is attached and if true, then checks to see if the "Label" style is found in the document. If that is true, then the macro intercepts the print command and reverses the print so the label will come out first. (Options, Print Pages in Reverse Order).
However, there are a couple of other things with these letters. Our users will sometimes create a letter with a label, envelope, fax cover sheet, etc, appended to it and will consistently reuse the same letter over and over, only printing what they want to use at the time using a Print Current Page macro or a Print Draft Letter macro I created. In these cases, I don't want the macro that intercepts the print command to reverse the print. I thought the best way to do this was to be to search the "Label" style and replace it with a different style so the intercept print macro wouldn't be able to find a label, and therefore, not run.
Here's the glitch. Mostly, this works okay, but if there is a paragraph mark at the end of the last line of the address and the table end of cell marker is on a line by itself, the search and replace of the "Label" style does not replace it and when the intercept print macro runs, it still detects that there is a "Label" style and it runs the macro and reverses the print. I have tried replacing the style through a selection search and replace, through a range search and replace and through a range search and replace loop. I get the same result each time.
So my question is -- is there any way to find a table end of row marker and put a space in front? With a space, it works. Remember, that this is an address and therefore, this row has several paragraph marks, some are blank as well (style changes on those)? I attached a sample of the label. Here is some sample code I have been playing with.
Sub PrintCurrentPage() Dim rStoryRange As Range Dim oPar As Paragraph Dim oRng As Word.Range Selection.Find.ClearFormatting On Error Resume Next Selection.Find.Style = ActiveDocument.Styles("Label") With Selection.Find .Text = "" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With If Selection.Find.Found Then For Each rStoryRange In ActiveDocument.StoryRanges With rStoryRange.Find .ClearFormatting .Text = "" .Wrap = wdFindContinue .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Style = "Label" .Replacement.ClearFormatting .Replacement.Style = "BodyTextSingle,bts" .Replacement.Text = "" .Execute Replace:=wdReplaceAll End With Next ActiveDocument.Bookmarks("PrintingLocation").Select Application.PrintOut FileName:="", Range:=wdPrintCurrentPage, Item:= _ wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _ Collate:=True, Background:=True, PrintToFile:=False Selection.HomeKey unit:=wdStory For Each rStoryRange In ActiveDocument.StoryRanges With rStoryRange.Find .ClearFormatting .Text = "" .Wrap = wdFindContinue .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Style = "BodyTextSingle.bts" .Replacement.ClearFormatting .Replacement.Style = "Label" .Replacement.Text = "" .Execute Replace:=wdReplaceAll End With Next Else ActiveDocument.Bookmarks("PrintingLocation").Select Application.PrintOut FileName:="", Range:=wdPrintCurrentPage, Item:= _ wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _ Collate:=True, Background:=True, PrintToFile:=False End If Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting End Sub
2013-03-26, 19:01 #2
- Join Date
- Jan 2001
- Melbourne, Victoria, Australia
- Thanked 149 Times in 142 Posts
Perhaps I don't understand the problem well enough but it sounds like you have made a kludge fix to solve the original print problem and now you want to run a second kludge to permanently change the document to avoid the first kludge being triggered. Surely there is a more elegant way to avoid the first kludge when the user doesn't require it. Can't your original fix be altered to be smart enough to know when it is required?
Assuming there is no other way around your issue, then the following code might work for you.
Sub Temp1() Dim aTbl As Table, aCell As Cell, aPara As Paragraph For Each aTbl In ActiveDocument.Tables For Each aCell In aTbl.Range.Cells Set aPara = aCell.Range.Paragraphs.Last If aPara.Style = "Label" And Len(aPara.Range.Text) = 2 Then aPara.Range.InsertBefore " " 'aPara.Range.Style = "Normal" 'if you wanted to change the style instead of adding a space End If Next aCell Next aTbl End SubAndrew Lockton, Chrysalis Design, Melbourne Australia
2013-03-28, 11:26 #3
- Join Date
- Sep 2007
- Thanked 1 Time in 1 Post
Thank you for this Andrew -- but I tried that route as well. Word does not consistently change the style on an empty table row marker.
You are correct though in that I feel like a dog chasing his tail. I try to fix one problem and then end up creating a bunch more. Is there some way to tell the macro that intercepts the print feature not to run when specific other macros are run?
That would solve all the issues in a much more elegant way?
Thanks so much for this.