×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Recorded Word Macro, won't run again from Macros

Recorded Word Macro, won't run again from Macros

Recorded Word Macro, won't run again from Macros

(OP)
Baffled, recorded this macro, which achieves the result required. But trying to rerun the macro error of no selection. I think its a brain fade day. Where is the correction needed.
Sub copygreentexttonewdoc()
'
' copygreentexttonewdoc Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Font.Color = 5287936
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Copy
Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
Selection.PasteAndFormat (wdFormatOriginalFormatting)
End Sub

RE: Recorded Word Macro, won't run again from Macros

You macro will only work if something is selected when you run it. Moreover, what gets copied & pasted to the new document will be whatever your selection is; it has nothing to do with your Find code.

Try:

CODE

Sub Demo()
Application.ScreenUpdating = False
Dim DocSrc As Document, DocTgt As Document, i As Long
Set DocSrc = ActiveDocument: Set DocTgt = Documents.Add
With DocSrc.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .Font.Color = 5287936
  End With
  Do While .Find.Execute
    i = i + 1
    DocTgt.Range.Characters.Last.FormattedText = .Duplicate.FormattedText
    With DocTgt.Range
      If .Characters.Last.Previous <> vbCr Then .InsertAfter vbCr
    End With
    If .End = ActiveDocument.Range.End Then Exit Do
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances copied."
End Sub 

Cheers
Paul Edstein
[MS MVP - Word]

RE: Recorded Word Macro, won't run again from Macros

(OP)
Paul, great code but it appears to get looped in
Do While .Find.Execute
i = i + 1
DocTgt.Range.Characters.Last.FormattedText = .Duplicate.FormattedText
With DocTgt.Range
If .Characters.Last.Previous <> vbCr Then .InsertAfter vbCr
End With
If .End = ActiveDocument.Range.End Then Exit Do
.Collapse wdCollapseEnd
Loop

It doesn't do the exit?

I have attached a typical source file

RE: Recorded Word Macro, won't run again from Macros

Without wanting to over-emphasize it, you didn't say anything about the content being in tables. Before:

CODE

If .End = ActiveDocument.Range.End Then Exit Do 
insert:

CODE

If .Information(wdWithInTable) = True Then
      If .End = .Cells(1).Range.End - 1 Then
        .End = .Cells(1).Range.End
        .Collapse wdCollapseEnd
        If .Information(wdAtEndOfRowMarker) = True Then
          .End = .End + 1
        End If
      End If
    End If 

Cheers
Paul Edstein
[MS MVP - Word]

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close