我有一个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
答案 0 :(得分:0)
我在这些情况下使用类似的东西。您可以根据需要更改它,但它会替换所有带有下划线的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
答案 1 :(得分:0)
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