True or False

G

Greg Maxey

Is the following statement technically true and accurate?

Code in template/global template AddIns is inaccessible to the VB object
module when the AddIn is loaded.


Thanks.
--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
J

Jonathan West

Greg Maxey said:
Is the following statement technically true and accurate?

Code in template/global template AddIns is inaccessible to the VB object
module when the AddIn is loaded.

As I understand it, that is untrue. External access to the code is available
through two means

1. Use of the Application.Run method (applies to any Public Sub or Function
in any regular module)

2. Setting a reference to the code container through the Tools Reference
menu. (Enables access to class modules, UserForms and Public Subs and
Functions in regular modules.)
 
G

Greg Maxey

Jonathan,

Yes of course. My question was poorly worded. The problem I have having is
accessing the actual code module when templates are loaded as Addins.

I can use code like this to get the names of all procedures in a module when
the template in question is attached to an open document:

Option Explicit
Dim oTemplate As Template
Dim arrProcedures() As String

Sub Testing()
Dim aTemplate As Template
Dim pArray() As String
For Each aTemplate In Templates
If aTemplate.Name = "Test Template.dotm" Then
Set oTemplate = aTemplate
Exit For
End If
Next
pArray = ListProceduresInModule
MsgBox pArray(1)
End Sub

Function ListProceduresInModule() As String()
Dim stdModule As VBComponent
Dim i As Long
Dim Count As Long
Dim NumLines As Long
Dim strThisProcedure As String
Count = 0
Set stdModule = oTemplate.VBProject.VBComponents("Main")
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) = 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()
End Function

However when that template is loaded as an AddIn the procedure fails on:

Set stdModule = oTemplate.VBProject.VBComponents("Main")

Can't perform operation since the project is protected.

So I guess the is can you access the VBA code module on projects that are
loaded as AddIns?




Jonathan said:
As I understand it, that is untrue. External access to the code is
available through two means

1. Use of the Application.Run method (applies to any Public Sub or
Function in any regular module)

2. Setting a reference to the code container through the Tools
Reference menu. (Enables access to class modules, UserForms and
Public Subs and Functions in regular modules.)

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
J

Jonathan West

Greg Maxey said:
Jonathan,

Yes of course. My question was poorly worded. The problem I have having
is accessing the actual code module when templates are loaded as Addins.

Ah. I understand you now. You are looking to access the VBProject object of
a template loaded as an add-in. If the project is protected, no you can't do
that. If the project isn't protected, you can. The distinction is not how
the template is loaded, but whether it is protected.
 
G

Greg Maxey

Jonathan,

"If the project is protected, no you can't do that. If the project isn't
protected, you can."

How?

I did nothing to "protect" the project. Since it throws that error when
loaded as an AddIn then I can only assume that loading it as an AddIn is
somehow protecting it as well. How do I prevent it from being protected?

Thanks.

Jonathan said:
Ah. I understand you now. You are looking to access the VBProject
object of a template loaded as an add-in. If the project is
protected, no you can't do that. If the project isn't protected, you
can. The distinction is not how the template is loaded, but whether
it is protected.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
T

Tony Jollans

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.
 
J

Jonathan West

Tony Jollans 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.

No, if you have a template open as a document, and the VB project is not
protected, and the Tools Macro Security dialog has the "Trust access to
Visual Basic Project" box checked, then you can use code to access the
VBProject object hierarchy of that template, even from code located in
another template.
 
G

Greg Maxey

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.
 
T

Tony Jollans

I agree completely but that isn't what I thought I said.

If a template is 'open as a document', or a document based on it is open,
then, yes, the VBProject is available.

Otherwise (ie, you have it open only as a global template or addIn) then the
VB Project is not available.
 
L

Lene Fredborg

Greg,

Maybe the following will help you:

I created a template and placed it in the Word Startup folder.
The following AutoExec macro results in run-time error 50289, “Can’t perform
operation since the project is protectedâ€, i.e. the error you see:

Sub AutoExec()

Dim n As Long
Dim odoc As Document

Set odoc = ThisDocument

'Show VBComponent names
With odoc.VBProject
For n = 1 To .VBComponents.Count
MsgBox .VBComponents(n).Name
Next n
End With

Set odoc = Nothing
End Sub


If I change the macro so that the add-in is opened as a document _before_
the VBComponents part of the code, everything works. The macro I used looks
like this:

Sub AutoExec()

Dim n As Long
Dim odoc As Document

'Open the add-in as document
Set odoc = Templates(ThisDocument.FullName).OpenAsDocument

'Show VBComponent names
With odoc.VBProject
For n = 1 To .VBComponents.Count
MsgBox .VBComponents(n).Name
Next n
End With

'Close document
odoc.Close savechanges:=wdDoNotSaveChanges
Set odoc = Nothing
End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
G

Greg Maxey

Lene,

Yes, I stumbled down a similar path earlier today. Thanks.


Lene said:
Greg,

Maybe the following will help you:

I created a template and placed it in the Word Startup folder.
The following AutoExec macro results in run-time error 50289, "Can't
perform operation since the project is protected", i.e. the error you
see:

Sub AutoExec()

Dim n As Long
Dim odoc As Document

Set odoc = ThisDocument

'Show VBComponent names
With odoc.VBProject
For n = 1 To .VBComponents.Count
MsgBox .VBComponents(n).Name
Next n
End With

Set odoc = Nothing
End Sub


If I change the macro so that the add-in is opened as a document
_before_ the VBComponents part of the code, everything works. The
macro I used looks like this:

Sub AutoExec()

Dim n As Long
Dim odoc As Document

'Open the add-in as document
Set odoc = Templates(ThisDocument.FullName).OpenAsDocument

'Show VBComponent names
With odoc.VBProject
For n = 1 To .VBComponents.Count
MsgBox .VBComponents(n).Name
Next n
End With

'Close document
odoc.Close savechanges:=wdDoNotSaveChanges
Set odoc = Nothing
End Sub

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


Nov 4, 2008. Remember it well. You just might
spend your sunset years telling your children and
your children's children what it was once like in
the United States where men were free.
 
J

Jonathan West

Greg Maxey said:
Lene,

Yes, I stumbled down a similar path earlier today. Thanks.

One of the completely wierd things about this is that Word, PowerPoint and
Excel have completely different approaches on this.

In Word, you can't access the code of add-ins, unless you have the
underlying template open as a document, but you can edit and save the code
if you do have the temmplate open.

In Powerpoint, you can't look at the code of an add-in at all unless you set
a specific entry in the registry. If you change that setting you can then
view and edit code in a .ppa add-in, but you can't save the changes. You
have to export the code, transfer it to the ppt file from which you created
the ppa, save the ppt and then re-save as a new ppa. Wierd.

In Excel, you can view and edit the code in .xla addins, and save the
changes direct from the VBA editor.

Don't you just love Microsoft's consistent approach to everything!
 
G

Greg Maxey

Love Microsoft? No. I do wish they would make Word like Excel in this
respect.

Thanks.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


Nov 4, 2008. Remember it well. You just might
spend your sunset years telling your children and
your children's children what it was once like in
the United States where men were free.
 

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