Microsoft Word macro help needed

viralnerd2013

New Member
Joined
Apr 8, 2013
Messages
21
Hello Mr. Excel users,

I am trying to create a macro that will go through a word document and find all paragraphs that begin with "Comment:", copy these paragraphs, and paste them all at the end of the document. The paragraphs could range from 1-5 sentences.

Any ideas?

Thanks and Happy New Year!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You can loop paragraphs by using the activedocument.paragraphs.count property. Set a temporary range for each paragraph, search each range using Find for "Comment", if found add the paragraph to a string variable. Add the completed string variable (containing all found "comment" paragraphs) at the end of the document. HTH. Dave
 
Upvote 0
Thanks Dave,

Unfortunately, being a newby, I do not know how to do what you suggested. Can you provide a few lines of code that I could tinker with?

Thanks,
Eric
 
Upvote 0
Here's some untested code. Please stay with the original thread. HTH. Dave
Code:
Sub test()
Dim Wapp As Object, Bigstring As String, Mydata$, SourceFile As String
Dim MyRange As Variant, Cnt As Integer

Bigstring = vbNullString 'combines paragraphs
SourceFile = "C:\TestFolder\Test.doc" 'change file path to suit

'Open source file and search
On Error GoTo FixErr
Set Wapp = CreateObject("Word.Application")
Wapp.Documents.Open FileName:=SourceFile, ReadOnly:=True
Wapp.ActiveDocument.Select
'loop to find all keywords in doc.
For Cnt = 1 To Wapp.Selection.paragraphs.Count
Set MyRange = Wapp.ActiveDocument.paragraphs(Cnt).Range
MyRange.SetRange Start:=MyRange.Start, _
    End:=Wapp.ActiveDocument.paragraphs(Cnt).Range.End
MyRange.Select

With Wapp.Selection.Find
.Text = "Comment:"
.Forward = True
.Execute
If .found = True Then
Mydata = MyRange.Text
End If
End With

'store in bigstring
Bigstring = Bigstring + Mydata
Mydata = vbNullString
Next Cnt

With Wapp.ActiveDocument
    .Content.InsertAfter Bigstring
End With
Exit Sub

FixErr:
On Error GoTo 0
MsgBox "error"
Wapp.ActiveDocument.Close savechanges:=False
Wapp.Quit
Set Wapp = Nothing
End Sub
 
Last edited:
Upvote 0
I'd take a simpler approach with a Word macro that's run from the document template or the source document itself and which also preserves the formatting of the copied content:
Code:
Sub Demo()
Dim i As Long, Rng As Range
With ActiveDocument
  Set Rng = .Range
  Rng.Collapse wdCollapseEnd
  For i = .Paragraphs.Count To 1 Step -1
    With .Paragraphs(i).Range
      If InStr(.Text, "Comment:") = 1 Then
        .Copy
        With Rng
          .Paste
          .Collapse wdCollapseStart
        End With
      End If
    End With
  Next
End With
End Sub
For Word macro installation & usage instructions, see: Installing Macros
 
Upvote 0
That's some cool stuff with the Instr range search thing. Maybe the following code could be added within the loop after the paste in case MS wants to help you out... and overfills your clipboard. The paste also won't be available after U leave your pc. Dave
Code:
Application.CutCopyMode = False
 
Upvote 0
I've never encountered any issues with the clipboard overflowing, though one might want to insert 'DoEvents' before the 'Next' line to give Word some breathing space for its own overheads in documents requiring a lot of comment copying/pasting.
 
Upvote 0

Forum statistics

Threads
1,225,661
Messages
6,186,288
Members
453,348
Latest member
newbieBA

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top