macro runtime error

D

dave_wilson

I have successfully been using a macro I created in Word (current version
2003) to open several
documents, paste them together, save the combined document, and then close
all the documents. I created a new macro today using key strokes, but when I
run it I get a runtime error message 4605 that says "this method or property
is not available because no tesxt is selected." As far as I know I have been
able to create macros in earleir versions of Word without any difficulty
using key strokes. I did notice that 2003 adds several phrases in macros that
were not there in earlier versions of Word. Any suggestions would be greatly
appreciated. My email is (e-mail address removed). Here is the macro:

ChangeFileOpenDirectory "C:\Wilsnrpt\customer\rettew\"
Documents.Open FileName:="3MD.DOC", ConfirmConversions:=False,
ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto, XMLTransform:=""
Documents.Open FileName:="3EP.DOC", ConfirmConversions:=False,
ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto, XMLTransform:=""
Documents.Open FileName:="3LGLS.DOC", ConfirmConversions:=False,
ReadOnly _
:=False, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate _
:="", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="" _
, Format:=wdOpenFormatAuto, XMLTransform:=""
Windows(1).Activate
Selection.EndKey Unit:=wdStory
Windows(3).Activate
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Copy
Windows(1).Activate
Windows(2).Activate
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.PasteAndFormat (wdPasteDefault)
Selection.Copy **********(this line is highlighted in yellow)
Windows(1).Activate
ActiveDocument.Close
ActiveDocument.Close
ActiveDocument.Close
End Sub
 
J

Jean-Guy Marcil

dave_wilson was telling us:
dave_wilson nous racontait que :
I have successfully been using a macro I created in Word (current
version 2003) to open several
documents, paste them together, save the combined document, and then
close all the documents. I created a new macro today using key
strokes, but when I run it I get a runtime error message 4605 that
says "this method or property is not available because no tesxt is
selected." As far as I know I have been able to create macros in
earleir versions of Word without any difficulty using key strokes. I
did notice that 2003 adds several phrases in macros that were not
there in earlier versions of Word. Any suggestions would be greatly
appreciated. My email is (e-mail address removed). Here is the macro:

Where to start... ;-)
First, using the recorder, especially when handling multiple document tasks,
is a sure way to get code that will eventually break down.
See at the end of my message for a link on this.

Second, using indexed windows is also dangerous (what if you forgot you
already had a document opened? And Word has a funny way of indexing these
windows that can be unpredictable at times).
Finally, the Selection object is very unstable, especially in this case.

See your code with my comments (I replace the Windows(x) with declared
document objects, more stable, but I am not sure what the Selection stuff is
supposed to do):

'_______________________________________
Dim DocMD As Document
Dim DocEP As Document
Dim DocLG As Document

ChangeFileOpenDirectory "C:\Wilsnrpt\customer\rettew\"

Set DocMD = Documents.Open(FileName:="3MD.DOC", ConfirmConversions:=False,
ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:=
_
"", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="",
_
Format:=wdOpenFormatAuto, XMLTransform:="")
Set DocEP = Documents.Open(FileName:="3EP.DOC", ConfirmConversions:=False,
ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:=
_
"", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="",
_
Format:=wdOpenFormatAuto, XMLTransform:="")
Set DocLG = Documents.Open(FileName:="3LGLS.DOC", ConfirmConversions:=False,
ReadOnly _
:=False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate
_
:="", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="" _
, Format:=wdOpenFormatAuto, XMLTransform:="")

DocMD.Activate

'What does this line achieve?
'Why place the cursor at the end of the document
'and then do nothing with it?
'So, why activate the first document?
Selection.EndKey Unit:=wdStory

DocLG.Activate

'What exactly are you selecting and copying?
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Copy

'Why activate Doc1, then immmediatly Doc2?
DocMD.Activate
DocEP.Activate

'What exactly are you selecting and copying?
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.PasteAndFormat (wdPasteDefault)
Selection.Copy '**********(this line is highlighted in yellow)

'Why activating a document just before closing it?
DocMD.Activate
DocMD.Close
DocEP.Close
DocLG.Close
'_______________________________________

Here is what I believe you want to do:
'_______________________________________
Dim DocMD As Document
Dim DocEP As Document
Dim DocLG As Document
Dim DocMDRange As Range

Const myPath As String = "C:\Wilsnrpt\customer\rettew\"

Set DocMD = Documents.Open(myPath & "3MD.DOC")
Set DocEP = Documents.Open(myPath & "3EP.DOC")
Set DocLG = Documents.Open(myPath & "3LGLS.DOC")

Set DocMDRange = DocMD.Range
With DocMDRange
.Collapse wdCollapseEnd
.FormattedText = DocEP.Range.FormattedText
.Collapse wdCollapseEnd
.FormattedText = DocLG.Range.FormattedText
End With

DocMD.Close wdSaveChanges
DocEP.Close wdDoNotSaveChanges
DocLG.Close wdDoNotSaveChanges

Set DocMDRange = Nothing
Set DocMD = Nothing
Set DocEP = Nothing
Set DocLG = Nothing
'_______________________________________


See:
http://word.mvps.org/faqs/macrosvba/UsingRecorder.htm
http://word.mvps.org/faqs/macrosvba/ModifyRecordedMacro.htm

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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

Similar Threads


Top