Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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
Regex 在Visual Basic中替换文件名中不需要的字符_Regex_Vba_Ms Word - Fatal编程技术网

Regex 在Visual Basic中替换文件名中不需要的字符

Regex 在Visual Basic中替换文件名中不需要的字符,regex,vba,ms-word,Regex,Vba,Ms Word,我有一个Word 2013模板的VB脚本,它选择字段将文件名放在一起 如何将文件名中所有不需要的字母、符号和特殊字符替换为字符串 以下是脚本: Imports System.Text.RegularExpressions ' wrong? Sub FileSave() If ActiveDocument.Path = "" Then FileSaveAs Exit Sub End If ActiveDocument.Save End Sub Sub FileSaveAs()

我有一个Word 2013模板的VB脚本,它选择字段将文件名放在一起

如何将文件名中所有不需要的字母、符号和特殊字符替换为字符串

以下是脚本:

Imports System.Text.RegularExpressions ' wrong?

Sub FileSave()

If ActiveDocument.Path = "" Then
    FileSaveAs
    Exit Sub
End If
ActiveDocument.Save

End Sub
Sub FileSaveAs()

Dim DocName As String

If (ActiveDocument.Bookmarks.Exists("Datum") = True) And (ActiveDocument.Bookmarks.Exists("Betreff") = True) And (ActiveDocument.Bookmarks.Exists("Bezug") = True) Then
    a = Trim(ActiveDocument.Bookmarks("Datum").Range.Text)
    b = Trim(ActiveDocument.Bookmarks("Betreff").Range.Text)
    c = Trim(ActiveDocument.Bookmarks("Bezug").Range.Text)
    DocName = a & " - " & b & " - " & c
    DocName = Replace(DocName, "/^*\s*/$", "") ' wrong

Else
    DocName = ActiveDocument.Name
End If

With Dialogs(wdDialogFileSaveAs)
    .Name = DocName
    .Show
End With

End Sub

在这些情况下,我使用类似的方法。您可以根据需要进行更改,但它会用下划线替换Windows文件路径中的所有特殊字符

Function CreateFriendlyName(pText as string) as String

    Dim objRegex As Object

    ' Initiate regex search
    Set objRegex = CreateObject("VBScript.RegExp")
    With objRegex
        .Pattern = "(\s|\\|/|<|>|\|\|\?|:)"
        .Global = True
        .IgnoreCase = True
    End With

    CreateFriendlyName = objRegex.Replace(pText , "_")

    Set objRegex = Nothing
End Function
函数CreateFriendlyName(pText作为字符串)作为字符串
作为对象的Dim objRegex
'启动正则表达式搜索
设置objRegex=CreateObject(“VBScript.RegExp”)
用objRegex
.Pattern=“(\s|\\\\\\\\/|\\\\\\\\?\:)”
.Global=True
.IgnoreCase=True
以
CreateFriendlyName=objRegex.Replace(pText,“389;”)
设置objRegex=Nothing
端函数
公共函数CreateFriendlyName(pText作为字符串)作为字符串
作为对象的Dim objRegex
'启动正则表达式搜索
设置objRegex=CreateObject(“VBScript.RegExp”)
用objRegex
.Pattern=“(\s|\\\\\\\\/|\\\\\\\\?\:)”
.Global=True
.IgnoreCase=True
以
CreateFriendlyName=objRegex.Replace(pText,“389;”)
设置objRegex=Nothing
端函数
子文件保存()
如果ActiveDocument.Path=”“,则
文件存储
出口接头
如果结束
ActiveDocument.Save
端接头
子文件saveas()
将DocName设置为字符串
如果(ActiveDocument.Bookmarks.Exists(“Datum”)=True)和(ActiveDocument.Bookmarks.Exists(“Betreff”)=True)和(ActiveDocument.Bookmarks.Exists(“Bezug”)=True),则
a=修剪(ActiveDocument.Bookmarks(“基准”).Range.Text)
b=Trim(ActiveDocument.Bookmarks(“Betreff”).Range.Text)
c=Trim(ActiveDocument.Bookmarks(“Bezug”).Range.Text)
DocName=a&“-”和b&“-”和c
DocName=CreateFriendlyName(DocName)
其他的
DocName=ActiveDocument.Name
如果结束
带对话框(wdDialogFileSaveAs)
.Name=DocName
显示
以
端接头

此函数返回什么?是的。。。显然遗漏了一些部分。谢谢你的编辑。
Public Function CreateFriendlyName(pText As String) As String
Dim objRegex As Object

' Initiate regex search
Set objRegex = CreateObject("VBScript.RegExp")
With objRegex
    .Pattern = "(\s|\\|/|<|>|\|\|\?|:)"
    .Global = True
    .IgnoreCase = True
End With

CreateFriendlyName = objRegex.Replace(pText, "_")
Set objRegex = Nothing
End Function


Sub FileSave()

If ActiveDocument.Path = "" Then
    FileSaveAs
    Exit Sub
End If
ActiveDocument.Save

End Sub
Sub FileSaveAs()

Dim DocName As String

If (ActiveDocument.Bookmarks.Exists("Datum") = True) And (ActiveDocument.Bookmarks.Exists("Betreff") = True) And (ActiveDocument.Bookmarks.Exists("Bezug") = True) Then
    a = Trim(ActiveDocument.Bookmarks("Datum").Range.Text)
    b = Trim(ActiveDocument.Bookmarks("Betreff").Range.Text)
    c = Trim(ActiveDocument.Bookmarks("Bezug").Range.Text)
    DocName = a & " - " & b & " - " & c
    DocName = CreateFriendlyName(DocName)

Else
    DocName = ActiveDocument.Name
End If

With Dialogs(wdDialogFileSaveAs)
    .Name = DocName
    .Show
End With

End Sub