Sub or Function not defined (Why ??)

G

Greg Maxey

I have a template that contains a userform module named "MACROS" and a =
standard code module named "Code." I have exported both of these =
modules to my C:\ directory.

I have another large collection of tempaltes that each contain code in =
the "ThisDocument" clase module and a standard code module named =
"NewMacros"

I have written a procedure to process some changes to each of these =
templates. The procedure opens each document and passes the document =
object to the procedure shown below which peforms the following =
functions:

1. Removes all code lines from the ThisDocument class module
2. Deletes the NewMacros code module
3. Imports the Userform "MACROS" and standard code module "Code."

Sub ImportFormAndCodeModules(ByRef Doc As Word.Document)
Dim vbProj As VBProject
Dim vbComp As VBComponent
Set vbProj =3D ActiveDocument.VBProject
Set vbComp =3D vbProj.VBComponents("ThisDocument")
'Clear any lines in "ThisDocument"
Do While vbComp.CodeModule.CountOfLines > 0
vbComp.CodeModule.DeleteLines 1
Loop
'Delete "NewMacros" code module
Set vbComp =3D vbProj.VBComponents("NewMacros")
Application.OrganizerDelete Source:=3DDoc.FullName, Name:=3DvbComp.Name, =
Object:=3DwdOrganizerObjectProjectItems
'Import new userform and code modules.
'For testing
vbProj.VBComponents.Import "C:\MACROS.frm"
vbProj.VBComponents.Import "C:\Code.bas"
End Sub

The processing appears to work as expected. There is one problem =
though. The "Code" module contains an AutoOpen procedure shown below:

Sub AutoOpen()
Options.ButtonFieldClicks =3D 1
MACROS.Show
With MACROS
..Top =3D Application.Top
..Left =3D Application.Left
End With
End Sub

After the process has been run, when I attempt to open some of the =
documents (some but not all) then I get an error message "Sub or =
Function Not Defined." When I "OK" out of the error message the =
document opens but hte AutoOpen procedure clearly did not run.

Now if open the VBE and make the slightest change to the AutoOpen =
procedure (e.g., delete one character and add it right back) then save =
the template it will open normally from then on.

Can anyone explain this behaviour and what, if anything, I can do to =
ensure the Code module is imported cleanly and fully so that this error =
will not occur?

Thanks.


--
Greg Maxey

Please visit my web site http://gregmaxey.mvps.org

Arrogance is a weed that grows mostly on a dunghill (Arabic proverb)
 

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