Going between documents using macros

F

Fogharty

I'm using Word 2004. I have two documents open. Is there a way, using
macros, to go from Document 1 to Document 2, where I'll go to a
certain line and copy it, then go back to Document 1 and save it using
the text I have just copied.

I have the macro down for going to the line, copying, and saving. What
I need is the part where I go from doc to doc without regard to the
document name. So I'll be in "Active Document" first, then I need to
go to "?" (other Document) then back to "Previous Document."

So I just need the navigation part.

Here is the code for doing all this within the SAME document (finding
a 10-character line):



Sub copy_course()

' copy_course Macro
' Macro recorded 1/22/10 by Fogharty
'
Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=1,
Name:=""
Selection.Find.ClearFormatting
With Selection.Find
.Text = "??????????"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.ClearFormatting
With Selection.Find
.Text = "??????????"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Copy

Dim myName As String
Dim myPath As String
Dim docName As String
docName = InputBox("Please enter the file name for the
document.")
myPath = ActiveDocument.Path
If myPath = "" Then
myPath = Options.DefaultFilePath(wdDocumentsPath)
End If

myName = myPath & Application.PathSeparator & docName
ActiveDocument.SaveAs myName

End Sub
 
J

John McGhie

There is no way to make this "bullet-proof" but you can get pretty close...

1) Set the Document name from the active document as either source or
destination (decide which one you're going to start the macro from.)

2) Read the Count method of the Documents collection: if there are more than
2 then you need to prompt the user for the destination document.

To prompt, you need to do more work to make a list of all the documents that
are not the source document and ask the user which one.

3) If there are only two documents open, the one that is not "This
Document" is the destination.

Once you have the document name, switching is very easy: you don't actually
need to switch at all: you can use a "With DestinationDocument" construct to
inject content directly.

Calling the "Next Window" command using SendKeys would be possible, but it's
always going to be a lottery, because you don't know where the focus is.

Hope this helps


I'm using Word 2004. I have two documents open. Is there a way, using
macros, to go from Document 1 to Document 2, where I'll go to a
certain line and copy it, then go back to Document 1 and save it using
the text I have just copied.

I have the macro down for going to the line, copying, and saving. What
I need is the part where I go from doc to doc without regard to the
document name. So I'll be in "Active Document" first, then I need to
go to "?" (other Document) then back to "Previous Document."

So I just need the navigation part.

Here is the code for doing all this within the SAME document (finding
a 10-character line):



Sub copy_course()

' copy_course Macro
' Macro recorded 1/22/10 by Fogharty
'
Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=1,
Name:=""
Selection.Find.ClearFormatting
With Selection.Find
.Text = "??????????"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.ClearFormatting
With Selection.Find
.Text = "??????????"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Copy

Dim myName As String
Dim myPath As String
Dim docName As String
docName = InputBox("Please enter the file name for the
document.")
myPath = ActiveDocument.Path
If myPath = "" Then
myPath = Options.DefaultFilePath(wdDocumentsPath)
End If

myName = myPath & Application.PathSeparator & docName
ActiveDocument.SaveAs myName

End Sub

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