Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-02-2024, 12:07 PM
ctviggen ctviggen is offline Deleting the first word of a second paragraph Windows 10 Deleting the first word of a second paragraph Office 2016
Advanced Beginner
Deleting the first word of a second paragraph
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default Deleting the first word of a second paragraph

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting all text from a slide pack without deleting the formatting redordead2389 PowerPoint 0 05-31-2023 06:50 AM
Deleting the first word of a second paragraph Word macro for deleting a line that starts with a specific character + deleting the line before eduardb Word 1 08-10-2022 03:17 AM
Inserting text from a Userform into a Field in a paragraph in a paragraph in a word document storemaz Word VBA 1 03-13-2020 08:11 AM
Deleting the first word of a second paragraph Deleting paragraph commands afndst Word VBA 2 12-30-2015 02:07 AM
Deleting the first word of a second paragraph Deleting a script in an equation without deleting the whole term allankey Word 1 02-15-2014 07:09 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:32 AM.


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