Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #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
  #2  
Old 03-02-2024, 01:48 PM
vivka vivka is offline Deleting the first word of a second paragraph Windows 7 64bit Deleting the first word of a second paragraph Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, ctviggen! Try this simple code:
Code:
Sub Del_Wd()
      ActiveDocument.range.Paragraphs(2).range.words(1).Delete
End Sub
Reply With Quote
  #3  
Old 03-09-2024, 11:02 AM
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

Thank you for that, and sorry for my delay in responding. I often only have weekends to work on this, then not much time.

Anyway, I have a variable oRng, which I did not discuss. After adding text and pasting text in, oRng has the range of the text that has been added and pasted.

So, I modified your code to be "oRng.paragraphs(2).Range.Words(1).Delete".

This worked...a bit.

We're using numbered paragraphs, my test document has something like "[00344](tab)", the numbered paragraph of [00344] followed by a tab character, then it's blank. I add some text, add a new paragraph, then paste selected text in and manipulate it. After doing this without the code above:

[00344](tab)Text I added
[00345](tab)[00345] Beginning of what was pasted in...

The "[00345]" is repeated for some reason. Only happens here. All the following paragraphs are fine.

If I use the "oRng.paragraphs(2).Range.Words(1).Delete" to delete the first word of the second paragraph, this is what happens:


[00344](tab)Text I added
[00345](tab)00345] Beginning of what was pasted in...


The beginning bracket [ is removed. If I attempt to delete the second "word" with "oRng.paragraphs(2).Range.Words(2).Delete", I get the following:


[00344](tab)Text I added
[00345](tab)00345 Beginning of what was pasted in...

I have deleted the beginning bracket [ and the ending bracket ], but not the "word" 00345.

Maybe the "00345" isn't a "word"? But it seems to be just numbers that are bold (because our paragraph numbering is bold). I can manually delete this with no issues. I tried (re)applying the style to that paragraph, nothing happens.

I could possibly delete by characters, so I tried the following:

Code:
     Dim tRng As Word.Range
     Set rTng = oRng.paragraphs(2).Range
     tRng.Collapse Direction:=wdCollapseStart
      tRng.Delete (wdCharacter,8)

But I can't get the syntax of the last part correct. (The code above generates an error.) To me, this looks like "expression.Delete([Unit], [Count])", which is here: Range.Delete method (Word) | Microsoft Learn

Unfortunately, there's no example there of using a Unit, Count. I'm at a loss as to how to delete 8 characters.
Reply With Quote
  #4  
Old 03-09-2024, 11:05 AM
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

Oops, that "rTng" should be tRng. But I still get an error on the last line of code.
Reply With Quote
  #5  
Old 03-10-2024, 05:17 AM
vivka vivka is offline Deleting the first word of a second paragraph Windows 7 64bit Deleting the first word of a second paragraph Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

ctviggen, what exactly do you want to delete in

[00344](tab)Text I added
[00345](tab)[00345] Beginning of what was pasted in...?

Everything until or after the tab? Or maybe sth else?
If you want to delete everything until after the tab in the selected paragraphs, try this:
Code:
Sub Del_Until()
Dim rng As range, oPar As Paragraph
    
Application.ScreenUpdating = False
    Set rng = selection.range
    For Each oPar In rng.Paragraphs
        oPar.range.Select
        selection.MoveEndUntil cset:=vbTab, count:=wdBackward
        selection.Delete
    Next oPar
Application.ScreenUpdating = True
Set rng = Nothing
 End Sub
If you want to delete everything until after the tab in the 2nd paragraph of the selection, try this:
Code:
Sub Del_Until()

    selection.Paragraphs(2).range.Select
    selection.MoveEndUntil cset:=vbTab, count:=wdBackward
    selection.Delete
Set rng = Nothing
End Sub

Last edited by vivka; 03-10-2024 at 11:10 AM.
Reply With Quote
  #6  
Old 03-10-2024, 07:22 AM
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

Thank you.



Basically, this is what I get:


[00344](tab)Text I added
[00345](tab)[00345] Beginning of what was pasted in...


This is what I want:


[00344](tab)Text I added
[00345](tab)Beginning of what was pasted in...


That is, the second "[00345]" is something that happens, I'm not sure why. I just want to delete this, since it's not supposed to be there and is an artifact of pasting (not sure WHY it occurs, though).


Let me see if I can modify your code to do this.
Reply With Quote
  #7  
Old 03-10-2024, 07:50 AM
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

I tried to get this done, and I'm getting closer, but I have to go to family stuff. I'll see if I can come back to this.


But, in pseudo-code, this is what I'm trying to do (where the variable oRng has the range of everything I've added/pasted in):



1- Go to the second paragraph of the range oRng
2- Start at the beginning of the paragraph, go 8 characters or to the character before the word "Example". Select these characters.
3- Delete the selected characters.



Seems super easy, but is harder than it appears.



Thank you for the help.
Reply With Quote
  #8  
Old 03-10-2024, 11:22 AM
vivka vivka is offline Deleting the first word of a second paragraph Windows 7 64bit Deleting the first word of a second paragraph Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Try the following:
Code:
Sub Del_Test()
Dim oRng As range
    Set oRng = selection.Paragraphs(2).range
    If InStr(oRng, "Example") Then
        oRng.Collapse
        oRng.MoveEndUntil cset:="Example" 
    Else
        oRng.Collapse
        oRng.MoveEnd unit:=wdCharacter, count:=8
    End If
    oRng.Delete
Set oRng = Nothing
End Sub
Reply With Quote
  #9  
Old 03-12-2024, 06:00 AM
vivka vivka is offline Deleting the first word of a second paragraph Windows 7 64bit Deleting the first word of a second paragraph Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

This is better. Since cset recognizes characters, not strings, "Example" is useless:
Code:
Sub Del_Test()
Dim oRng As range
    Set oRng = selection.Paragraphs(2).range
    If InStr(oRng, "Example") Then
        oRng.Collapse
        oRng.MoveEndUntil cset:="E"
    Else
        oRng.Collapse
        oRng.MoveEnd unit:=wdCharacter, count:=8
    End If
    oRng.Delete
Set oRng = Nothing
End Sub
Reply With Quote
Reply



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 12:51 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