Ah, well, I'm sorry I inspired you to do that, since in the meantime RS has
greatly improved their Office automation capabilities.
Alas, I have no idea what I was thinking at the time, and you're right,
there is no Components collection for the ActiveDocument -- it's called
VBComponents. Apparently you can access VBComponents of a document and also
of the application (through it's VBE, eg, Application.VBE.VBComponents.Add
etc).
Here's a little bit of info I turned up:
"The top object for accessing VBA objects is the VBE object, which can be
accessed through the Application object. The VBE object, in turn, contains a
collection of VBProject objects. The VBProject object, in turn, contains the
VBComponents collection. UserForm objects and code modules are elements of
the VBComponents collection.
In Microsoft Word for Windows 2000, each document, as well as every
template, can contain its own VBProject object. In the Word documents, the
VBProject object is located underneath the Document object.
The following sample code creates a new UserForm and assigns the variable
MyNewForm to it. You can now use this new VBComponent object to manipulate
the User Form. Sub BuildMyForm()
Set MyNewForm = _
VBE.ActiveVBProject.VBComponents.Add(ComponentType:=vbext_ct_MSForm)
End Sub
"
It seems that in Word 2003 (which is what I have installed), or earlier,
they might have replaced VBProject at the application level with
ActiveVBProject.
Here is an RB method that will retrieve the count of components in the
active VB project:
Sub ShowVBComponentsCount()
dim word as wordapplication
dim vbComponents as OLEObject
dim vbComponentCount as Variant
word = new wordapplication
vbComponents =
OLEObject(OLEObject(word.Property("VBE")).Property("ActiveVBProject")).Prope
rty("VBComponents")
vbComponentCount = vbComponents.Property("Count")
MsgBox vbComponentCount
End Sub
I did not try to do a vbComponents.Add because I just don't care enough
right now to try it, but let me know how it turns out if you do try it.
***** However, before exploring this option in detail, you should take into
account the security issues I warned about. For instance, a quick Google
search turned up this ominous note:
"I have hit a roadblock, though. When I try to access project components
using Application.ActiveDocument.VBProject.VBComponents I get the error
"Programmatic access to Visual Basic Project is not trusted". Clearing the
"Trust access to Visual Basic Project" checkbox under Tools -> Options ->
Security -> Macro Security -> Trusted Sources gets rid of this error,
however having to go around changing this setting in everyone's profile
would defeat the purpose of the project somewhat."
So unless you have complete administrative control over the machines where
you will be deploying your application, this might be a show-stopper.
Overall, I think it might be best to consider re-writing your code to use
RB's new and improved Word automation.
Regards,
Walter
-----Original Message-----
From: realbasic-nug-bounces at lists dot realsoftware dot com
[mailto:realbasic-nug-bounces at lists dot realsoftware dot com] On Behalf Of
Maximilian Tyrtania
Sent: Tuesday, August 31, 2004 4:51 AM
To: REALbasic Network Users Group
Subject: Re: Telling Word to execute some VBA-code
Hi Walter,
> One solution to explore is to try installing the code as a module and
> then running it. You should be able to do something like call
> ActiveDocument.VBProject.Components.Add (I'm sure that VBProject
> exists, but you'd have to check to find out the actual syntax for
> adding a module) to load the VBA source.
>
> One drawback that I can think of right off is that this is probably
> the kind of thing that will be disallowed in XP service pack 2, if it
> hasn't already been disallowed (I think they might have turned this
> feature off in Office 2003).
It's good to see you on this thread. After all it was you who inspired me to
use the appleevent MyWord_Appleevent = NewAppleEvent("MSWD","dovb","MSWD").
Some time ago you posted this as a very interesting alternative to using the
Office plugins. And your post ended with the words "I assume this approach
would work on Windows as well" (see
http://www.realsoftware.com/listarchives/realbasic-nug/2003-06/msg02068.html
)
I wonder what exactly you had in mind when you wrote this. There even was a
little discussion about this here in the NUG.
Unfortunately the ActiveDocument.VBProject object doesn't have a
"Components"-property.
Best wishes,
-> Maximilian Tyrtania | Mty at fischerAppelt dot de
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://www.realsoftware.com/listarchives/lists.html>
|