简体   繁体   中英

Excel VBA to copy and paste non english text in HTML/txt file

I am a developing a macro which will find the EN text and replace with FR text in HTML file. I have written below code. I want to replace "asthma tamed" with "l'asthmesous contrôle" but in HTML file it is showing output like this "l�asthmesous contr�le".

Note:- I cannot change any system settings.

Sub replacetext()

    Const ForReading = 1
    Const ForWriting = 2
    Dim strFileName, strOldText, RstrOldText, strNewText As String
    
    strFileName = ##### HTML File Path #####
    strOldText = "asthma tamed"
    MsgBox (strOldText)
    'RstrOldText = RemoveHTML(strOldText)
    'MsgBox (RstrOldText)
    strNewText = "l’asthmesous contrôle"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
    
    strText = objFile.ReadAll
    objFile.Close
    
    strNewText = Replace(strText, strOldText, strNewText)
    Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
    objFile.WriteLine strNewText
    objFile.Close
    
    MsgBox "Text Replaced !!!"
End Sub

Function RemoveHTML(text)
    Dim regexObject As Object
    Set regexObject = CreateObject("vbscript.regexp")

    With regexObject
        .Pattern = "<!*[^<>]*>"
        .Global = True
        .IgnoreCase = True
        .MultiLine = True
        .Format = "Unicode Text"
    End With

    RemoveHTML = regexObject.Replace(text, "")
End Function

have you tried adding \ before each special character? how about using &ocirc; for the circumflex'd o and just ' for the ' or '?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM