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

时间:2009-08-31 06:43:30

标签: vba ms-word

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

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

P.S。我从今天早上才开始使用VBA =)如此简单的答案表示赞赏

1 个答案:

答案 0 :(得分:10)

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