Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Oct 2007
    Posts
    171
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Need help with simple macro

    hi all, I have the following code which isnt working because the "piece" range in the collection fails on the Piece.copy method. I cant think of what I am doing wrong. Help appreciated!!

    cheers,
    Dom

    Code:
    Public Sub GenerateDocument(Tag As String)
     
        Dim createdDocument As Document
        Dim outputContent As New Collection
        Dim i As Integer
        Dim piece As Range
        
        'Select the document
        ActiveDocument.Range(Start:=0, End:=ActiveDocument.Content.End).Select
        
     On Error GoTo err:
        'find all the matching bits
        With Selection.Find
            .ClearFormatting
            .Text = "\[" + Tag + "\]*\[\/"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
            While .Execute
               outputContent.Add (Selection.Range)
            Wend
        End With
       
        ' create a new document
        Set createdDocument = Application.Documents.Add
        'put the content in it
        For Each piece In outputContent
            piece.Copy
            createdDocument.Range.Paste
        Next piece
                 
        Exit Sub
        
    err:
       MsgBox "GenerateDoc: " & err.Desciption
    End Sub

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Gold Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,425
    Thanks
    7
    Thanked 211 Times in 200 Posts
    I think it's because "piece" is defined as a range, not content, and you are trying to copy the range instead of selecting it and copying the selection.

    cheers, Paul

  4. #3
    2 Star Lounger
    Join Date
    Oct 2007
    Posts
    171
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nope, that can't be the problem.

    The first line of the following fails saying that the object doesnt support the method. so Piece is always nothing. OUtputContent is full of all the stuff I need to paste into a different document.

    Code:
    For Each piece In outputContent
            piece.Copy
            createdDocument.Range.Paste
    Next piece
    if I do

    Code:
        For i = 1 To outputContent.Count
            outputContent(i).Select
            Selection.Copy
            createdDocument.Range.Paste
        Next i
    instead, then the select fails.

  5. #4
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    193
    Thanks
    4
    Thanked 45 Times in 37 Posts
    Why are you stuffing everything into a collection and then pulling it out again? It's more efficient (and has fewer mysterious problems) if you just transfer each piece as you find it.

    There are other things here to think about. First, using Selection.Find forces Word to scroll and render pages, which slows the execution -- often by an order of magnitude. Instead, use a range object's .Find, which doesn't cause any scrolling. Second, your pasting would put all of the found items one after the other, without any intervening space or paragraph mark. Third, each pasted item will end with an incomplete tag, just the left bracket and the slash.

    Try this replacement:

    Code:
    Public Sub GenerateDocument(Tag As String)
     
        Dim createdDocument As Document
        Dim rg As Range
    '    Dim destRg As Range ' see "alternatively" comment below
        
        Set rg = ActiveDocument.Range
        Set createdDocument = Application.Documents.Add
    '    Set destRg = createdDocument.Range
        
     On Error GoTo err:
        'find all the matching bits
        With rg.Find
            .Text = "\[" + Tag + "\]*\[\/"
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchWildcards = True
            While .Execute
               createdDocument.Range.InsertAfter rg.Text & vbCr
               ' alternatively, to include formatting:
    '           destRg.FormattedText = rg.FormattedText
    '           destRg.InsertAfter vbCr
    '           Set destRg = createdDocument.Range
    '           destRg.Collapse wdCollapseEnd
            Wend
        End With
        Exit Sub
        
    err:
       MsgBox "GenerateDoc: " & err.Desciption
    End Sub

Posting Permissions

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