PDA

View Full Version : Using Word VAB (as host) to import VBA modules and useforms into MS Outlook (XP/2003)



benb
08-26-2015, 07:27 AM
Hi,

I have an unusual challenge...

I wish to use MS Word as the host application to import VBComponents (namley a module and a userform) into MS Outlook using hopefully the VBComponents call and and save VBAProject.otm file.

The problem is I know Outlook isn't functionally rich enough with its own Application object (reference library) and avoiding a serious of SendKeys commands which is at best flaky, is there a way to populate code and the userform objects to Outlook?

The added complication is, I cannot gain access to or read/write to the C drive as it's locked down and therefore cannot use the copy file action into users roaming profile's folders hence having to dynamically create a setup routine (as a one-off event for each new roaming pc).
The users will be hotdesking hence the setup to be able to be called from within Word via their accessible Normal.dot template file.

Hope that make sense - any help please?

Ben

TonyJollans
08-31-2015, 07:41 AM
Word's Normal Template (shared with Outlook in Office 2003) is in your roaming profile on the network. Outlook's VbaProject.OTM is also in the roaming profile. You won't be able to save anything in either of them unless you have write access. So, if you can put something in the Normal Template, why not in Outlook's VBA file?

Outlook has a very complex object model (and so does Word) but the VBA functionality you need for progammatic access to the VBA Project is the same in both.

All told, I'm confused about what you're really wanting to do. Can you give a little more detail of why you need to do this in the way you suggest, and why from Word?

benb
09-02-2015, 07:17 AM
The challenge is users cannot write directly to the C: drive and therefore dave the OTM file (even programmatically). However, they can save added VBA code and userforms in their own Outlook applications (VBE window view) which will generate this file once saved. The fact their Normal.Dot is stored on a personal network drive which is accessible and that Outlook cannot be customised this way, we need to use Word as the host to run the setup of creating VBA code in Outlook.

Here's some more detail of the task...

I require a VBA procedure written in a MS Word Module (stored to the Normal.dot template file) to do the following:


1. Create an MS Outlook object and set an instance so the following (if possible can happen).


2. Load the VBE Window (Visual basic editor ALT + F11) in Outlook (whether it has the focus of not - active).


3. Import one standard module (.bas file) with VBA code. This can be a simple generic example like a message box test "Hello World".


Sub MessageTest()
MsgBox "Hello World!"
End Sub


4. Import one basic example Userform (.frm file) with one CommandButton to call the MessageTest public procedure above.


5. Set a reference (normally and manually called via the Tools, References command) in Outlook and set an example reference like Microsoft Word Object Library (or any other MS Object for now).


6. Close Outlook, save the changes to the newly added VBAProject.OTM file it will have created and dispose of object reference back in Word.

7. Create a toolbar with one example icon and attached macro (using the CommandBar call).


The above code is required to distribute to users who will need to run this type of procedure (as a one-off) for each new desktop login pc (as a roaming profile) to their Outlook accounts as they will not be able to copy and save the VBAProject.OTM file in the natural C:\ drive location to their user folder and even in VBA code cannot be accessed (highly secured on the network).


The example code is not relevant just the ability to import a module with code and a userform with code and set references etc. hence the simple code example above. The full code has already been written by me and I'm at a loss to how a procedure to create the above can be executed via another host (in this case MS Word - as users can customise this file, Normal.dot).


Looking at the Outlook VBA object library, unlike Word or Excel, there are no methods or enough members like 'Import' in the Outlook 'Application' object which is why I need a more creative solution as well as we cannot simply copy files to the hard-drive in the normal way.Therefore, a dynamic way to create and save the VBAProject.OTM is required.

I do have a good handle and knowledge of VBA (Excel, Word and Access) but Outlook is my weaker point and have studied the Application object which is not as rich as the others - hence my challenge of a work-around to achieve this.

Thanks for anyone's help here :)