• Need help with simple macro

    Author
    Topic
    #493019

    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
    Viewing 1 reply thread
    Author
    Replies
    • #1435211

      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

      • #1435322

        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.

    • #1435339

      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
    Viewing 1 reply thread
    Reply To: Need help with simple macro

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: