macro runtime error

D

dave_wilson

I have successfully been using a macro I created in Word to open several
documants, 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 text is selected." 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. Here is the macro: ( The line with the error is noted by
"******"

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

Jezebel

The trouble with creating macros by recording keystrokes is that the macros
may rely on the specifics of what's going on at the time you record it; so
if you run the macro under different circumstances, it might not work. In
this case, your code relies on whether any documents are already open when
you start, and if so, specifically what they are called. The issue is those
Windows().Activate statements -- these are switching between the currently
opened documents (equivalent to making selections form the Windows menu).
Your macro opens three documents -- presumably the only documents open when
you recorded the macro, so they were Windows 3, 1 and 2 respectively (the
Windows list is in alphabetical order). But if you run the macro with other
documents already open, those Windows statements may be activating the wrong
documents, so your selection statements might not work.

You'll be able to see what's going on if you display Word and VBA
side-by-side; then put the cursor anywhere in the macro and press F8,
repeatedly, to execute the macro one instruction at a time.

A better approach is to use document variables --

Dim pDoc1 as Word.Document
Set pDoc1 = Documents.Open(FileName:="C:\Wilsnrpt\customer\rettew\3MD.DOC")
:
pDoc1.Range(a, b).Copy
pDoc2.Range(x, y).Paste

etc

The macro recorder is at its best for repetitive keystroke sequences and as
a starting point for writing a real macro
 

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