Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    delete part of a para under vb (all)

    I know I should know this but how do I delete some text from a paragraph? For example, if I set myRange to a paragraph and then I want to delete say 10 characters from it - how would i do it? Doesn't necessarily have to be the first 10 characters; could be from anywhere in the para.

    Dim myRange As Range

    'start with first paragraph
    Set myRange = ActiveDocument.Paragraphs(1).Range
    If LCase(Left(myRange.Paragraphs(1).Range.Text, 10)) = "SubSystem|" Then
    'remove the 'SubSystem|' from the para and continue
    ?
    endif
    Thanks.
    Robie

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: delete part of a para under vb (all)

    You can use something like this instead:

    Dim myRange As Range

    'start with first paragraph
    Set myRange = ActiveDocument.Paragraphs(1).Range
    myRange.Find FindText:="SubSystem|", ReplaceWith:="", Replace:=wdReplaceOne

    If you want to replace all occurrences within the paragraph, use wdReplaceAll instead of wdReplaceOne.

  3. #3
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: delete part of a para under vb (all)

    Thanks Hans that's perfect.

    Sorry, I have got another question. If I am going through a document one paragraph at a time, how can I keep a track of where I am if I want to insert something in between. Let me explain bit more:

    Para1
    Para2
    Para3
    Para4
    Para5
    Para6
    Para7
    Para8
    Para9

    Using range by the way:
    1. Process Para1 and Para2, extract content from the paras, delete them - I have no problems so far
    2. Then I would like to insert a table before Para3 (with extracted contents from paras 1 and 2). BTW: I have created a table as an Autotext entry with all spacing, styles, etc set
    3. Leave Para3 after the table
    4. Loop back to 1 line in this process (i.e. process Para4 onwards) & restart again

    The output mught look something like this after processing
    TableCell 1,1 | TableCell 1,2
    TableCell 2,1 | TableCell 2,2
    Para3
    TableCell 1,1 | TableCell 1,2
    TableCell 2,1 | TableCell 2,2
    Para6
    TableCell 1,1 | TableCell 1,2
    TableCell 2,1 | TableCell 2,2
    Para9

    Hope this makes sense. Can't give you a sample yet unfortunately.
    Thanks.
    Robie

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: delete part of a para under vb (all)

    This could become confusing. Perhaps it would be better to leave the original document unchanged while you loop through its paragraphs, and to fill a new document with the content you need.

    Dim docOld As Document
    Dim docNew As Document
    Set docOld = ActiveDocument
    Set docNew = Documents.Add

    Instead of using ActiveDocument, use docOld to refer to the original document. You can add the table to the new document, then copy/paste a paragraph, etc.

  5. #5
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: delete part of a para under vb (all)

    Wonderful. Thanks.

    So copy & paste should be a simple process - I assume.
    Thanks.
    Robie

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: delete part of a para under vb (all)

    To copy a paragraph from the old document and paste it at the end of the new one:
    <code>
    docOld.Paragraphs(n).Range.Copy
    Dim rng As Range
    Set rng = docNew.Content
    rng.Collapse Direction:=wdCollapseEnd
    rng.Paste
    </code>
    (n is a paragraph number)

  7. #7
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: delete part of a para under vb (all)

    Thanks Hans. All works wonderfully well now. Your idea about creating a new doc an copying stuff across was inspired. Thanks again.
    Thanks.
    Robie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •