Harsh Kumar
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
please advise
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
please advise
Perhaps...
If ActiveDocument.Bookmarks(bmName) = "side1" Or ActiveDocument.Bookmarks(bmName) = "side2" Then
' do something
end if
Jinx :)
Use Bill's answer to prevent issues with mixed case words
Use Bill's answer to prevent issues with mixed case words
ASKER
Thanks guys for your answer... hmmmm.. and what if i have like 20 bms like "side1", "side 2", "side 3", "side4" etc.
ASKER
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
Function
Example
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
Example
If IsExcluded(bmName, ListOfExcludedBm) Then
'do something
End if
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
~bp
ASKER
@BP - Cant get the this to work :
@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....
strMatch = "side1,side2,side3,side4"
If Instr(1, "," & ActiveDocument.Bookmarks(bmName) & ",", "," & strMatch & ",", 1) > 0 Then
' do something
end if
@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....
ASKER
This works for me:
But I guess there could be a more clever way
If LCase(wdDoc.Bookmarks(sbkmk(tmp2))) = "side1" Or LCase(wdDoc.Bookmarks(sbkmk(tmp2))) = "side2" Then
But I guess there could be a more clever way
ASKER
@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
~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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks alot guys! @BP- Thanks alot! I used your solution since it matches the code i already have.
@Shaun - thanks alot for your time!
@Shaun - thanks alot for your time!
Open in new window
~bp