ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 6665|回复: 18

[求助] vba 调用企业微信上传临时素材接口并上传文件

[复制链接]

TA的精华主题

TA的得分主题

发表于 2019-5-23 13:44 | 显示全部楼层 |阅读模式
调用接口是通的,就是写POST语句的时候不知道哪里错了Dim Url As String
Const CorpID As String = "ww08cb8a4e65296616"  '企业在企业微信ID
Dim Secret As String
Dim rs1 As String
Const ErrCode As String = """errcode"":0,""errmsg"":""ok"""
Function Token(CorpID As String, Secret As String) As String
Secret = "kx2z9Tw9w3K6jPXYYZT1CuYyfYRzMjoW7HV7qhJN0nI"    '用于发送消息的应用Secret
Dim http
Set http = CreateObject("MSXML2.ServerXMLHTTP")
Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" & CorpID & "&corpsecret=" & Secret & ""
http.Open "get", Url, False 'post get 都可以
http.send ""
If http.Status = 200 Then
Token = http.responsetext
End If
Sheet1.Cells(1, 7) = Token
'MsgBox (Token)

If InStr(Token, "access_token") > 1 Then
Token = Split(Token, ",")(2)

Token = Split(Token, ":")(1)

Token = Replace(Token, """", "")

Sheet1.Cells(2, 7) = Token
Else
Token = ""
End If
End Function

Function upload()
Dim http
cFile = "C:\Users\dengshaoxin\Desktop\ces.txt"
'cfile = "2.jpg"
tokenstr = Token(CorpID, Secret)
Set http = CreateObject("MSXML2.ServerXMLHTTP")
Sheet1.Cells(3, 7) = tokenstr
Url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=" & tokenstr & "&type= image"
http.Open "Post", Url, False
http.setRequestHeader "Content-Type", "multipart/form-data; boundary=-------------------------acebdf13572468"
http.send SendBody(cFile) '发送字串
rs1 = http.responsetext '返回值
If http.Status = 200 Then
rs1 = http.responsetext
End If
MsgBox ("你的返回值" & rs1)
End Function

Function SendBody(ByVal fn As String)
    Dim MyStr As String
    Dim b() As Byte
    Dim StrByte() As Byte
    Dim sLen As Long
     tokenstr = Token(CorpID, Secret)
     MyStr = "POST https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=" & tokenstr & "&type=file HTTP/1.1" & vbCrLf
     MyStr = MyStr & "Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468" & vbCrLf
     MyStr = MyStr & "Content-Length: 220" & vbCrLf
     MyStr = MyStr & vbCrLf
     MyStr = MyStr & "---------------------------acebdf13572468" & vbCrLf
     MyStr = MyStr & "Content-Disposition: form-data; name=""media"";filename=""" & fn & """; filelength=6" & vbCrLf
     MyStr = MyStr & "Content-Type: application/octet-stream" & vbCrLf

    StrByte = StrConv(MyStr, vbFromUnicode) '将之前的字符串转为byte
    b = FileToByte(fn) '将要上传的文件转为byte
'    合并字符串及文件成一个byte
    sLen = UBound(StrByte)
    ReDim Preserve StrByte(sLen + UBound(b) - 1)
    For i = 2 To UBound(b)
        StrByte(sLen + i - 1) = b(i)
    Next

    MyStr = MyStr & vbCrLf
    MyStr = MyStr & "mytext" & vbCrLf
    MyStr = MyStr & "---------------------------acebdf13572468--"
  Sheet1.Cells(5, 7) = MyStr
  'Sheet1.Cells(6, 7) = b
  'Sheet1.Cells(7, 7) = MyStr

    b = StrConv(MyStr, vbFromUnicode)
      MsgBox (b)

    sLen = UBound(StrByte)
    ReDim Preserve StrByte(sLen + UBound(b) + 1)
    For i = 0 To UBound(b)
        StrByte(sLen + i + 1) = b(i)
    Next

    SendBody = StrByte
   'MsgBox (StrByte)
End Function
Function FileToByte(ByVal fn As String)
     With CreateObject("adodb.stream")
        .Open
        .LoadFromFile fn
        .Charset = "GB2312"
        .Type = 1
        FileToByte = .Read
        .Close
    End With
End Function

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-5-23 13:46 | 显示全部楼层
报44001错误

TA的精华主题

TA的得分主题

发表于 2019-5-23 14:35 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
有没有VBA下载对账单的代码?

TA的精华主题

TA的得分主题

发表于 2019-5-23 15:27 来自手机 | 显示全部楼层
浙江绍兴deng 发表于 2019-5-23 13:46
报44001错误

https://blog.csdn.net/xuexiaodong009/article/details/83081911
搜到一片可能是拼接字符串的问题,这个用php比较方便的

TA的精华主题

TA的得分主题

发表于 2019-5-23 15:28 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-5-27 08:23 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-5-27 08:24 | 显示全部楼层
向東 发表于 2019-5-23 14:35
有没有VBA下载对账单的代码?

我没有这样的代码,看看其他人有没

TA的精华主题

TA的得分主题

发表于 2019-5-27 09:53 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-5-29 15:09 | 显示全部楼层
Dim Url As String
Const CorpID As String = "微信的ID"  '企业在企业微信ID
Dim Secret As String
Dim rs1 As String
Const ErrCode As String = """errcode"":0,""errmsg"":""ok"""
Function Token(CorpID As String, Secret As String) As String
Secret = "应用的secret"    '用于发送消息的应用Secret
Dim http
Set http = CreateObject("MSXML2.ServerXMLHTTP")
Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" & CorpID & "&corpsecret=" & Secret & ""
http.Open "get", Url, False 'post get 都可以
http.send ""
If http.Status = 200 Then
Token = http.responsetext
End If

If InStr(Token, "access_token") > 1 Then
Token = Split(Token, ",")(2)

Token = Split(Token, ":")(1)

Token = Replace(Token, """", "")

Else
Token = ""
End If
End Function

Function upload()
Dim http
'cfile = "C:\Users\dengshaoxin\Desktop\ces.txt"
cfile = "C:\Users\dengshaoxin\Desktop\2.jpg"
tokenstr = Token(CorpID, Secret)
Set http = CreateObject("MSXML2.ServerXMLHTTP")
Url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=" & tokenstr & "&type=file"
http.Open "Post", Url, False
http.setRequestHeader "Content-Type", "multipart/form-data; boundary=-------------------------acebdf13572468"
'http.setRequestHeader "Content-Length", "220"
http.send SendBody(cfile) '发送字串
rs1 = http.responsetext '返回值
If http.Status = 200 Then
rs1 = http.responsetext
End If
MsgBox ("你的返回值" & rs1)
Sheet1.Cells(1, 1) = rs1
End Function

Function SendBody(ByVal fn As String)
    Dim MyStr As String
    Dim b() As Byte
    Dim StrByte() As Byte
    Dim sLen As Long
   
     MyStr = "---------------------------acebdf13572468" & vbCrLf
     MyStr = MyStr & "Content-Disposition: form-data; name=""media"";filename=""" & fn & """" & vbCrLf
     MyStr = MyStr & "Content-Type: application/octet-stream" & vbCrLf
     MyStr = MyStr & vbCrLf
     
    StrByte = StrConv(MyStr, vbFromUnicode) '将之前的字符串转为byte
   
    b = FileToByte(fn) '将要上传的文件转为byte
'    合并字符串及文件成一个byte
    sLen = UBound(StrByte)
    ReDim Preserve StrByte(sLen + UBound(b) - 1)
    For i = 2 To UBound(b)
        StrByte(sLen + i - 1) = b(i)
    Next
   
    MyStr = vbCrLf & "---------------------------acebdf13572468--"
    MyStr = MyStr & vbCrLf
   
    b = StrConv(MyStr, vbFromUnicode)
    sLen = UBound(StrByte)
    ReDim Preserve StrByte(sLen + UBound(b) + 1)
    For i = 0 To UBound(b)
        StrByte(sLen + i + 1) = b(i)
    Next
    SendBody = StrByte

End Function
Function FileToByte(ByVal fn As String)
     With CreateObject("adodb.stream")
        .Open
        .LoadFromFile fn
        .Charset = "GB2312"
        .Type = 1
        FileToByte = .Read
        .Close
    End With
End Function

TA的精华主题

TA的得分主题

 楼主| 发表于 2019-5-29 15:09 | 显示全部楼层
我已经搞定了,代码上传中
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-20 19:23 , Processed in 0.037805 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表