does a certain member of a collection exist?

L

Larry

Basic question: How do you determine if a particular member of a
collection actually exists in that collection? For example, if a
certain document is in the documents collection, or a certain template
is in the addins collection?

Larry
 
J

Jeff Hall

Basic question: How do you determine if a particular member of a
collection actually exists in that collection? For example, if a
certain document is in the documents collection, or a certain template
is in the addins collection?

Dim myThingy as <whatever>
Dim myFlag as Boolean

myFlag = False
for each myThingy in ActiveDocument.<whatever-collection)

if myThingy.<property> = "whatIamLookingFor" then
myFlag = True
exit for
end if

next

--------------------------------------------------------------------
Jeff Hall MSc MRICS
Director, Eon Commerce Ltd.
http://www.eon-commerce.com

Software available for you to evaluate before buying...
EasyHTML/Help CHM file Editor for MS Word
http://www.easyhtmlhelp.com
--------------------------------------------------------------------
 
L

Larry

Thanks, I've tried different variations of this but it still doesn't
work. With myTemplate in the global dialog but not installed, I run the
macro, and nothing happens.

Dim myTemplate As AddIn
Dim myFlag As Boolean
myFlag = False

For Each myTemplate In AddIns

If myTemplate.Name = "C:\Program Files\Microsoft
Office\Templates\FixContext97.dot" Then
myFlag = True
If myFlag = True Then MsgBox "yes."
Exit For
End If
Next
 
L

Larry

Thanks much, Jay. I had played with the syntax several different ways
and kept missing it.

Larry
 
M

Mark Tangard

One minor tip: You don't have to set MyFlag to False explicitly
at the beginning. Boolean variables always initalize as False.
 
L

Larry

I sent this reply on Tuesday, but it never appeared in the group:


Thanks, all. I also deleted the Dim myFlag = False as Mark suggested.

For anyone who's interested, here's the final result. While I have many
macros using For Each, I had never known how to determine if something
was simply in a collection or not. The first part determines whether
it's in the collection. Then I run the second part of the macro which
takes one action or another depending on the result of the first part.

This macro was also tricky because of the double stage involved in
installing a global: first adding it to the collection, then installing
it. I found it was easier, instead of having it in the collection all
the time and either installing or unstalling, to do both steps each
time, so it's either in the collection and installed, or not in the
collection.

Dim myTemplate As AddIn
Dim myFlag As Boolean
Dim FixContext As String
FixContext = "C:\Program Files\Microsoft
Office\Templates\FixContext97.dot"
System.Cursor = wdCursorNormal

' Determine if FixContext is in AddIns collections.
For Each myTemplate In AddIns
If myTemplate.Name = "FixContext97.dot" Then
myFlag = True
Exit For
End If
Next

' If FixContext is not in collection, add it.
If myFlag = False Then
If MsgBox("FixContext template is currently NOT INSTALLED as Global."
_
& vbCr & "INSTALL now.", vbOKCancel, _
"Toggle FixContext Template as Global") = vbOK Then _
AddIns.Add FileName:=FixContext, Install:=True
Else
' If FixContext is in collection, delete it.
If MsgBox("FixContext template is currently INSTALLED as Global." _
& vbCr & "UNINSTALL now.", vbOKCancel, _
"Toggle FixContext Template as Global") = vbOK Then _
AddIns(FixContext).Delete
End If
 
L

Larry

Hi Jezebel (that's a strange name to pick considering what happened to
Jezebel in the Bible :) ),

I couldn't get your code with the On Error Resume Next statement to
work, and I don't understand it, but I tried to use a similar idea, that
is, to find out if a member exists in a collection without using a For
Each statement. This works, but maybe you can show me how to improve
it.

Dim myDoc As Document
On Error GoTo Message
Set myDoc = Documents("aaaaa.doc")
If myDoc = ActiveDocument Then
MsgBox "my doc is active document."
Else
MsgBox "my doc is not active document."
End If
Exit Sub
Message:
MsgBox "doc not in documents collection."

Thanks,
Larry
 
J

Jeff Hall

One minor tip: You don't have to set MyFlag to False explicitly
at the beginning. Boolean variables always initalize as False.

I didn't know that. I also thought it was good practice explicitly to
initialise all variables?

--------------------------------------------------------------------
Jeff Hall MSc MRICS
Director, Eon Commerce Ltd.
http://www.eon-commerce.com

Software available for you to evaluate before buying...
EasyHTML/Help CHM file Editor for MS Word
http://www.easyhtmlhelp.com
--------------------------------------------------------------------
 

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