首页 > 解决方案 > WScript.Shell 以 UTF8 读取输出

问题描述

我正在尝试从 WScript.Shells Exec 返回值读取输出。有些行包含中文/德语/西里尔字符,如下所示:

Glas für Rahmen

问题是,我在 VBA 脚本中收到了“错误”的字符。

我可以更改我的字符集StdOut吗?我通过 WScript.Shell 启动的可执行文件的开发人员确保输出为 UTF8。如果我在 CMD 中启动可执行文件,我可以看到正确的字符。

Private oShell As Variant

Private Sub Class_Initialize()
    Set oShell = CreateObject("WScript.Shell")
End Sub

Public Function StartExeAndGetOutput(executable As String, arguments As String) As String
    Dim returnValue As Object

    Set returnValue = oShell.Exec(executable & " " & arguments)

    Dim oOutput As Object
    Set oOutput = returnValue.StdOut

    Dim s As String
    Dim sLine As String
    While Not oOutput.AtEndOfStream
        sLine = oOutput.ReadLine
        If sLine <> "" Then s = s & sLine & vbCrLf
    Wend

    StartExeAndGetOutput = s
End Function

标签: excelvbacharacter-encoding

解决方案


不幸的是你不能,但你可以理解什么是字符集,你可以为 UTF-8 “转换”或“转换”,标准输出是什么?


推荐阅读