我想添加一个替换无效字符的函数(!“#¤%& /()=?`^ *>;:@£$ {[]} |〜\,。'¨' + - )在文件名中带有“_”。
这是我当前(并且正在工作)的宏:
Sub Save_to_PDF()
Dim v As Variant
Dim name, datedd As String
ThisWorkbook.Sheets(Array("sheet1")).Select
datedd = Date
strFilename = Worksheets("sheet1").Range("B2")
v = Application.GetSaveAsFilename(strFilename & " document " & datedd & " .pdf", "PDF Files (*.pdf), *.pdf")
On Error GoTo openfile
If FileExists(v) Then
Kill v
End If
If VarType(v) = vbString Then
ThisWorkbook.Sheets("sheet1").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=v, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, From:=1, To:=1, OpenAfterPublish:=True
End If
Exit Sub
openfile:
MsgBox "You have one file open allready whith that name. Please try again", vbInformation, "Please close file"
Exit Sub
End sub
答案 0 :(得分:0)
以下函数将替换预先选择的字符:
Function clean_filename(fname As String, replace_with As String)
Dim inv_chars As String
Dim cpos As Long
invchars = "!""#¤%&/()=?`^*>;:@£${[]}|~\,.'¨´+-"
For cpos = 1 To Len(invchars)
fname = Replace(fname, Mid(invchars, cpos, 1), replace_with)
Next
clean_filename = fname
End Function
您可以在捕获v(作为字符串)后添加v = clean_filename(v,"_")
来使用此功能。
但是,您已将.
和/
包含在无效字符列表中,并且两者对文件名都很重要,因此您可能需要考虑将其删除和/或以不同方式处理这些文件名