troubleshooting Question

Microsoft Word Macro to Find, Copy and Paste Text with Specific Style to a new Document

Avatar of james_tubberville
james_tubbervilleFlag for United States of America asked on
Visual Basic ClassicMicrosoft WordVB Script
2 Comments1 Solution3774 ViewsLast Modified:
I'm attempting to take a very large, constantly changing document and automate several functions by searching for a style and copying the text to a new document.
Each document section is constantly changing references and acronyms (among others) depending on the type of work being performed. Styles have been applied to both the reference and acronyms.

For example:
Acronym: Big Fluffy Dog (BFD) -- Acronym Style applied
Reference: Employee 1 modifies a section. At the end of the section he references Policies 1 Paragraph 3.3 and 3.4 and Policy 2 Paragraph 4.1 as applied (REF: Policy1 3.3, 3.4; Policy2 4.1) -- Reference Style applied

This would produce
Policy1 3.3 Page 5
Policy1 3.4 Page 5
Policy2 4.1 Page 8

Theoretically, I should be able to run a macro that copies the reference with the applied style to a new document and sort. Simply changing the style will allow me to produce the same effect for all; however, I can't get it to copy to a new document. I've eliminated all sorting and breaking for troubleshooting (1 step at a time).

If I replace text="" with text="example" it selects all example in the document so I know it's searching via text. But leaving text blank (as I never know what it will be) the macro does not work. It seems as if it's not searching via style.

I've also tried recording a macro: searching for the style format, blank text, opening a new document, and pasting. I get the end result manually. It runs; however, it does not copy text to the new document.

I can duplicate the issue in Word 2011 (mac).

Sub Acronym_Extract()
' Acronym_Extract Macro
Dim rText As Range

Dim oDoc As Document

Dim oNewDoc As Document

Set oDoc = ActiveDocument

Set oNewDoc = Documents.Add

                Selection.Find.Style = ActiveDocument.Styles("Acronym Char1")
         With Selection.Find
                .Text = ""
                .Replacement.Text = ""
                .Forward = True
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Wrap = wdFindContinue
            Do While Selection.Find.Execute(Forward = True)
                Set rText = Selection.Range
                oNewDoc.Range.InsertAfter rText & vbCr
                Selection.Information(wdActiveEndPageNumber) & vbCr
                rText.Collapse direction:=wdCollapseEnd
        End With

End Sub

Above I've pasted the Word 2010 code; My Word 2011 code is identical with a few small exceptions.

I'm sure I've overlooked a key function. Any insight would be appreciated.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros