Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 检查字符串是否包含引号(";)_Vba_String_Ms Word_Character_Special Characters - Fatal编程技术网

Vba 检查字符串是否包含引号(";)

Vba 检查字符串是否包含引号(";),vba,string,ms-word,character,special-characters,Vba,String,Ms Word,Character,Special Characters,我想在字符串中搜索一个字符。我正在使用InStr和 我想返回将传递给用户表单的字符 如果我手动将字符串指定给变量,我的代码工作正常。例如: ProtFunc = "21G_TRIP""" 代码将找到“,然后返回一个消息框,显示: “坏性格” 使用Word文档中的选项时会出现问题。它查找“,但不返回字符 见下面的代码: 注意:我看到的部分代码是从各种论坛上使用的 Sub CharFind () 'For testing purposes 'ProtFunc = "21G_Trip""" Pro

我想在字符串中搜索一个字符。我正在使用
InStr
和 我想返回将传递给用户表单的字符

如果我手动将字符串指定给变量,我的代码工作正常。例如:

ProtFunc = "21G_TRIP"""
代码将找到
,然后返回一个消息框,显示:

“坏性格”

使用Word文档中的选项时会出现问题。它查找
,但不返回字符

见下面的代码:

注意:我看到的部分代码是从各种论坛上使用的

Sub CharFind ()

'For testing purposes
'ProtFunc = "21G_Trip"""
ProtFunc = Selection

'Checking the ProtFunc for invalid Characters
'This searches for invalid characters in the ProtFunc String and will inform    user of error


'Forsome reason the search picks up an additional character,
'the line below removes the last charater of the ProtFunc

'ProtFunc = Left(ProtFunc, Len(ProtFunc) - 1)


'The following characters will produce invalid file paths: \ / : * ? " < > |
'Additionally the following are checked too:     , . ;

 strTemp = ProtFunc

If (InStr(1, strTemp, "/") > 0) Or (InStr(1, strTemp, "") > 0) Or _
(InStr(1, strTemp, "*") > 0) Or (InStr(1, strTemp, "?") > 0) Or _
(InStr(1, strTemp, "|") > 0) Or (InStr(1, strTemp, """") > 0) Or _
(InStr(1, strTemp, "<") > 0) Or (InStr(1, strTemp, ">") > 0) Or _
(InStr(1, strTemp, ":") > 0) Then




'Assigning the invaild charater to be displayed
If (InStr(1, strTemp, ",") > 0) Then Char = "," Else
 If (InStr(1, strTemp, ".") > 0) Then Char = "." Else
  If (InStr(1, strTemp, ":") > 0) Then Char = ":" Else
   If (InStr(1, strTemp, ";") > 0) Then Char = ";" Else
    If (InStr(1, strTemp, "\") > 0) Then Char = "\" Else
     If (InStr(1, strTemp, "/") > 0) Then Char = "/" Else
      If (InStr(1, strTemp, "*") > 0) Then Char = "*" Else
       If (InStr(1, strTemp, "?") > 0) Then Char = "?" Else
        If (InStr(1, strTemp, """") > 0) Then Char = """" & BDBDBDB Else
         If (InStr(1, strTemp, "<") > 0) Then Char = "<" Else
          If (InStr(1, strTemp, ">") > 0) Then Char = ">" Else
           If (InStr(1, strTemp, "|") > 0) Then Char = "|"





MsgBox "Bad Character." & vbCr & Char

'Assigning the the invaild character to the ListBox in the User Form       ErrorMsgProtName
ErrorMsgProtName.ListBox1.AddItem (Char)
'Showing the User Form ErrorMsgProtName
ErrorMsgProtName.Show
'Clearing the ListBox in the User Form ErrorMsgProtName for the next round
ErrorMsgProtName.ListBox1.Clear

Else

MsgBox ProtFunc & "Is fine"

End If

'InvalChar Search - End



End Sub
子字符查找()
“用于测试目的
'ProtFunc=“21G\U行程”“”
ProtFunc=选择
'正在检查ProtFunc中的无效字符
'这将搜索ProtFunc字符串中的无效字符,并将通知用户错误
“出于某种原因,搜索会选择另外一个字符,
'下一行删除ProtFunc的最后一个字符
'ProtFunc=Left(ProtFunc,Len(ProtFunc)-1)
'以下字符将产生无效的文件路径:\/:*?“<>|
'此外,还检查了以下内容:;
strTemp=ProtFunc
如果(InStr(1,strTemp,“/”)>0)或(InStr(1,strTemp,“”)>0)或_
(InStr(1,strTemp,“*”)大于0)或(InStr(1,strTemp,“?”)大于0)或_
(InStr(1,strTemp,“|”)大于0)或(InStr(1,strTemp,“”)>0)或_
(InStr(1,strTemp,“”)>0)或_
(InStr(1,strTemp,“:”)>0)然后
'指定要显示的invaild字符
如果(InStr(1,strTemp,“,”)>0),则Char=“,”Else
如果(InStr(1,strTemp,“.”)大于0,则Char=“.”Else
如果(InStr(1,strTemp,“:”)>0),则Char=“:”Else
如果(InStr(1,strTemp,“;”)>0),则Char=“;”Else
如果(InStr(1,strTemp,“\”)>0),则Char=“\”Else
如果(InStr(1,strTemp,“/”)大于0,则Char=“/”Else
如果(InStr(1,strTemp,“*”)>0),则Char=“*”Else
如果(InStr(1,strTemp,“?”)大于0,则Char=“?”Else
如果(InStr(1,strTemp,“”“”)大于0,则Char=“””&bdb-Else
如果(仪表1,strTemp),“其他
如果(InStr(1,strTemp,“|”)大于0,则Char=“|”
MsgBox“坏字符”。&vbCr&Char
'将invaild字符分配给用户表单ErrorMsgProtName中的列表框
ErrorMsgProtName.ListBox1.AddItem(字符)
'显示用户表单ErrorMsgProtName
ErrorMsgProtName.Show
'为下一轮清除用户表单ErrorMsgProtName中的列表框
ErrorMsgProtName.ListBox1.Clear
其他的
MsgBox ProtFunc&“很好”
如果结束
'无效字符搜索-结束
端接头
Word可能使用了“智能报价”而不是标准报价:

”    Smart quote. Unicode hex value &H201D.
"    Standard quote. Unicode hex value &H22.
smart quote可以在Windows路径中使用,因此无需删除它,除非您真的想删除它

顺便说一句,正则表达式会让你的生活更轻松:

Dim re
Set re=CreateObject(“VBScript.RegExp”)
re.Pattern=“[\\/:\*\?”“\\\\;]”
如果重新测试(strTemp),则
MsgBox“坏字符”。&vbCr&re.Execute(strTemp)(0)
其他的
MsgBox strTemp&“很好”
如果结束

Then Char=“”&bdbdbdbdb行中的
bdbdb
是什么?抱歉,我只是想看看Msgbox中是否显示了正确的字符。请原谅我不知道regex?一个正则表达式。我在下面的回答中显示了一个示例。因此代码重新执行(strTemp)(0)提供坏字符?我可以将其存储为字符串并传递到UserForm?我看到word正在文档中使用智能引号。是的。您可以将
re.Execute(strTemp)(0)
分配给字符串变量以保存无效字符:
Char=re.Execute(strTemp)(0)
。太棒了,我很感激你的帮助
Dim re
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "[\\/:\*\?""<>\|]"

If re.Test(strTemp) Then
    MsgBox "Bad Character." & vbCr & re.Execute(strTemp)(0)
Else
    MsgBox strTemp & " is fine"
End If