MS Office Forum / Word / Programming / January 2006
How to get the index numbers of certain members of a collection in order to use them later?
|
|
Thread rating:  |
Larry - 28 Jan 2006 20:32 GMT I have a macro that inadvertently uninstalls all the installed Add-ins. Nothing can be done about that. So I want to create code that, before the macro runs, gets the index number of each of the currently installed add-ins, and then, after the macro runs, uses those index numbers to re-install the add-ins that have been uninstalled.
Let's say there are four add-ins in the Add-ins collection, and the first and third Addin are currently installed. I run something like this:
For Each myAddin In Application.AddIns If myAddin.Installed = True Then myAddin.Installed = False X = myAddin.index End If Next myAddin
But of course after this code is run, X will only be equal to 3, the index of the last addin that was installed. What code do I put in that place that would reserve the index number of all of the add-ins that are being uninstalled? And then how would I access those numbers to re-install the add-ins later?
I've only used very simple arrays in the past so I'm not expert in arrays at all. Though maybe an array would not be necessary.
Thanks for any help. Larry
Helmut Weber - 28 Jan 2006 21:46 GMT Hi Larry,
most of the following is untested, as I don't dare to install a dozen addins.
You can count the addins: AddIns.Count
You can dimension an array: Dim ArrAdd() As String ReDim ArrAdd(AddIns.Count)
You can store the addins fullname there:
For l = 1 To AddIns.Count ArrAdd(l) = AddIns(1).Path & "\" & AddIns(l).Name Next
Note that without "option base 1" there is an unused entry (0) in the array. which doesn't do any harm.
You still have to store the names somewhere, if the addins are _deleted_, not just uninstalled. An uninstalled addin is still in the addins collection.
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
-- Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Helmut Weber - 28 Jan 2006 22:06 GMT Another typo,
just in case you get puzzled:
> ArrAdd(l) = AddIns(1).Path & "\" & AddIns(l).Name ! ArrAdd(l) = AddIns(l).Path & "\" & AddIns(l).Name ^
To be viewed with an non-proportional font.
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Larry - 29 Jan 2006 00:32 GMT > For l = 1 To AddIns.Count > ArrAdd(l) = AddIns(1).Path & "\" & AddIns(l).Name > Next But this would be reserving the whole collection. I only want to reserve the add-ins that are currently installed. However, maybe that will work anyway. Let's continue.
I don't need the whole path, only the name is needed to re-install an uninstalled add-in which is still in the collection.
So, let's say I do this with four add-ins, two of which are currently installed:
Dim ArrAdd() As String ReDim ArrAdd(AddIns.Count)
For i = 1 To AddIns.Count If AddIns(i).Installed = True Then ArrAdd(i) = AddIns(i).Name AddIns(i).Installed = False End If Next
At this point, all the add-ins have been uninstalled. Now the question is, how do I re-install only the add-ins that were uninstalled by the macro? (I don't want to install the ones that were not installed to begin with.)
> Hi Larry, > [quoted text clipped - 19 lines] > if the addins are _deleted_, not just uninstalled. > An uninstalled addin is still in the addins collection. Helmut Weber - 29 Jan 2006 10:12 GMT Hi Larry
hmm, if I get you right, you want a list of all installed addins.
Like this and in many other was:
Sub Test500() Dim oAdd As AddIn Dim ArrAdd() As String Dim l As Long l = 0 For Each oAdd In AddIns If oAdd.Installed = True Then l = l + 1 ReDim Preserve ArrAdd(l) ArrAdd(l) = oAdd.Name End If Next ' call another macro to uninstall some or all addins ' restore the uninstalled addins For l = 1 To UBound(ArrAdd) If AddIns(ArrAdd(l)).Installed = False Then AddIns(ArrAdd(l)).Installed = True End If MsgBox ArrAdd(l) ' for testing Next
End sub
HTH
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Larry - 29 Jan 2006 10:58 GMT Thank you very much Helmut. I've got it working. I just had to make two changes.
The first change was cosmetic, so that I could read it better. I changed your "I", which in my code window is virtually identical to the number "1", to a little "i" so I could read it.
Then I ran into an error with this line:
If AddIns(ArrAdd(i)).Installed = False Then
And I understand why (ArrAdd(i)) is a string, not an index number.
So to re-install the installed add-ins, I used this code instead :
' restore the uninstalled addins For i = 1 To UBound(ArrAdd) ' I'm adding this For Each oAdd In AddIns If oAdd.Name = ArrAdd(i) Then oAdd.Installed = True End If Next MsgBox ArrAdd(i) ' for testing Next
Now the whole test macro looks like this:
Dim oAdd As AddIn Dim ArrAdd() As String Dim i As Long i = 0 For Each oAdd In AddIns If oAdd.Installed = True Then i = i + 1 ReDim Preserve ArrAdd(i) ArrAdd(i) = oAdd.Name ' I'm adding this line to uninstall the installed add-in oAdd.Installed = False End If Next ' I added this: MsgBox "Number of uninstalled add-ins in array " & UBound(ArrAdd)
' restore the uninstalled addins For i = 1 To UBound(ArrAdd) ' I'm adding this For Each oAdd In AddIns If oAdd.Name = ArrAdd(i) Then oAdd.Installed = True End If Next MsgBox ArrAdd(i) ' for testing Next
Larry
> Hi Larry > [quoted text clipped - 27 lines] > > HTH Larry - 29 Jan 2006 15:42 GMT Helmut,
There's something about this code I don't understand, the meaning of ArrAdd(i).
Look at these two lines.
ReDim Preserve ArrAdd(i) ArrAdd(i) = oAdd.Name
The first line is changing the number of elements in the array, as represented by ArrAdd(i). The second line is setting the current array element, represented by ArrAdd(i), as the name of the current add-in. So ArrAdd(i) is both the array, AND a particular element of the array. Is that the correct way of understanding it?
Larry
> Thank you very much Helmut. I've got it working. I just had to make > two changes. [quoted text clipped - 84 lines] > > > > HTH Jay Freedman - 29 Jan 2006 16:41 GMT >Helmut, > [quoted text clipped - 13 lines] > >Larry Hi Larry,
Not quite...
The ReDim statement changes the size of the array. ArrAdd is the name of the array to change, and the (i) says what the new size should be.
In the assignment statement, the (i) selects a particular element from the array.
Despite the similar appearance of ArrAdd(i) in both places, the uses are different. I suppose when the ReDim statement was being created, they could have made the syntax something else, like
ReDim ArrAdd() To i
but they didn't. It's just one of those things you need to get used to. :-)
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Larry - 29 Jan 2006 16:54 GMT Thanks, Jay.
> >Helmut, > > [quoted text clipped - 39 lines] > Email cannot be acknowledged; please post all follow-ups to the > newsgroup so all may benefit. Larry - 31 Jan 2006 21:49 GMT This will be kindergarten stuff to most people here, but arrays are new to me and I'm fascinated by them, the way such simple code can be so powerful. Here's a practice array I just wrote which types a paragraph below the current paragraph in which the word order of the first paragraph is reversed.
Application.ScreenUpdating = False Dim myString() As String Dim i As Long i = 1 For i = 1 To Selection.Paragraphs(1).Range.Words.Count Selection.Paragraphs(1).Range.Words(i).Select ReDim Preserve myString(i) myString(i) = Selection.Text Next
Selection.Paragraphs(1).Range.Collapse wdCollapseEnd Selection.TypeParagraph
For i = UBound(myString) To 1 Step -1 Selection.TypeText myString(i) Next
> Thanks, Jay. > [quoted text clipped - 43 lines] > > Email cannot be acknowledged; please post all follow-ups to the > > newsgroup so all may benefit. Larry - 31 Jan 2006 23:43 GMT Or, even better, this macro reverses not just the word order but the character order, though it takes a while to run.
Application.ScreenUpdating = False Dim r As Range Set r = Selection.Range
Dim myString() As String Dim i As Long i = 1 For i = 1 To Selection.Paragraphs(1).Range.Characters.Count Selection.Paragraphs(1).Range.Characters(i).Select ReDim Preserve myString(i) myString(i) = Selection.Text Next
Selection.Paragraphs(1).Range.Collapse wdCollapseEnd Selection.TypeParagraph
For i = UBound(myString) To 1 Step -1 Selection.TypeText myString(i) Next r.Select
> This will be kindergarten stuff to most people here, but arrays are new > to me and I'm fascinated by them, the way such simple code can be so [quoted text clipped - 71 lines] > > > Email cannot be acknowledged; please post all follow-ups to the > > > newsgroup so all may benefit.
|
|
|