Using macro with two documents Word 2004

F

Fogharty

I am working with a large group of documents that have questions and
answers in them. The answers are using red text, everything else is in
black. I would like to create a macro that will search the active
document, find the first paragraph of red text, copy it, go to a
second document titled "Answer keys" and paste the text in there.

So far so good.

But then I would like to go back to the first document, and find the
NEXT paragraph of red text, copy, go to "Answer keys.doc", paste, go
back to original document, etc. until there is no more new red text to
be found. The macro I recorded specifies the name of the first
document "0101 Lesson 1" but I would like the macro to be more generic
than that... to go to the other document that is NOT "Answer keys" to
find the text, copy, and so on.

How is this to be done? So that the macro will go back to the first
document, find the next paragraph of red text, copy, and so forth.

When I recorded a macro, it kept copying the same text over and over,
and of course, the document name will change on the next file.

This is what I recorded:


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
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.Find.Execute
Selection.Copy
Windows("Answer keys.doc").Activate
Application.Run MacroName:="MathTypeCommands.UIWrappers.EditPaste"
Windows("0101 Lesson 1.doc").Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Copy
Windows("Answer keys.doc").Activate
With ActiveWindow
.Left = 764
.Top = 36
End With
Application.Run MacroName:="MathTypeCommands.UIWrappers.EditPaste"
Windows("0101 Lesson 1.doc").Activate
With ActiveWindow
.Left = 8
.Top = 7
End With
End Sub


Eventually this macro will be shared with Word 2007 users, and I'll
have to come up with something to make it work in Word 2008 (perhaps
an Applescript of some kind.)
 
J

John McGhie

What you are talking about cannot be recorded.

It's actually a fairly substantial programming job.

Here's some code that will run in Word 2007, that contains some useful bits
you will need... This calls the File System Object. Parts of the File
System Object are supported in Word 2004 VBA.

Look up FileSystemObject in the VBA Object Browser, and open the Help on it
from within the Object Browser (which is the only place you will find it
documented).

If I were you, I would find another way to do this :) You can do it in
Word on the PC. On the Mac, I think you might need to hire a developer!

The code sample...

Option Explicit

Sub MakeSnapshot()
' Lists files in directory and inserts each one

Dim Snapshot As Document
Dim myDialog As Dialog
Dim FolderSpec As String
Dim FieldString As String
Dim doubleSlash
Dim i As Long
Dim fso, f, f1, fc, s, Temp
Dim FileSpec
Dim x As Integer
Dim aField As Field
Dim vbQuote As String
vbQuote = Chr(34)

WordBasic.DisableAutoMacros

Set Snapshot = ActiveDocument

x = MsgBox("Choose any of the files in the folder that contains the source
documents.", vbInformation + vbOKCancel, "Snapshot Macro")

If x = 1 Then
Set myDialog = Dialogs(wdDialogFileFind)
myDialog.Display
myDialog.Update
FolderSpec = myDialog.SearchPath
Set fso = CreateObject("Scripting.FileSystemObject")
Set Temp = fso.GetSpecialFolder(2)
Snapshot.SaveAs FileName:=Temp & "\" & "Snapshot Temp",
FileFormat:=wdFormatDocument
Else: End
End If

doubleSlash = Split(FolderSpec, "\")
doubleSlash = Join(doubleSlash, "#%") & "#%"
With Snapshot
.CustomDocumentProperties("FolderPath") = FolderSpec
.Fields.Update
End With

StatusBar = "Please wait..."

Selection.EndKey Unit:=wdStory, Extend:=wdMove

If ActiveWindow.View.SplitSpecial = wdPaneNone Then
ActiveWindow.ActivePane.View.Type = wdNormalView
Else
ActiveWindow.View.Type = wdNormalView
End If

Set f = fso.GetFolder(FolderSpec)
Set fc = f.Files
i = f.Files.Count

For Each f1 In fc
If f1.Type = "Microsoft Word Document" And _
Left(f1.Name, 4) <> "Snap" And _
Left(f1.Name, 2) <> "~$" Then

' Put the code that processes each document here


End If
Next

Set fso = Nothing
Set f = Nothing
Set fc = Nothing
Set f1 = Nothing

StatusBar = "Files Done"
Snapshot.Save

MsgBox "All Done"

End Sub


I am working with a large group of documents that have questions and
answers in them. The answers are using red text, everything else is in
black. I would like to create a macro that will search the active
document, find the first paragraph of red text, copy it, go to a
second document titled "Answer keys" and paste the text in there.

So far so good.

But then I would like to go back to the first document, and find the
NEXT paragraph of red text, copy, go to "Answer keys.doc", paste, go
back to original document, etc. until there is no more new red text to
be found. The macro I recorded specifies the name of the first
document "0101 Lesson 1" but I would like the macro to be more generic
than that... to go to the other document that is NOT "Answer keys" to
find the text, copy, and so on.

How is this to be done? So that the macro will go back to the first
document, find the next paragraph of red text, copy, and so forth.

When I recorded a macro, it kept copying the same text over and over,
and of course, the document name will change on the next file.

This is what I recorded:


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
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.Find.Execute
Selection.Copy
Windows("Answer keys.doc").Activate
Application.Run MacroName:="MathTypeCommands.UIWrappers.EditPaste"
Windows("0101 Lesson 1.doc").Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Copy
Windows("Answer keys.doc").Activate
With ActiveWindow
.Left = 764
.Top = 36
End With
Application.Run MacroName:="MathTypeCommands.UIWrappers.EditPaste"
Windows("0101 Lesson 1.doc").Activate
With ActiveWindow
.Left = 8
.Top = 7
End With
End Sub


Eventually this macro will be shared with Word 2007 users, and I'll
have to come up with something to make it work in Word 2008 (perhaps
an Applescript of some kind.)


--

This email is my business email -- Please do not email me about forum
matters unless you intend to pay!

John McGhie, Microsoft MVP (Word, Mac Word), Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. | Ph: +61 (0)4 1209 1410
+61 4 1209 1410, mailto:[email protected]
 
F

Fogharty

I think... I think the easiest way to do this is a simple Find and
Replace to get rid of all the black text, leaving the red text behind,
then manually copy and past that into a separate document, then close
the first one with saving changes.

While I like to do everything with the push of a button or two,
sometimes a shortcut ain't a shortcut.

Thanks John
 
F

Fogharty

I think... I think the easiest way to do this is a simple Find and
Replace to get rid of all the black text, leaving the red text behind,
then manually copy and paste that into a separate document, then close
the first one without saving changes.

While I like to do everything with the push of a button or two,
sometimes a shortcut ain't a shortcut.

Thanks John
 
J

John McGhie

My opinion precisely :)

You "can" do this, and if you have to do 500 a day, you write code like I
did...

Otherwise: Find/Replace sounds like goodness to me :)

Cheers


I think... I think the easiest way to do this is a simple Find and
Replace to get rid of all the black text, leaving the red text behind,
then manually copy and past that into a separate document, then close
the first one with saving changes.

While I like to do everything with the push of a button or two,
sometimes a shortcut ain't a shortcut.

Thanks John


--

This email is my business email -- Please do not email me about forum
matters unless you intend to pay!

John McGhie, Microsoft MVP (Word, Mac Word), Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. | Ph: +61 (0)4 1209 1410
+61 4 1209 1410, mailto:[email protected]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top