Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nonbreaking hyphen 1

Status
Not open for further replies.

kyda48

Technical User
Jul 1, 2010
2
US
I am trying to write a macro for Word 2007 in which I have an If statement that seeks to determine if a string is a nonbreaking hyphen. I have tried:

If string = "Chr(30)"
If string = "-"
If string = "^~"


I have even tried to copy and paste the exact character I want into the VBA but then I get:

If string = " "

and it still doesn't recognize it.


Any ideas on what VBA recognizes as the nonbreaking hyphen?

 
Well, this looks dodgy:
If string = "Chr(30)"

surely it would be:
If string = Chr(30)

Cheers, Glenn.

Beauty is in the eye of the beerholder.
 
What is more: you are using a reserved word ("string") as variable name.
That's a no-no.

Rename your variable to something else.
"strng" will already do.

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
>surely it would be:
If string = Chr(30)

A pedant writes ...

If string = Chr$(30)
 
Thanks GlennUK. I was comparing the string to other punctuation marks and they had the quotation marks around them. I didn't even stop to think that I didn't need it for this one. Also, the "string" I used in the post was just for illustrative purposes. I did not actually use the word "string" as my string. Thanks to all who responded.
 
You are testing for a single character? Seems a little odd. How are you working up to getting that character. Just as an afterthought:
Code:
Sub HyphenOrNotHypen()
If InStr(1, Selection.Text, Chr(30)) > 0 Then
   MsgBox "Yes, it does."
Else
   MsgBox "No, it does not."
End If
End Sub
Checks to see if there is a Chr(30) hypen anywhere in the selected text. It could just as easily be a given string:
Code:
Sub HyphenOrNotHypen()
Dim myString As String
myString = "Yad" & Chr(30) & "dayadda"
If InStr(1, myString, Chr(30)) > 0 Then
   [highlight]MsgBox "Yes, it does."[/highlight]
Else
   MsgBox "No, it does not."
End If
End Sub
Or possibly a Function.
Code:
Function IsHypenOrNot(strIn As String) As Boolean
If InStr(1, strIn, Chr(30)) > 0 Then
   IsHypenOrNot = True
End If
End Function

Sub Yadda()
If IsHypenOrNot(ActiveDocument.Paragraphs(1).Range.Text) Then
   MsgBox "The first paragraph has a non-breaking hypen."
End If
End Sub

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top