I have code that when I copy text into my document, it introduces an error in the "second" paragraph. That is, I add text as one paragraph, I then paste text into the second paragraph (and many more). These paragraphs have a numbered style, eg, "[0100] Paragraph text", and for some reason, when I paste text into the document, it ends up with "[0100] [0100] Paragraph Text", where I want to delete the second "[0100]". (NOTE: The text I'm copying in is pasted in as text without these paragraph numbers. I paste in many paragraphs, and only the first pasted para -- which is the second para of selected range -- has this error.)
So, I want to delete the first word (+ any spaces until the second word) of the second paragraph. Seems easy, right? Well, I've worked for a few hours on it.
The oRng has the selected range in the code below. You can see one delete statement that deletes the first character in the second paragraph. I show another delete statement that deletes the entire second paragraph.
This code looks for a space. Should there be additional code to look after the space for another character that is not a space (what if there are two spaces?)? Then once that "i" is known, delete characters up to i-1? That makes sense to me, but I'm not sure how to do this.
Code:
Dim firstWordEnd As Long
Dim secondPara As Paragraph
Dim i As Long
' Get the second paragraph in the range
Set secondPara = oRng.paragraphs(2)
' Check if there's at least one character in the paragraph
If secondPara.Range.Characters.Count > 1 Then
' Loop through the characters in the second paragraph range
For i = 1 To secondPara.Range.Characters.Count
' Check if the character is a word separator (space)
If secondPara.Range.Characters(i) = " " Then
' Delete the range up to the end of the first word
' The following deletes the first character of the second para
' secondPara.Range.Characters(1).Delete
' The following deletes the entire second para
secondPara.Range.MoveStart unit:=wdCharacter, Count:=i
secondPara.Range.Delete
Exit For
End If
Next i
End If