PDA

View Full Version : Word macro to delete repeating text



jipsun
05-03-2006, 03:19 PM
My company prints out invoices that repeat the customer information almost twice every page. I want to write a macro that will delete the repeats and leave the first section of information.

Each section of customer info starts with the same string of a couple words, but the next 25 lines are different. I want to delete "unique string" + the following 25 lines.

Any suggestions?

fumei
05-03-2006, 10:30 PM
Maybe, but you need to really step-by-step this out. If I understand correctly - and I have read this a couple of times and am NO sure I Do understand - you want to be able to take an input string and search for that and delete it, followed by 25 lines.

1. Be specific
2. 25 lines? 25 paragraphs?
3. Do you want an inout box to ask for this input string? Is always going to be the same string?

Sorry, but your post does not give clear enough info.

jipsun
05-04-2006, 11:17 AM
Thanks for the reply fumei.

Here's what I need:

Every invoice will have many sections where customer information is repeated.
I want to keep the first section and delete the rest.
The only thing the same about all the invoices will be the first two words "company invoice" and then the following 25 lines contain the customer info (different for each customer).

The input string can be written into the macro since it will always be the same.

Basically I need to find "company invoice", then go to the next "company invoice" and delete this line plus the following 25 lines. Then I need to go to the third "company invoice" and delete this plus the following 25 lines, this continues until all repeats of "company invoice" plus 25 lines are deldted.

fumei
05-04-2006, 10:03 PM
1. What do you mean by "section"? Do you mean a REAL section - what Word considers a section? That is, an area of a document separated by Section Breaks?

2. Are these "lines" actually paragraphs? That is, are they separated by paragraph marks? Not line breaks, but paragraph marks?

Frankly, I have to wonder about the design of this. Why are there repeated areas of redundant information? Are you working from a template? If so, the easiest thing would be to make the areas of interest bookmarks. Then it is a very easy thing to delete the bookmarks. No need to do any search for text, and following "lines". Just delete bookmarks.

jipsun
05-05-2006, 09:27 AM
1. No, not a Word defined section, just a chunk of the page that takes up 26 lines.

2. Yes, each line has a paragraph mark, so technically there are 26 paragraphs.

Our invoices are created in our inventory program and then exported to Word so we can print them. For whatever reason, the makers of the program decided that the customer information needed to be repeated throughout the body of the invoice. If there is a template for the invoices, then I think it is written into the software and not something I can manipulate in Word.

fumei
05-09-2006, 07:45 PM
Then, if this is cloned from a template, I would DEFINITELY go in and make the chunks bookmarks. Give each one a identifying name (Page2Chunk..?, Section3Chunk...?...whatever). That would make it much much easier to delete. You could then delete the chunk as a chunk rather than lines of text. Just identify the chunk as a bookmark.

jipsun
05-11-2006, 03:26 PM
Actually, I got some of it figured out now, I just need to figure out how to loop it. This macro will find and delete the customer info sections, but I need to loop it to delete all.


Sub delete()
'
' delete Macro
' Macro recorded 5/11/2006 by jsun
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = _
"company invoice"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveDown Unit:=wdLine, Count:=25, Extend:=wdExtend
Selection.delete Unit:=wdCharacter, Count:=1
End Sub

fumei
05-11-2006, 09:31 PM
Fine. Go it your way. That is the "beauty" of VBA. If you want to do it that way, and loop it....then loop it. Look up loops in Help. A Do...While would work.

jipsun
05-17-2006, 12:41 PM
Thanks for your insight Gerry. Although I went a different way, you still hit a couple switches in my head and allowed me to solve it. I know it looks ugly, but I cheated and recorded most of it.


Sub killheaders()

'This macro will delete all invoice headers except for the first one.
'Created by jipsun on May 17, 2006.

'Part 1: Copy the first header


Selection.find.ClearFormatting
With Selection.find
.Text = _
"company invoice"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.find.Execute
Selection.MoveDown Unit:=wdLine, count:=25, Extend:=wdExtend
Selection.copy

'Part 2: Count the number of invoice headers

Dim icount As Integer
Dim strin As String
strin = "company Invoice"
With ActiveDocument.Content.find
Do While (.Execute(findtext:=strin, Forward:=True) = True) = True
icount = icount + 1
Loop
End With


'Part 3: Delete according to how many invoice headers are present

Dim n As Long
For n = 1 To icount
Selection.find.ClearFormatting
With Selection.find
.Text = _
"company invoice"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.find.Execute
Selection.MoveDown Unit:=wdLine, count:=25, Extend:=wdExtend
Selection.delete Unit:=wdCharacter, count:=1

Next n

'Part 4: Paste the first header back into the top of the sheet


Selection.HomeKey Unit:=wdStory
Selection.PasteAndFormat (wdPasteDefault)

End Sub


:beerchug:

mdmackillop
05-17-2006, 01:16 PM
Hi Jipsun,
If you select your code and click the VBA button, it formats the code as shown, making it more readable.
Regards
MD

fumei
05-18-2006, 10:52 PM
jipsun - if it works to your satisfaction, then OK, sure, that is fine.

However, this is NOT very good use of VBA code. In fact, but sorry, this is poor use of VBA code. It does not promote any understanding of how Word functions, nor how VBA functions. Using the Selection object to copy text, delete it, do a bunch of other stuff (including a interative count that is absolutely NOT needed), and then put the same text back into its original location?


Hmmmmm. But again, it will work, yes.