running Word commands

L

Larry

1. 'What is the best way to run a built-in Word command from VBA?

2. Why do some built-in commands run successfully, and others don't?

If I run:

Application.Run MacroName:="WordRight"

it works

If I run Application.Run MacroName:="StartOfWindow"

it doesn't work. I get an error message or nothing happens.

Larry
 
J

Jezebel

Application.Run is used for running macros, not built-in commands.
Apparently you have a macro called 'WordRight" and you don't have a macro
called "StartOfWindow".

The built-in commands are methods of the objects within the Word model. Your
own example shows how it's done: The Application object has a method called
'Run' -- so the basic technique is to give a command of the form

[Object name].[method]

In many cases, the methods also can or must have arguments. In your example,
'MacroName' is a required argument.
 
K

Klaus Linke

Hi Larry,

The built-in commands are all methods of the WordBasic object.
If you use "WordBasic.StartOfWindow", it should work fine.

Greetings,
Klaus
 
L

Larry

Thank you!

I wonder how anyone would have found that information in Word 97 VBA
Help. :)

Larry
 
L

Larry

Yet, curiously, even though WordBasic.StartOfWindow works, if I try
WordBasic.WordRightExtend, I get an error box saying this command is not
available because it is not a WordBasic macro command, though it can be
assigned to a menu, etc.

So some built-in commands can be run this way, and others can't.

Any explanation?

Larry
 
K

Klaus Linke

Larry said:
Yet, curiously, even though WordBasic.StartOfWindow works,
if I try WordBasic.WordRightExtend, I get an error box saying
this command is not available because it is not a WordBasic macro
command, though it can be assigned to a menu, etc.

So some built-in commands can be run this way, and others can't.

Any explanation?

Probably WordRightExtend isn't implemented as a built-in command at all.
A macro "Sub WordRightExtend()" doesn't intercept Ctrl+Shift+Right either.

This stuff doesn't seem to be documented at all.
For old commands, you can download the VBA95 help file, but for new
commands, you have to play guessing games and just look what works.

Greetings,
Klaus
 
L

Larry

Yes, there doesn't seem to be any rhyme or reason to it. With some
built-ins, if you create a macro based on them, the macro has code in
it; with other built-ins, all you get is a blank macro.

Larry
 
K

Klaus Linke

Larry said:
Yes, there doesn't seem to be any rhyme or reason to it. With some
built-ins, if you create a macro based on them, the macro has code in
it; with other built-ins, all you get is a blank macro.

Larry



.... and you often don't get code though the command works fine and can be
intercepted:

Sub ViewDocumentMap()
'
' ViewDocumentMap Macro
' Toggles state of the Heading Explorer
' You have to type in the code yourself:
WordBasic.ViewDocumentMap
End Sub


It's a jungle out there...

Klaus
 
P

Peter Hewett

Hi Klaus

I take your point about Word generating blank macros, or better yet code that
doesn't work! But you don't need to resort to WordBasic for the DocumentMap,
use:

ActiveWindow.DocumentMap = Not ActiveWindow.DocumentMap

Cheers - Peter
 
K

Klaus Linke

Peter Hewett said:
Hi Klaus

I take your point about Word generating blank macros, or better yet
code that doesn't work! But you don't need to resort to WordBasic
for the DocumentMap, use:

ActiveWindow.DocumentMap = Not ActiveWindow.DocumentMap


Hi Peter,

You're right, but I still prefer WordBasic when I replace built-in commands,
since I can be (pretty) sure the command will behave the same as before.
Often, the built-in commands will act differently depending on the current
selection, or the current view, and with WordBasic I don't need to worry
about complications.

As a simple example,
WordBasic.CenterPara
will center the paragraph and remove leading and trailing spaces,
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
will only do the former.

"Resort to" sounds like you regard the WordBasic commands as something
antiquated, kept for compatibility.
The built-in commands (even new ones in Word2003) are still implemented as
methods of the "WordBasic" object.

Greetings,
Klaus
 
P

Peter Hewett

Hi Klaus

I guess to do regard WordBasic as antiquated but absolutely essential!

Rather than being just dogmatic or pedantic, can you explain what you mean or
how you jnow or how you conclude that "The built-in commands (even new ones
in Word2003) are still implemented as methods of the "WordBasic" object".

I'm genuinely interested in how Word works, idiosyncrasies, inconsistencies
and all!

Cheers - Peter
 
K

Klaus Linke

Hi Peter,
Rather than being just dogmatic or pedantic

Wasn't my intention at all... Sorry if it came across that way!!
can you explain what you mean or how you jnow or how you
conclude that "The built-in commands (even new ones in Word2003)
are still implemented as methods of the "WordBasic" object".

WordBasic.FilePermission
WordBasic.UseBalloons
WordBasic.DeleteAllInkAnnotations
WordBasic.ReadingMode
didn't exist back in Word95.

"WordBasic" here seems just another word for "built-in"; it doesn't have
much to do with the WordBasic macro language.

:)
Klaus
 
P

Peter Hewett

Hi Klaus

I wasn't meaning you (dogmatic or pedantic), I meant myself for taking you to
task over this!!!

Ok, I understand now. Even the WordBasic.ViewDocumentMap
did not exist in Word 6. So Microsoft are maintaing compatability between
new methods and the WordBasic object.

Thanks for that + Cheers - Peter
 

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