none
how to copy the text of a range from the active document and paste it in a new blank word document? RRS feed

  • Question

  • Hi Buddies!

     

    I am working a a procedure whose task is to loop trough the paragraphs collection of the selected word document

    and if it finds a paragraph whose style property is equal to "heading 1", type the text of that paragraph into the new document.

    I don't know what properties/methods of the range object to use to set the range from the beginning of the found paragraph object to the end

    of it nor how to copy and paste the range into the new documents

    if anyone could give me a hand I will appreciate tons.

    here is a snippet of my code to give a better idea of what I am trying to accomplish

     

    Private Sub btnListHeadings_Click()
    
    Dim IntSelection As Integer
    Dim i As Integer, pp As Paragraph, MyRange As Range
    
    IntSelection = lstDocuments.ListIndex
    
    If IntSelection = -1 Then
        MsgBox "must select a document from the listbox", vbOKOnly + vbInformation, "Select a Document"
        Exit Sub
    Else
        Documents.Add DocumentType:=wdNewBlankDocument, Visible:=True
        
    For Each pp In Documents(lstDocuments.Value).Paragraphs
    
        If Documents(lstDocuments.Value).Paragraphs(pp).Style = "Heading 1" Then
            Set MyRange = Documents(lstDocuments.Value).Range.StartOf(wdParagraph)
    MyRange.end = Documents(lstDocuments.Value).Range.EndOf(wdParagraph)

     End If Next pp End If End Sub

    thanks

     


    • Edited by flashy_lord Tuesday, September 20, 2011 4:58 PM
    Tuesday, September 20, 2011 3:05 AM

All replies

  • Hi

    I think if you loop through the document and find the paragraphs it will be bit time consuming. There are two alternate approach

     

    1) Search for the Style - using Content.Find

    2) Prepare a TOC for the given styles (this will be applicable for Heading styles easily) and you need not grab any thing - the Heading will comeout as ToC and you can copy and paste the content to a new doc

     

    Cheers

    Shasur

     

     


    http://www.vbadud.blogspot.com http://www.dotnetdud.blogspot.com
    Tuesday, September 20, 2011 12:33 PM
  • I am required to loop trough the paragraphs collection object, since is a college assignment. better of all, the document I have to loop through is only 5

    paragraphs long. what I want to know if the way I am doing it is correct. I yet have to figure out how to copy a range once defined with a starting and ending

    position

     

    thanks

    Tuesday, September 20, 2011 4:55 PM