Tony, Jonathan
Perhaps I am not explaining the problem correctly.
I create template TestTemplateAddIn and save it in my startup folder. The
template has the code shown below. I have never protected this template in
anyway. I have my startup folder listed as a trusted location and I have
trust access to VBA project selected.
I am trying to access the code module to build a list of procedures in the
module. When I start Word it throws and error.
If I change AutoExec to AutoOpen and then attempt to load the template
manually as an AddIn I get the same error.
If I change AutoExec to AutoNew and open a new document based on the
template then the code runs without error and I can get my list.
Question. Is there some way to get around the "Project is unviewabl/Project
is protected" alerts that occur when this template is loaded as an AddIn?
Option Explicit
Dim arrProcedures() As String
Dim i As Long
Dim oTemplate As Template
Sub AutoExec()
GrabTemplate
arrProcedures = ListProceduresInModule
End Sub
Sub GrabTemplate()
Dim aTemplate As Template
For Each aTemplate In Templates
If aTemplate.Name = "TestAddInTemplate.dotm" Then
Set oTemplate = aTemplate
Exit For
End If
Next
End Sub
Sub Macro1()
MsgBox "I'm macro 1"
End Sub
Sub StopListing()
End Sub
Function ListProceduresInModule() As String()
Dim oDOc As Document
Dim stdModule As VBComponent
Dim Count As Long
Dim NumLines As Long
Dim strThisProcedure As String
Count = 0
On Error GoTo Err_Handler
Set stdModule = oTemplate.VBProject.VBComponents("Module1")
NumLines = stdModule.CodeModule.CountOfLines
For i = stdModule.CodeModule.CountOfDeclarationLines + 1 To NumLines
If strThisProcedure <> stdModule.CodeModule.ProcOfLine(i, vbext_pk_Proc)
Then
If strThisProcedure <> "StopListing" Then
ReDim Preserve arrProcedures(Count)
arrProcedures(Count) = Replace(stdModule.CodeModule.ProcOfLine(i,
vbext_pk_Proc), "_", " ")
Count = Count + 1
strThisProcedure = stdModule.CodeModule.ProcOfLine(i, vbext_pk_Proc)
End If
End If
Next i
ListProceduresInModule = arrProcedures()
Exit Function
Err_Handler:
MsgBox Err.Number & " " & Err.Description
End Function
Sub SpoutOffProcedures()
For i = 0 To UBound(arrProcedures) - 1
Debug.Print arrProcedures(i) & " "
Next i
End Sub
Tony said:
The projects (VBComponents) of global templates other than Normal are
not available either via the the UI (where you get the project is
unviewable message) or via code. It's not a matter of being
protected; it isn't, AFAIK, loaded. All that is loaded is executable
code; to gain access to anything else in the template requires that
you open it as a document.
--
Greg Maxey - Word MVP
My web site
http://gregmaxey.mvps.org
Nov 4, 2008. Remember it well as you
will llikely spend your sunset years telling youu children and
Your children's children what it was once like in the United States
where men were free.