#1
|
|||
|
|||
VB for Word - Need macro to loop each line in a document
Hi All -
I am VERY new to macros in Word. I have written the following macro for Word that will remove line numbers from the start of each line (1 line per paragraph). These line numbers are automatically created in an EDI software that I pull data from and they need to be removed. The macro is removing everything in the line up to the second word (EDI segment). Instead of changing the counter (value currently set at 80) I would like for the macro to run through the entire document. Some of the EDI files that I pull will have +7000 lines and it will get tiresome to have to change this value each time. The rest of the macro is just removing all of the CR so that the data can be shown as a stream. Trust me, I wish everyone used an EDI viewer because this wouldn't be as much of a pain. Please let me know if you have any thoughts. ' EDI fix Macro ' Dim i As Long i = 1 Do Until i > 80 Selection.MoveRight Unit:=wdWord, Count:=2, Extend:=wdExtend Selection.Delete Unit:=wdCharacter, Count:=1 Selection.MoveDown Unit:=wdLine, Count:=1 i = i + 1 Loop Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "^p" .Replacement.Text = "" .Forward = True .wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll End Sub |
#2
|
|||
|
|||
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oPar As Paragraph Dim oRng As Range For Each oPar In ActiveDocument.Paragraphs If IsNumeric(oPar.Range.Characters(1)) Then Set oRng = oPar.Range oRng.Collapse wdCollapseStart oRng.MoveEndUntil "S", wdForward oRng.Delete End If Next Set oRng = ActiveDocument.Range oRng.Text = Replace(oRng.Text, vbCr, "") lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
Thank you. This is doing the overall job that I am requesting but it is removing some lines completely and others it is removing too many characters from the start of the line. Here is a deidentified example:
1. ISA*00* *00**AA*123456789*AA*123456789*123456*1111*^*98765*1234567*0*P*: 2. GS*LV*123789456*123987546*20151222*1122*1*X*005010 X221A1 3. ST*835*123456789 There could be thousands of lines like this with 2 or three character segment headers. The end result with the streaming would be this: ISA*00* *00* *AA*123456789 *AA*123456789 *123456*1111*^*98765*1234567*0*P*: GS*LV*123789456*123987546*20151222*1122*1*X*005010 X221A1 ST*835*123456789 Due to text/word limitations the GS looks like it is on a new line but there is no CR. After running your macro this is what I get: SA*00* *00* *AA*123456789 *AA*123456789 *123456*1111*^*98765*1234567*0*P*: S*LV*123789456*123987546*20151222*1122*1*X*005010X 221A1 ST*835*123456789 The beginning of the segment headers is getting removed. Please let me know if you need any additional information. |
#4
|
||||
|
||||
You could do the lot without a macro, using a wildcard Find/Replace, where:
Find = ^13[0-9]{1,}.^32 Replace = ^p At most, you might need to insert an empty paragraph at the start of the document.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Paul is right, however I thought the numbers in each paragraph was followed by the word "Segment"
This should work: Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oPar As Paragraph Dim oRng As Range For Each oPar In ActiveDocument.Paragraphs If IsNumeric(oPar.Range.Characters(1)) Then Set oRng = oPar.Range oRng.Collapse wdCollapseStart oRng.MoveEndUntil ".", wdForward oRng.MoveEnd wdCharacter, 2 oRng.Delete End If Next Set oRng = ActiveDocument.Range oRng.Text = Replace(oRng.Text, vbCr, "") lbl_Exit: Exit Sub End Sub |
#6
|
||||
|
||||
The macro equivalent of the wildcard Find/Replace is:
Code:
Sub Demo() Application.ScreenUpdating = False With ActiveDocument.Range .InsertBefore vbCr With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "^13[0-9]{1,}.^32" .Replacement.Text = "^p" .Forward = True .Wrap = wdFindContinue .Format = False .MatchWildcards = True .Execute Replace:=wdReplaceAll End With While .Characters.First = vbCr .Characters.First.Delete Wend End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
loop macro, vb, word 2013 lines |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
macro to add brackets to each line and add single quotes to each word in the line | bracketandquotes | Word VBA | 17 | 02-16-2015 03:51 PM |
Save As Macro using first line of document as document name | redzan | Word VBA | 1 | 01-31-2015 09:24 PM |
Loop through each Line in Word | ilcaa72 | Word VBA | 4 | 02-13-2014 11:48 AM |
Macro to loop in Word | Yamaha Rider | Word VBA | 2 | 02-07-2012 05:33 PM |
WORD Macro - import picture - resize - position - page break - loop | Nano07 | Word VBA | 2 | 11-02-2011 05:14 AM |