Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recorded Word Macro, won't run again from Macros 1

Status
Not open for further replies.

atyrautony

Technical User
May 21, 2020
2
0
0
KZ
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
 
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]
 
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
 
 https://files.engineering.com/getfile.aspx?folder=17fe6ab1-fc16-4bcf-ae68-675eea14451d&file=TOLI-DT-W8419-T025-006.docx
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]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top