代码之家  ›  专栏  ›  技术社区  ›  Peter Elias

VB中文字符串(&C)

  •  0
  • Peter Elias  · 技术社区  · 10 年前

    我正在尝试使用VB6从网站捕获中文文本

    简单的代码如下所示,适用于英语网站

        Private Function RequestText(sURL, Optional sMethod = "POST")
        'You may have caching issues using GET
        Dim XMLHTTP
        Set XMLHTTP = CreateObject("microsoft.XMLHTTP")
        sMethod = UCase(sMethod)
        XMLHTTP.Open sMethod, sURL, False
        XMLHTTP.send (Null) '"x=x"
    
        RequestText = XMLHTTP.responseText
        Set XMLHTTP = Nothing
    End Function
    
    Private Sub cmdText_Click()
        Dim html as string
        html = RequestText("http://url")
        Clipboard.Clear
        Clipboard.SetText html
        MsgBox "Done"
    End Sub
    

    当试图将文本粘贴到word、记事本或db时,中文字符显示为???? 对此有什么解决方案吗?

    1 回复  |  直到 10 年前
        1
  •  1
  •   Serenity    10 年前

    VB6在调用API调用时是ANSI,因为WIN95不支持unicode。在COM中,内部是Unicode。因此,剪贴板所需的任何API调用都会将TEXT转换为ANSI。因此,不要使用TEXT。XMLHTTP有多种格式可供选择。

    Internet Explorer可以访问剪贴板。用它来做。

    此示例代码读取剪贴板。注意,您必须导航到任何本地文件以避免出现安全提示。

    Set Arg = WScript.Arguments
    set WshShell = createObject("Wscript.Shell")
    Set Inp = WScript.Stdin
    Set Outp = Wscript.Stdout
    Sub Clip
        Set ie = CreateObject("InternetExplorer.Application") 
        ie.Visible = 0
        ie.Navigate2 FilterPath & "Filter.html"
        Do 
            wscript.sleep 100
        Loop until ie.document.readystate = "complete"  
        txt=ie.document.parentwindow.clipboardData.GetData("TEXT")
        ie.quit
        If IsNull(txt) = true then 
            outp.writeline "No text on clipboard"
        else
            outp.writeline txt
        End If
    End Sub