Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-07-2016, 03:03 PM
ppayaw ppayaw is offline Deleting Characters in a specific location in Word 2010 Windows 10 Deleting Characters in a specific location in Word 2010 Office 2010 64bit
Novice
Deleting Characters in a specific location in Word 2010
 
Join Date: Dec 2016
Posts: 5
ppayaw is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 12-07-2016, 03:45 PM
macropod's Avatar
macropod macropod is offline Deleting Characters in a specific location in Word 2010 Windows 7 64bit Deleting Characters in a specific location in Word 2010 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 12-08-2016, 10:29 AM
ppayaw ppayaw is offline Deleting Characters in a specific location in Word 2010 Windows 10 Deleting Characters in a specific location in Word 2010 Office 2010 64bit
Novice
Deleting Characters in a specific location in Word 2010
 
Join Date: Dec 2016
Posts: 5
ppayaw is on a distinguished road
Default 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
Reply With Quote
  #4  
Old 12-08-2016, 03:04 PM
macropod's Avatar
macropod macropod is offline Deleting Characters in a specific location in Word 2010 Windows 7 64bit Deleting Characters in a specific location in Word 2010 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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:
the carriage control in the 1st position which is a '1' for new page or a '0' to skip 2 lines
None of this is normal Word terminology. If you want content deleted throughout the file, you're going to have to be clearer on precisely what that content is.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 12-09-2016, 09:26 AM
ppayaw ppayaw is offline Deleting Characters in a specific location in Word 2010 Windows 10 Deleting Characters in a specific location in Word 2010 Office 2010 64bit
Novice
Deleting Characters in a specific location in Word 2010
 
Join Date: Dec 2016
Posts: 5
ppayaw is on a distinguished road
Default

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
Attached Files
File Type: docx xyz.docx (13.8 KB, 13 views)

Last edited by ppayaw; 12-09-2016 at 01:46 PM.
Reply With Quote
  #6  
Old 12-09-2016, 09:14 PM
macropod's Avatar
macropod macropod is offline Deleting Characters in a specific location in Word 2010 Windows 7 64bit Deleting Characters in a specific location in Word 2010 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 12-12-2016, 12:26 PM
ppayaw ppayaw is offline Deleting Characters in a specific location in Word 2010 Windows 10 Deleting Characters in a specific location in Word 2010 Office 2010 64bit
Novice
Deleting Characters in a specific location in Word 2010
 
Join Date: Dec 2016
Posts: 5
ppayaw is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 12-12-2016, 01:16 PM
macropod's Avatar
macropod macropod is offline Deleting Characters in a specific location in Word 2010 Windows 7 64bit Deleting Characters in a specific location in Word 2010 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The code works fine with the sample document attached to your previous post.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 12-13-2016, 08:11 AM
ppayaw ppayaw is offline Deleting Characters in a specific location in Word 2010 Windows 10 Deleting Characters in a specific location in Word 2010 Office 2010 64bit
Novice
Deleting Characters in a specific location in Word 2010
 
Join Date: Dec 2016
Posts: 5
ppayaw is on a distinguished road
Default

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
Reply With Quote
Reply



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 Characters in a specific location in Word 2010 Deleting A blank Line that has a specific heading style , word 2010 & 2013 SteveWcg Word 5 01-08-2014 10:37 PM
Deleting Characters in a specific location in Word 2010 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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:46 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft