Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    May 2016
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Copy Paragraphs Containing Words To Document - Delete Old Paragraphs

    Hello Folks,

    how is everyone

    I would like to copy some paragraphs, but being new to vba macros I'm not sure if i am doing it the right way.


    Copy all paragraphs containing

    Bell Flower

    Blue Throatwort

    Amaryllis

    Delphinium

    To a new document and then delete the paragraphs from the old document

    Here is what I have so far as due to my understanding of macro.

    Code:
    Sub CopyParagraphs()
    
    Dim oPara As Range
    Dim oRng As Range
    Dim oTarget As Document
    
    
      Set oTarget = Documents.Open(FileName:=C:\Users\Lilly\Desktop\Flowers.docx", Visible:=False)
    
            
       For Each oPara In ActiveDocument.Paragraphs
        
       If InStr(1, oPara.Range, "Bell Flower") > 0 Then
       If InStr(1, oPara.Range, "Blue Throatwort") > 0 Then
      
            
       oRng.Insert
        
       Cut the Paragraph from the main document
            
        End If
        
        Next oPara
    
    End Sub
    I recorded a macro but i am sure i did something wrong as it was a lot of code

    please help, thank you

    lilly
    Last edited by lilly; 2016-05-23 at 13:21.

  2. #2
    3 Star Lounger
    Join Date
    May 2010
    Location
    Philadelphia, PA, USA
    Posts
    208
    Thanks
    3
    Thanked 27 Times in 26 Posts
    I am not an expert in VBA but what you are doing is rather easily done by recording a macro.

    First make a copy of the file you are going to work with and save it under another name. Then open a new blank document. Then record a macro selecting ONE paragraph with the FIRST phrase you have listed, copying it to the clipboard, and pasting into the new document.

    If that works correctly, just keep running it until you have copied all such paragraphs.

    Then you can open the VBA editor and see the generated code and substitute the next phrase you want to use. This procedure will accomplish what you want to do and also show you how to write the code. I don't know if VBA allows you to do this but once you have this bit of code working correctly, you can use it as the basis of a User Defined Function which you can use any time you need to do the same thing.

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by nate01pa View Post
    I am not an expert in VBA but what you are doing is rather easily done by recording a macro.
    No, it isn't. The macro recorder is about as dumb as a box of rocks; it doesn't record everything one might need to do for this and what it does record is specific to that instance only.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by lilly View Post
    I would like to copy some paragraphs, but being new to vba macros I'm not sure if i am doing it the right way.


    Copy all paragraphs containing

    Bell Flower

    Blue Throatwort

    Amaryllis

    Delphinium

    To a new document and then delete the paragraphs from the old document
    Lilly: it's not clear how a macro would help with this. If you're simply replacing one set of paragraphs with another, surely it would be a simple task to copy, say the new 'Bell Flower' paragraph, find 'Bell Flower' in the existing document and, if it's the right paragraph, select and replace it. I imagine that, if you have 5 'Bell Flower' paragraphs, only one would be replaced or, if they differ and each of them needs replacing, you'd have a different replacement paragraph for each of them. It's also not clear from your post how the macro is supposed to identify what each new paragraph relates to. Is there some kind of indexing, for example?
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    New Lounger
    Join Date
    May 2016
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hello Paul,

    I recorded a macro but it was very unfriendly looking and i didn't understand it.

    bell flower.png


    I am trying to extract all the paragraphs that have the word bell flower into another document, delete from original the paragraphs we copied.

    now lets go onto Amarylis - Find all the paragraphs that have that word in and copy it over - delete from original.

    now lets find Delfinuim and do the same.

    Well i have been copying and pasting for 2 days now and

    I've got a long way to go

    does that picture make sense above, i hope a macro program could help me
    thank you

    lilly

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    OK, but for a given source paragraph, how is Word to recognise what keywords (i.e word or phrase) it's looking for? It's all very well for you to know the paragraph might contain the Bell Flower, Blue Throatwort, Amaryllis, Delphinium, etc. keywords, but how is Word to recognise what is important about a given paragraph?

    It's also unclear from your latest post whether you're starting off with a single document containing some boilerplate text plus your paragraphs containing your keywords and trying to:
    • copy the paragraphs containing your specified text into another document that already contains paragraph with the keywords; or
    • create a series of documents that have the boilerplate text and the corresponding keyword paragraphs (without all the other keyword paragraphs).
    Perhaps you could clarify.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #7
    New Lounger
    Join Date
    May 2016
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hello Paul,

    I apologise i didnt realise it would be this complicated.


    All the content Starts off in 1 document - the master document.

    So I am searching for the paragraphs - when I find all the paragraphs containing bell flower, I copy those paragraphs only into BellFlowers.docx
    Delete them from the original document.

    Next I will search for all the paragraphs containing Amarylis - copy them to a document called Amarylis.Docx
    Delete old paragraphs from main document...and on to next

    I am sorry for not being able to describe what im doing.

    Good old copy and paste is very long process like a slow tortoise
    thank you

    Lilly

  8. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    What are the 'original document' and 'main document' and how do these relate to the 'master document'? Your descriptions are opaque.

    if the 'original document' and 'main document' are not the 'master document', how is the code to recognise which paragraph(s) to delete from the 'original document' or 'main document' for any given found instance of a paragraph containing 'Bell Flower' in what you call the 'master' document?
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. #9
    New Lounger
    Join Date
    May 2016
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi Paul,

    oops - yes my language is not the best of explanators, if thats a word.

    There is only one master document it contains all the paragraphs. So this would be the original document.

    It would be deleted from this.

    So currently I am copying paragraphs from this document into other documents, then deleting it from original master.



    lilly

  10. #10
    New Lounger
    Join Date
    May 2016
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi Paul,
    hope below makes sense.

    Master Document
    (the one i am copying from) - > Copy Bell Flower paragraphs -> Bell flower.docx

    Delete From Master

    Next

    Copy Amarylis paragraphs -> Amarylis.docx

    Delete From Master


    There is only 1 master document

    lilly

  11. #11
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    So, something like:
    Code:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long, StrFnd, RngSrc As Range, RngTgt As Range, DocSrc As Document, DocTgt As Document
    StrFnd = Array("Bell Flower", "Blue Throatwort", "Amaryllis", "Delphinium")
    Set DocSrc = ActiveDocument
    For i = 0 To UBound(StrFnd)
      Set DocTgt = Documents.Add
      With DocSrc.Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = StrFnd(i)
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchCase = False
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
          .Execute
        End With
        Do While .Find.Found
          Set RngSrc = .Paragraphs(1).Range
          Set RngTgt = DocTgt.Range.Characters.Last
          RngTgt.Collapse wdCollapseStart
          RngTgt.FormattedText = RngSrc.FormattedText
          .Start = RngSrc.End
          .Find.Execute
        Loop
      End With
      DocTgt.SaveAs2 FileName:=DocSrc.Path & "\" & StrFnd(i) & ".docx", _
        FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
      DocTgt.Close False
    Next
    Application.ScreenUpdating = True
    End Sub
    Note that this code leaves your 'master' document alone. If you really want to delete it's contents, you could insert:
    DocSrc.Range.Delete
    after:
    Next
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  12. The Following User Says Thank You to macropod For This Useful Post:

    lilly (2016-05-25)

  13. #12
    New Lounger
    Join Date
    May 2016
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hello Paul,

    I have briefly tested it and......

    oh my gosh, i was not expecting that at all.

    You have taken my breath away - it created the documents and copied the paragraphs like magic.

    I am so happy, I am on permanent copy paste ban now!!

    I had no idea what a vba was until I had this colossal job to do,

    and it took me ages to find the developer tab.

    I have copied and pasted about 200 pages so far.

    It's an old book material that has to be updated, so a lot of the text has to be extracted over to new documents.


    I didn't think copy and pasting could be this hard but after about 30 times my hand began to hurt,
    as the cursor kept jumping and the keyboard button was hard like a rock.

    You are a sweet heart thank you for helping me.


    Thank you.gif

    Lilly

Posting Permissions

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