0

I have Powerpoint presentation. On each slide I have 8 shapes with text space. They can contain text representing group which has sth related to content/data update and so on. I have following arrays which contain users for those responsibility area:

GEN = Array("username_01","username_02","username_03",..."username_xx")
POL = Array("username_01","username_02","username_03",..."username_xx")
B2B = Array("username_01","username_02","username_03",..."username_xx")
RUS = Array("username_01","username_02","username_03",..."username_xx")

And this function which is checking if user is in array

   Function IsInArray(stringToBeFound As Variant, arr As Variant) As Boolean
   IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
   End Function

My problem is that when I want to use the function it works only if I will give the Array name as below:

auser = Environ("UserName")
IsInArray(auser,GEN) 'it will give me answer if the user is in array

I want to get shape text:

res_group_txt = ActivePresentation.Slides(i).Shapes(shape_owner).TextEffect.Text

And put it somehow in a function so it will not return an error

auser = Environ("UserName")
IsInArray(auser,res_group_txt)

I have tried to change variables and look through all topics but I have not found answer :(

Help pls :)

BR Misza

1
  • Have you tried changing the parameters of the filter function to CompareMethod.Text or CompareMethod.binary? Commented Jul 8, 2017 at 17:08

2 Answers 2

1

You can use a Dictionary object to map the text to the array...

Dim oDic As Object
Dim GEN As Variant
Dim POL As Variant
Dim B2B As Variant
Dim RUS As Variant

GEN = Array("username_01", "username_02", "username_03")
POL = Array("username_01", "username_02", "username_03")
B2B = Array("username_01", "username_02", "username_03")
RUS = Array("username_01", "username_02", "username_03")

Set oDic = CreateObject("Scripting.Dictionary")
oDic.comparemode = vbTextCompare

oDic("GEN") = GEN
oDic("POL") = POL
oDic("B2B") = B2B
oDic("RUS") = RUS

Then you can call your function as follows...

IsInArray(auser, oDic(res_group_txt))
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, the answer is 'yes', you can access these arrays by name. You'd use the CallByName() function which enables you to access any property (and, indeed, method) of an object by its name, passed as a string.

The small adjustment you'd need to make to your code would be to create an object which contained the arrays as properties. Specifically, you'd do this by inserting a Class object (Insert > Class Module). In the example below, I've called the class cArrayFields and added your code as follows:

Option Explicit

Public GEN As Variant
Public POL As Variant
Public B2B As Variant
Public RUS As Variant

Private Sub Class_Initialize()
    GEN = Array("username_01", "username_02", "username_03", "username_04")
    POL = Array("username_02", "username_03", "username_04")
    B2B = Array("username_03", "username_04")
    RUS = Array("username_04")
End Sub

In your main procedure (the one in your module), your code would simply be:

Dim o As cArrayFields
Dim targetShape As Shape
Dim targetName As String, shapeText As String, aUser As String
Dim arr As Variant
Dim i As Long


targetName = "MyShape"
aUser = "username_03" 'test example

Set o = New cArrayFields
For i = 1 To 4
    Set targetShape = ActivePresentation.Slides(i).Shapes(targetName)
    shapeText = targetShape.TextEffect.Text
    arr = CallByName(o, shapeText, VbGet)
    Debug.Print IsInArray(aUser, arr)
Next

However, I wonder if your users and responsibilities are structured in the most efficient manner. A more intuitive way to go might be to have a list of users and each member contains a list of the areas they are responsible for. If you did it this way, then the look-up would be far simpler; for example you could just use a Collection object, which accesses each item by a String key. So your code could just be a couple of small routines to create the lists:

Private Sub DefineUserList()
    Set mUsers = New Collection

    AddNewUser "username_01", "GEN"
    AddNewUser "username_02", "GEN", "POL"
    AddNewUser "username_03", "GEN", "POL", "B2B"
    AddNewUser "username_04", "GEN", "POL", "B2B", "RUS"
End Sub
Private Sub AddNewUser(userName, ParamArray respAreas() As Variant)
    Dim resp As Collection
    Dim v As Variant

    Set resp = New Collection
    For Each v In respAreas
        resp.Add True, CStr(v)
    Next
    mUsers.Add resp, userName

End Sub

And then your look-up routines in your main module as follows:

Option Explicit

Private mUsers As Collection

Public Sub Main()
    Dim targetShape As Shape
    Dim targetName As String, shapeText As String, aUser As String
    Dim i As Long


    DefineUserList

    targetName = "MyShape"
    aUser = "username_03" 'test example

    For i = 1 To 4
        Set targetShape = ActivePresentation.Slides(i).Shapes(targetName)
        shapeText = targetShape.TextEffect.Text
        Debug.Print IsUsersArea(aUser, shapeText)
    Next
End Sub

Private Function IsUsersArea(userName As String, respArea As String) As Boolean
    On Error Resume Next
    IsUsersArea = mUsers(userName).Item(respArea)
    On Error GoTo 0
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.