#1
|
|||
|
|||
Deleting Characters in a specific location in Word 2010
I am trying to write a macro in Word 2010 that will specifically delete any characters that is in position 1 of an entire word document. When I exported a listing from the mainframe and opening it in Word, I am getting the carriage control in the 1st position which is a '1' for new page or a '0' to skip 2 lines. I saw a VBA code for a find and replace but it's doing it for the entire document. I just need to target position 1.
I hope someone can help me. Thanks in advance |
#2
|
||||
|
||||
At its simplest:
ActiveDocument.Range.Characters.First.Delete However, it might be better to implement a loop to delete all characters before the first 'valid' character. For example: Code:
Sub Demo() Application.ScreenUpdating = False With ActiveDocument.Range Do While .Characters.First Like "[" & vbTab & "-" & vbCr & "]" .Characters.First.Delete Loop End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Replacing any character with a space in the 1st position of every line in the document
Hi Paul,
Thanks for the reply. Let me first let you know that I'm a newbie and Im using the Internet to learn. I tried your suggestion and it work on the first occurance. Not sure why it did not delete the rest of the document. Also I made a mistake in my request. I really need to replace everything in position 1 with a space. After running your code, I saw a problem. It shifted the line to the left and if the code work for the whole document, the document will look like teeth in a saw blade. Here is my code that examine the first character of each line and it working. I'm getting the page break and the line skips. Dim intCntr Dim FirstLine intCntr = 0 For Each FirstLine In ActiveDocument.Paragraphs intCntr = intCntr + 1 If Left(FirstLine, 1) = "1" Then If intCntr > 1 Then ActiveDocument.Paragraphs(intCntr).PageBreakBefore = True End If Else If Left(FirstLine, 1) = "0" Then ActiveDocument.Paragraphs(intCntr).LineUnitBefore = 2 Else If Left(FirstLine, 1) = "-" Then ActiveDocument.Paragraphs(intCntr).LineUnitBefore = 3 End If End If End If Next FirstLine Since I'm already examining the first character, Can I just assign a " " (space) to the variable? I just don't know how to access that variable. Thanks once again to all, Phil |
#4
|
||||
|
||||
The macro I posted doesn't process anything other than the start of the document because your requirements are unclear. For example, one can only guess what you mean by:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Hi Paul,
I apologize for not being very clear. Its probably my mind thinking and talking in the mainframe language. I will try to be clearer this time. When a listing is created in the mainframe, position 1 in every line of the report is reserve for carriage returns. This is how the mainframe communicate to the printers for line spacing. Each line in the document actually starts at position 2. So when you FTP the report out of the mainframe into a text file you will see all the carriage returns in position 1. A “1” is a page break a “0” is skip 2 lines a “-“ is skip 3 lines a blank is print next line. All I wanted to do is to strip these carriage return after I'm done using it in my macro and replace them with a blank so I don’t mess up the alignment of each line and the document looks right. I attached a sample of the document xyz.docx so it will be a little bit clearer. The document consist of hundreds of pages and I only attched 1 page out of this document. I already ran my macro for this document that perform the page break and the skipping of lines. If you look at this sample page, in position 1 of each line you will still see the l the carriage return codes(except for blank, it just mean next line) which I used to do the page breaks and line skipping. Thank you again for looking at my post and being so patient. Phil Last edited by ppayaw; 12-09-2016 at 01:46 PM. |
#6
|
||||
|
||||
Try:
Code:
Sub Demo() Application.ScreenUpdating = False With ActiveDocument.Range .InsertBefore vbCr With .Find .ClearFormatting .Replacement.ClearFormatting .Format = False .Forward = True .Wrap = wdFindContinue .MatchWildcards = True .Text = "^13[0-1]>" .Replacement.Text = "^p" .Execute Replace:=wdReplaceAll .Text = "^13[\-]" .Execute Replace:=wdReplaceAll End With .Characters.First.Delete End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Hi Paul,
Thank you again for taking the time reading and answering my post. I tried the code you gave me and unfortunatly it only worked for the first line and it looks like it ignored the rest of the document. I did a step thru and it looping thru the code but not replacing anything after the first line of the document. I tried very hard searching the web but did not get any answer. I really appriciate your patient. Thanks you, Phil |
#8
|
||||
|
||||
The code works fine with the sample document attached to your previous post.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Paul,
Thank you very much for all the help. If there is anything I can help you with the mainframe let me know. (you are probably saying what is a mainframe - LOL) Santa can early this year for me. Happy Holidays and a very Prosperous New Year to you and your family. Thanks, Phil |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Copying specific columns of a table to WORD and deleting rows | ffinley | Word VBA | 5 | 12-07-2015 04:01 PM |
Deleting Range between Characters ! - % | Jonny | Word | 1 | 01-28-2015 04:35 PM |
Deleting A blank Line that has a specific heading style , word 2010 & 2013 | SteveWcg | Word | 5 | 01-08-2014 10:37 PM |
Word 2010 save to new location | youngdave55 | Word | 5 | 05-10-2012 12:12 AM |
Create Hyperlinks from Word to specific location in PDF | sukanyae | Word | 0 | 02-25-2010 04:08 PM |