![]() |
|
#1
|
|||
|
|||
![]()
Hi,
I have the GRE words list that I have copied from a PDF file to Word. First character of each line in this Word file is NOT an alphanumeric one or Tilde (~). If they are, I want to move to next line, otherwise delete that character and the space joining it. I need to do this to all lines. Here is my Code and this works fine only for first few lines and goes for a toss: -------------------------------------- Code:
Sub FormatData() 'Declare variables Dim numOfLines As Integer Dim ascVal As Integer Dim doc As Document Set doc = ActiveDocument 'Count the number of non blank lines In current document numOfLines = ActiveDocument.BuiltInDocumentProperties("NUMBER OF LINES") 'Move to start of document Selection.HomeKey Unit:=wdStory 'ascVal = (Asc(doc.Sentences(58).Characters(1).Text)) 'MsgBox (ascVal) For x = 1 To numOfLines 'MsgBox (x) ascVal = (Asc(doc.Sentences(x).Characters(1).Text)) 'MsgBox (ascVal) Selection.HomeKey Unit:=wdLine If Not (((ascVal >= 65 And ascVal <= 90) Or (ascVal >= 97 And ascVal <= 122) Or (ascVal >= 48 And ascVal <= 57) Or (ascVal = 126))) Then 'MsgBox (doc.Sentences(x).Characters(1).Text) Selection.Delete Unit:=wdCharacter, Count:=2 Selection.MoveDown Unit:=wdLine, Count:=1 Selection.HomeKey Unit:=wdLine 'MsgBox (x) Else Selection.MoveDown Unit:=wdLine, Count:=1 Selection.HomeKey Unit:=wdLine End If 'Selection.HomeKey Unit:=wdLine Next End Sub Thanks.....Karthik.... Last edited by macropod; 03-15-2014 at 05:01 AM. Reason: Added code tags & formatting |
#2
|
||||
|
||||
![]()
Try:
Code:
Sub Demo() Application.ScreenUpdating = False With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "^13[!A-Za-z0-9~] " .Replacement.Text = "^p" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Thanks. Paul.
These are the ones I want to achieve: 1. I want to first join all lines that start with an alphabet or Tilde (~) to their previous lines. 2. Remove the first character and its joining spaces in each line if the character is not alphanumeric or Tilde (~) - You have given the code for this but it leaves the first line and does the changes for all remaining lines. The code I am trying for 1st point which I got from the net is: -------------------------------------------------------------------- Code:
Sub LoopingText() 'Will move to the end of each line in the document and move the text to match 'Declare variables Dim outputStr As String Dim currLine As String Dim endChar As String Dim numOfLines As Integer Dim ascVal As Integer 'Count the number of non blank lines in current document numOfLines = ActiveDocument.BuiltInDocumentProperties("NUMBER OF LINES") 'Move to start of document Selection.HomeKey Unit:=wdStory 'Start the loop - looping once for each line For x1 = 1 To numOfLines 'MsgBox (x1) 'ascVal = (Asc(doc.Sentences(x1).Characters(1).Text)) ascVal = (Asc(ActiveDocument.Sentences(x1).Characters(1).Te xt)) 'Move to start of line Selection.HomeKey Unit:=wdLine 'Select entire line and copy into variable currLine Selection.EndKey Unit:=wdLine, Extend:=wdExtend currLine = Selection.Range.Text 'Remove final character (line break) from currLine currLine = Left(currLine, (Len(currLine) - 1)) 'Check to see if character currently on end of outputStr is " " (space) endChar = Right(outputStr, 1) If x1 = 1 Then 'Add the current line to the variable outputStr 'Since this is the first run through, don't add a space outputStr = outputStr & currLine Selection.MoveDown Unit:=wdLine, Count:=1 Selection.HomeKey Unit:=wdLine Else If Not (endChar = " ") And (((ascVal >= 65 And ascVal <= 90) Or (ascVal >= 97 And ascVal <= 122) Or (ascVal >= 48 And ascVal <= 57) Or (ascVal = 126))) Then 'If preceding line does not have a space at the end 'Add current line to the variable outputStr outputStr = outputStr & " " & currLine Selection.EndKey Unit:=wdLine Selection.TypeParagraph Selection.MoveDown Unit:=wdLine, Count:=1 Selection.HomeKey Unit:=wdLine Else Selection.MoveDown Unit:=wdLine, Count:=1 Selection.HomeKey Unit:=wdLine End If End If 'Move down one line Selection.MoveDown Unit:=wdLine, Count:=1 'Move to the next part of the loop Next x1 'Finally 'paste' outputStr onto the document ActiveDocument.Range = outputStr ActiveDocument.Save End Sub This code just joins all lines together irrespective of my conditions. Please help....Thanks....Karthik.... Last edited by macropod; 03-15-2014 at 05:09 AM. Reason: Added code tags & formatting |
#4
|
||||
|
||||
![]()
Try:
Code:
Sub Demo() Application.ScreenUpdating = False With ActiveDocument.Range .InsertBefore vbCr With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "^13[A-Za-z0-9~]" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute Replace:=wdReplaceAll .Text = "^13[!A-Za-z0-9~] " .Replacement.Text = "^p" .Execute Replace:=wdReplaceAll End With .Characters.First = vbNullString End With Application.ScreenUpdating = True End Sub PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
Thanks Paul. The code works fine for my 2nd point.
But I am not able to achieve my 1st point. Previously, I searched for first line to be any character other than alphanumerics or tilde. Now, after your recent code that does its magic ![]() Please help on this and you can see the code that I use in Sub joinLines(). Cheers.... |
#6
|
||||
|
||||
![]()
In my last post, I asked that you:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
Tags |
delete first character |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Opul3nce | Excel | 1 | 10-15-2012 11:11 PM |
![]() |
mauricioaglr | Excel | 7 | 03-09-2012 06:45 PM |
![]() |
Gwen Butler | Office | 2 | 09-29-2011 06:10 AM |
![]() |
Wskip49 | Word | 5 | 08-28-2011 07:54 PM |
![]() |
stnicholas81 | Excel | 1 | 07-25-2011 01:31 AM |