Link to home
Start Free TrialLog in
Avatar of Harsh Kumar
Harsh KumarFlag for Denmark

asked on

VBA - If Bookmark = "XXBOOKMARKXX" then

Hi guys,

I need some quick help again,

bmName is a variable that loops through Bookmarks, and I need to check that when bookmark "side1" or when bookmark "side2" is bmName then it should do something.

What am i doing wrong?


this is the code I'm fiddling with
If ActiveDocument.Bookmarks(bmName) = "side1" Or "side2" Then

' do something

end if

Open in new window


please advise
Avatar of Bill Prew
Bill Prew

Perhaps try:

If LCASE(ActiveDocument.Bookmarks(bmName)) = "side1" Or LCASE(ActiveDocument.Bookmarks(bmName)) = "side2" Then

' do something

end if

Open in new window

~bp
Perhaps...
If ActiveDocument.Bookmarks(bmName) = "side1" Or ActiveDocument.Bookmarks(bmName) = "side2" Then

' do something

end if

Open in new window

Jinx :)
Use Bill's answer to prevent issues with mixed case words
Avatar of Harsh Kumar

ASKER

Thanks guys for your answer... hmmmm.. and what if i have like 20 bms like "side1", "side 2", "side 3", "side4" etc.
i'm guessing i will need to pars it through a function? is there a way to create an index of some kind where I can compare the bmName with a list? something like

If ActiveDocument.Bookmarks(bmName) = ListOfExcludedBm Then

'do something

end if

Open in new window

Function
Function IsExcluded(Bookmark As String, ListOfExcluded As Range)
    For Each Cell In ListOfExcluded
        If Bookmark = Cell.Text Then
            IsExcluded = True
            Exit Function
        End If
    Next
    IsExcluded = False
End Function

Open in new window


Example
If IsExcluded(bmName, ListOfExcludedBm) Then

'do something

End if

Open in new window

If you really have 20 or so then you might want to take a slightly different approach, and have a long string with the various values like:

strMatch = "side1,side2,side3,side4"
If Instr(1, "," & ActiveDocument.Bookmarks(bmName) & ",", "," & strMatch & ",", 1) > 0 Then
    ' do something
end if

Open in new window

~bp
@BP - Cant get the this to work :
strMatch = "side1,side2,side3,side4"
If Instr(1, "," & ActiveDocument.Bookmarks(bmName) & ",", "," & strMatch & ",", 1) > 0 Then
    ' do something
end if

Open in new window


@Shaun - Trying to understand your function.... how will side1, side2 etc. be determinted? there is other bookmarks like page1,page2 that don't need to do something....
This works for me:
                If LCase(wdDoc.Bookmarks(sbkmk(tmp2))) = "side1" Or LCase(wdDoc.Bookmarks(sbkmk(tmp2))) = "side2" Then

Open in new window


But I guess there could be a more clever way
@BP - how should i Dim it? as string?
@Shaun - Trying to understand your function.... how will side1, side2 etc. be determinted? there is other bookmarks like page1,page2 that don't need to do something....
By selecting a range
Yes, Dim as string.

~bp
@Shaun - Trying to understand your function.... how will side1, side2 etc. be determinted? there is other bookmarks like page1,page2 that don't need to do something....

By selecting a range or named range
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks alot guys! @BP- Thanks alot! I used your solution since it matches the code i already have.
@Shaun - thanks alot for your time!