Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2007

Tip: Looking for answers? Try searching our database.

Programmatically Writing/Copying Macros to a Document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George Lee - 30 May 2007 15:17 GMT
How do you write macro code to existing document?

For example, from within my host application the user will be able to open a
document. I would like to add a macro to that document only. I will not have
control over the document ahead of time, nor the template they are using.

So how would you programmatically copy or write macros to a document?
Jonathan West - 30 May 2007 16:49 GMT
> How do you write macro code to existing document?
>
[quoted text clipped - 5 lines]
>
> So how would you programmatically copy or write macros to a document?

You can use the OrganizerCopy method to copy a module into the document.

But if the user has High security set in Word, the macro won't run anyway.
And you can't change the security level through code.

What exactly are you trying to achieve?

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org

George Lee - 30 May 2007 18:37 GMT
I want to dynamically add a macro to an active document. The Import method of
VBE interface works for the most part but I can't see where it adds macros to
the currently attached template. I'd like to add an AutoExit dynamically when
a certain file is opened and remove the macro when when the file is closed.

> > How do you write macro code to existing document?
> >
[quoted text clipped - 12 lines]
>
> What exactly are you trying to achieve?
Jonathan West - 30 May 2007 18:45 GMT
>I want to dynamically add a macro to an active document. The Import method
>of
[quoted text clipped - 4 lines]
> a certain file is opened and remove the macro when when the file is
> closed.

An AutoExit macro will only fire then the document is unloaded as an add-in.
It won't run at all when the document is opened or closed.

What is this AutoExit macro supposed to be doing?

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org

George Lee - 31 May 2007 19:03 GMT
I want to intercept the some files being closed. I can intercept the "Close"
menu command, the "Exit" command, and the document close button. I can't make
that interception if the user closes the document by clicking the
application's window X button, which is actually the most likely way they'll
close the document. I'd rather not have a global interception in everyone's
template. That gets me to adding a routine to individual document's template.
Is that incorrect thinking?

> >I want to dynamically add a macro to an active document. The Import method
> >of
[quoted text clipped - 9 lines]
>
> What is this AutoExit macro supposed to be doing?
Jonathan West - 08 Jun 2007 10:19 GMT
>I want to intercept the some files being closed. I can intercept the
>"Close"
[quoted text clipped - 8 lines]
> template.
> Is that incorrect thinking?

Yes. if you want to be able to intercept the closing of a file, and in
certain circumstances ensure that the file is not closed despite the user's
command, then your only option is the DocumentBeforeClose event of the
Application object. This article will help you to learn to write application
event procedures

Writing application event procedures
http://www.word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm

Since this is an event of the Application object, it will fire whenever any
document is closed. So if you want to operate only on documents attached to
a particular template, you would have to check the AttachedTemplate property
of the document, and branch accordingly.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.