|
楼主 |
发表于 2019-2-25 14:12
|
显示全部楼层
本帖最后由 dengni 于 2019-2-27 09:55 编辑
企业微信用户的数据库表一直在变更,前面的代码已经用不成了。下面的是重新写了代码,亲测使用正常。
功能是将微信用户数据写入自己的数据库
数据库表各字段名见蓝色字符
公众号请变更Url的连接
Sub GetWxUers()
'获取微信人员
Dim http
Dim MyArr, BtArr
Dim i1 As Integer, i2 As Integer, Str1 As String, Str2 As String, Str3 As String
Set http = CreateObject("MSXML2.ServerXMLHTTP")'token(CorpID, SecretStr0) token函数见一楼
Url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=" & token(CorpID, SecretStr0) & "&department_id=1&fetch_child=1&status=0"
http.Open "Get", Url, False
http.send ""
If http.status = 200 Then
Str1 = http.responseText
End If
'排除
Str1 = Replace(Str1, """", "")
sql = ""
MyArr = Split(Str1, "},{")
'表字段 自己数据库中表的字段要和企业微信中的字段相同
BtArr = Array("userid", "name", "department", "position", "mobile", "gender", "email", "avatar", "status", "enable", "isleader", "english_name", "hide_mobile", "telephone")
For i1 = 0 To UBound(BtArr)
Str2 = Str2 & BtArr(i1) & ","
Next i1
Str2 = Left(Str2, Len(Str2) - 1)
For i = 0 To UBound(MyArr)
For i1 = 0 To UBound(BtArr)
Select Case BtArr(i1)
Case "department"
Str1 = Replace(Split(Split(MyArr(i), BtArr(i1) & ":")(1), "]")(0), "[", "")
Case Else
Str1 = Split(Split(MyArr(i), BtArr(i1) & ":")(1), ",")(0)
End Select
Str3 = Str3 & "'" & Str1 & "',"
Next i1
Str3 = Left(Str3, Len(Str3) - 1)
sql = sql & " INSERT into [users] (" & Str2 & ") Values (" & Str3 & ");"
Str3 = ""
Next i
Set ConnWy = Conn("SQL连接")ConnWy.Execute ("delete from [users] ")
ConnWy.Execute (sql)
ConnWy.Close
End Sub
Sub GetWxdepart()
'获取微信部门
Dim http
Dim MyArr, BtArr
Dim i1 As Integer, Str1 As String
Set http = CreateObject("MSXML2.ServerXMLHTTP")
'token(CorpID, SecretStr0) token函数见一楼
Url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=" & token(CorpID, SecretStr0)
'
http.Open "Get", Url, False
http.send ""
If http.status = 200 Then
Str1 = http.responseText
End If
'排除
Str1 = Replace(Str1, """", "")
sql = ""
MyArr = Split(Str1, "},{")
Str1 = ""
'表字段 自己数据表中设字段和企业微信中相同
BtArr = Array("id", "name", "parentid", "order")
For i = 0 To UBound(MyArr)
For i1 = 0 To UBound(BtArr)
Str1 = Str1 & "'" & Split(Split(MyArr(i), BtArr(i1) & ":")(1), ",")(0) & "',"
Next i1
Str1 = Left(Str1, Len(Str1) - 1)
sql = sql & " INSERT into [departments] (id,name,parentid,[order]) " & " Values (" & Str1 & ");"
Str1 = ""
Next i
Set ConnWy = Conn("SQL连接")ConnWy.Execute ("delete from [departments] ")
ConnWy.Execute (sql)
ConnWy.Close
MsgBox "更新成功"
End Sub
|
|