在名称文件中另存西里尔字母和特殊字符为xlsx文件

时间:2018-12-14 12:57:52

标签: excel vba

我必须以html格式另存为xlsx文件。当文件名包含特殊字符或西里尔字母时,出现错误。

如何转义这些特殊字符,以使转换为.html的文件与带有VBA指令的原始xlsx文件的名称相同。

当我通过传递excel手动进行转换时,输出文件(html)的名称与xlsx文件的名称相同。

例如,我有一个xlsx文件名

xzy1234567890!@నేనుÆды.test.ext.xlsx

转换后,我想要这个名字

xzy1234567890!@నేనుÆды.test.ext.htm

这是我的子

Public Sub convertFile(ByRef arg As Variant)

    On Error GoTo GestionErreurs

    Dim repout, nameFile, reponse As String, wk As Workbook
    Dim f As Variant
    Dim Cell As Object

    ' display file running
    display_running arg

    repout = Range("output").Value

    ' if arg or repout are empty launch error message
    If arg = "" Then
        reponse = MsgBox("Please fill in a file to convert !", vbExclamation, "Error")
        status = 0
        Exit Sub
    ElseIf repout = "" Then
        reponse = MsgBox("Please enter a directory for the convert file !", vbExclamation, "Error")
        status = 0
        Exit Sub
    End If


    ' check specials caracters
    For Each Cell In Range("words")
     If InStr(1, arg, Cell) > 0 Then
        'replace
        f = retFind(Cell, Range("spcarater"), 2)
        arg = Replace(arg, Cell, f)
    End If

    Next Cell


   ' build path and name of him file
    nameFile = repout & "\" & Mid(Dir(arg), 1, InStrRev(Dir(arg), ".") - 1) & ".htm"

    nameFile = WorksheetFunction.EncodeURL(nameFile)

    ' open Xlsx files to convert
    Workbooks.Open filename:=arg, ReadOnly:=True
    'Workbooks(Dir(arg)).Activate

    ' copy the firt sheet in new woorbook
    ActiveWorkbook.Sheets(1).Copy

    ' save as new wookbook in HTM format
    ActiveWorkbook.SaveAs nameFile, xlHtml

    ' close all wookbooks
    ActiveWorkbook.Close
    'Workbooks(Dir(arg)).Activate
    Workbooks(Dir(arg)).Close

    ' return staus 1 > OK
    status = 1

    Exit Sub

GestionErreurs:
    hide_Running
    MsgBox "A errur has occurred : " & Error & Chr(13) & "Conversion failure for the file: " & Chr(13) & arg, vbCritical
    status = 0

End Sub

0 个答案:

没有答案