Macro steps fine, won't run (5941 runtime error)

P

Paul B

Hi. I'm trying to figure out why this macro will step through ok,
but chokes when run (via F5 in the VBA app, or via hotkey in
Word.

Word says that the x variable doesn't exist. If you examine the
code, take note that it's the second instance of this variable
that's causing the problem. I had to recount the smarttags
because of the new Selection. Word counts the x correctly on
step-through, but not on Run. Maybe it just goes too fast on Run?

Here's the code. It probably looks a bit kludgy, but it isolates
a smarttag, executes it, then formats the pasted text, so there
are a lot of whizbangs.

Any insight would be appreciated.
p.


Sub LogosSmartTag()
' execute smarttag immediately preceding

' select line, cursor at line's end
Selection.HomeKey unit:=wdLine, Extend:=wdMove
Selection.EndKey unit:=wdLine, Extend:=wdExtend

myrange = Selection.Range
' With myrange
x = Selection.Range.SmartTags.Count
' End With
y = ActiveDocument.Paragraphs.Count
ActiveDocument.SmartTags(x).Select
Selection.Collapse wdCollapseStart
Selection.TypeParagraph
Selection.EndKey unit:=wdLine, Extend:=wdExtend


HERE'S WHERE THE RUNTIME ERROR HAPPENS.
5941: OBJECT DOESN'T EXIST
THIS REFERS TO THE "x" VARIABLE

x = Selection.SmartTags.Count
Selection.SmartTags(x).SmartTagActions(4).Execute

yy = ActiveDocument.Paragraphs.Count
y = yy - y

' merge citation to last verse
Selection.MoveDown unit:=wdParagraph, Count:=y - 1,
Extend:=wdMove
Selection.TypeBackspace
Selection.TypeText Text:=" "

' apply verse style
Selection.EndKey unit:=wdLine, Extend:=wdMove
With myrange
Selection.MoveUp unit:=wdParagraph, Count:=y - 1,
Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("verse")
Selection.MoveDown unit:=wdParagraph, Count:=1,
Extend:=wdMove
End With

' resume text body style
Selection.TypeParagraph
Selection.Style = ActiveDocument.Styles("text body")
End Sub
 
T

Tony Jollans

Maybe it just goes too fast on Run?

Maybe. Try adding a DoEvents statement immediately before the failing
statement.
 
P

Paul B

Maybe. Try adding a DoEvents statement immediately before the failing
statement.

I thought out my needs more carefully, and eliminated some of the
text handling verbiage. For reasons unclear to me, this
eliminated the problem.

In my struggle I also made the amazing discovery that vba does
not include a Timer function? Seems it used to, since it's still
in the Help file.

Here's the final code.
p.


Sub LogosSmartTag()
' execute last smarttag in line
' 1/08, by Paul B. with help from friends

' select line smarttag is on
Selection.HomeKey unit:=wdLine, Extend:=wdMove
Selection.EndKey unit:=wdLine, Extend:=wdExtend
myrange = Selection.Range

' select last smarttag on line
x = Selection.Range.SmartTags.Count
ActiveDocument.SmartTags(x).Select

' x = Selection.SmartTags.Count
y = ActiveDocument.Paragraphs.Count
Selection.SmartTags(x).SmartTagActions(4).Execute
yy = ActiveDocument.Paragraphs.Count
y = yy - y

' merge citation to last verse
Selection.MoveDown unit:=wdParagraph, Count:=y, Extend:=wdMove
Selection.TypeBackspace
Selection.TypeText Text:=" "

' apply verse style
Selection.EndKey unit:=wdLine, Extend:=wdMove
With myrange
Selection.MoveUp unit:=wdParagraph, Count:=y, Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("verse")
Selection.MoveDown unit:=wdParagraph, Count:=1, Extend:=wdMove
End With

' resume text body style
Selection.TypeParagraph
Selection.Style = ActiveDocument.Styles("text body")
End Sub
 
P

Paul B

It's still in VBA, too. Don't know why you can't see it.

I'm a bit confused on this. When I initially included a
timer/do-events function, it worked. Then suddenly vba refused to
capitalize/recognize "Timer" and began erroring out - "no such
function". I went online and found many complaints that the timer
is lacking. Here's the code I used:

Sub timer()
Dim WAIT As Double

WAIT = timer
' WAIT + <number of seconds>
While timer < WAIT + 2
DoEvents 'do nothing
Wend
End Sub

p.
 
P

Paul B

You will have difficulty if you call your procedure "Timer".

You nailed it! As soon as I changed that I was in business. I
also made a timer using the Time function, but formatting the
pause time was pretty unwieldy.

Thanks much for your help!
p.
 
P

Paul B

You nailed it! As soon as I changed that I was in business. I
also made a timer using the Time function, but formatting the
pause time was pretty unwieldy.

Thanks much for your help!
p.

Actually, I had that routine as part of the larger macro, without
the "sub timer ()" problem, and it still didn't work. So I'm
still confused on that issue. But the routine with a new name
runs ok now, and the macro's own problem is fixed, so I'm in
business, if not totally understanding.

Thanks again.
p.
 

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