2014-12-14 2 views
0

파일 이름을 함께 입력 할 필드를 선택하는 Word 2013 템플릿 용 VB 스크립트가 있습니다.Visual Basic에서 파일 이름에 쓸모없는 문자를 대체하십시오.

원하지 않는 문자, 기호 및 특수 문자를 파일 이름으로 대체하려면 어떻게해야합니까?

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 

최종 하위

답변

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 
+0

이 기능 반환 무엇? – cy221

+0

예 ... 분명히 일부 부품을 놓쳤습니다. 편집 해 주셔서 감사합니다. – Matt

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