VBA WS Toolkit,如何将当前文件作为字节数组获取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1356118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:44:55  来源:igfitidea点击:

VBA WS Toolkit, how to get current File as Byte Array

vbams-word

提问by Daveo

Using VBA I want to send a copy of the current word document to a web service? How can is get the current document as a Byte Array?

使用 VBA 我想将当前 word 文档的副本发送到 Web 服务?如何将当前文档作为字节数组获取?

I know how to use the web service just not sure how to get the current file as a binary object to send?

我知道如何使用网络服务只是不确定如何将当前文件作为二进制对象发送?

p.s. I have only been using VBA since this morning =) So simple answers are appreciated

ps 从今天早上开始我就一直在使用 VBA =) 非常感谢简单的答案

回答by Oorang

Public Sub Example()
    Dim bytFile() As Byte
    bytFile = GetFileBytes("c:\test\dirdump.doc")
    ''// Do something with bytFile here.
End Sub

Public Function GetFileBytes(ByVal path As String) As Byte()
    Dim lngFileNum As Long
    Dim bytRtnVal() As Byte
    lngFileNum = FreeFile
    If LenB(Dir(path)) Then ''// Does file exist?
        Open path For Binary Access Read As lngFileNum
        ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
        Get lngFileNum, , bytRtnVal
        Close lngFileNum
    Else
        Err.Raise 53
    End If
    GetFileBytes = bytRtnVal
    Erase bytRtnVal
End Function