Outside import of forms/modules to Normal.DOT

K

Khai

Hi -

I have 2 user forms, and 2 modules I need to import to Normal.DOT, however,
I'm unsure how to do this via Automation from an outside VB6 program, or -
using a document, and writing it through VBA.

I know it can be done, a database program I use does it.. just trying to
figure it out now. =D

THanks,
-KHai
 
A

Astrid

Hi Kai,

First as an answer to your question, yes that's possible. In your VBA project use some code like this (code assumes that modules and userforms are in the template the code is called from):
In order to get this code to run, you need a reference in your VBA project (Tools - References in the VBE) to Visual Basic for Applications Extensibility library.
-----------------------------------------------------------------------
Sub ExportCode()
Dim sTempdir As String
Const csMod1Name As String = "Module1"
Const csForm1Name As String = "Userform1"
Const csModExt As String = ".bas"
Const csFrmExt As String = ".frm"
Dim oVbProject As VBProject
Dim oVbComponent As VBComponent

On Error Resume Next
'Get temp path
sTempdir = Environ("Temp")
If Right(sTempdir, 1) <> Application.PathSeparator Then
sTempdir = sTempdir & Application.PathSeparator
End If

Set oVbProject = ThisDocument.VBProject
For Each oVbComponent In oVbProject.VBComponents
If StrComp(oVbComponent.Name, csMod1Name, vbTextCompare) = 0 Then
oVbComponent.Export FileName:=sTempdir & oVbComponent.Name & csModExt
ElseIf StrComp(oVbComponent.Name, csForm1Name, vbTextCompare) = 0 Then
oVbComponent.Export FileName:=sTempdir & oVbComponent.Name & csFrmExt
End If
Next

NormalTemplate.VBProject.VBComponents.Import _
FileName:=sTempdir & csMod1Name & csModExt
NormalTemplate.VBProject.VBComponents.Import _
FileName:=sTempdir & csForm1Name & csFrmExt

Set oVbComponent = Nothing
Set oVbProject = Nothing

End Sub
-----------------------------------------------------------------------

Now that you know that it's possible, one word of advice. IMO it's not a good idea to mess around with the user's normal.dot. Apart from that this template is meant for personal modifications, it also can corrupt, in which case, your code is lost.
You might want to consider using an addin (if your code needs to be available at all times) or in a separate template (if the code should be available only for specific documents).
For more info see:

What do Templates and Add-ins store?
http://www.mvps.org/word/FAQs/Customization/WhatTemplatesStore.htm

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/
 

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