说明
1. **获取企业微信凭据**:
- 登录到企业微信管理后台。
- 获取自建应用的AgentId(应用ID)和CorpSecret(应用的密钥)。
- 获取企业的CorpId(企业ID)。
2. **配置Excel VBA环境**:
- 打开Excel,按下 `Alt + F11` 打开VBA编辑器。
- 插入一个新的模块(在菜单中选择"插入" -> "模块")。
- 将下面提供的VBA代码复制粘贴到新模块中。
3. **修改代码中的占位符**:
- 将代码中的 `YOUR_AGENT_ID`、`YOUR_CORP_SECRET` 和 `YOUR_CORP_ID` 替换为您的实际企业微信应用的AgentId、CorpSecret和企业的CorpId。
- 将 `USERID` 替换为您想要接收消息的企业微信用户ID或部门ID。
注意,由于Excel VBA不支持一些.NET功能,我们需要进行一些调整。以下是可以在Excel VBA中运行的代码: 1. 首先,您需要在Excel VBA中添加引用: - 打开Excel,按 `Alt + F11` 打开VBA编辑器。 - 在菜单栏选择“工具” > “引用”。 - 在引用列表中勾选 “Microsoft XML, v6.0” 和 “Microsoft Internet Controls”。 - 点击“确定”关闭对话框。
2024-6-21 更新:由于企业微信修改规则,只有2022年1月04日之前创建的自建应用才可以使用此方式。经过摸爬滚打发现了这一条,我说怎么我用同样的代码,换新建的应用就不行了。
这里有规则说明:
企业微信 (自建应用) 如何授权? | 集简云连接数百款软件无需API接口开发企业微信 (自建应用) 如何授权?, 企业微信 (自建应用) 如何授权?文档,企业微信 (自建应用) 如何授权?帮助说明,企业微信 (自建应用) 如何授权?API接口文档,企业微信 (自建应用) 如何授权?开发,企业微信 (自建应用) 如何授权?开发文档
https://www.jijyun.cn/help/detail/9
-------------------------------------以上不要复制------------------
经过修改我给做成函数了,使用sub过程需要在写一些vba代码单元格读取什么循环什么的太麻烦
简化了函数名使用公式实现发送消息,然后返回结果更符合表格的基本使用习惯
希望给大家带来方便,具体使用方法如图。
资源不用下载,复制下面的代码到vba编辑器中就行了!!!全程使用WPS。

2024-6-19 更新一下:

这里需要设置一下 否则会在单元格提示 : #NAME?
如果是提示 #VALUE! 可能是你没有安装好 vba7.0 环境vba for wps office7.0下载_vba for wps office7.0官方免费下载_2024最新版_华军软件园
2024-6-19 更新end
Function SendWX(ToUser As String, Message As String) As String
' 企业微信应用的ID和密钥,替换为您的实际信息
Const AgentId As String = "YOUR_AGENT_ID"
Const CorpSecret As String = "YOUR_CORP_SECRET"
Const CorpId As String = "YOUR_CORP_ID"
Dim result As String
' 接收消息的用户ID,可以是单个用户或者部门
'Const ToUser As String = "sunyu"
' 消息内容
'Dim Message As String
'Message = "这是一条测试消息。"
' 获取access_token
Dim AccessToken As String
AccessToken = GetAccessToken(CorpId, CorpSecret)
' 发送消息
If AccessToken <> "" Then
result = SendMessage(AccessToken, AgentId, ToUser, Message)
SendWX = result
Else
MsgBox "获取access_token失败"
End If
End Function
Function GetAccessToken(CorpId As String, CorpSecret As String) As String
Dim url As String
url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" & CorpId & "&corpsecret=" & CorpSecret
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", url, False
.Send
If .Status = 200 Then
Dim response As Object
Set response = ParseJson(.responseText)
' Assuming the response is something like: {"access_token":"your_access_token","expires_in":7200}
GetAccessToken = response("access_token")
Else
GetAccessToken = ""
End If
End With
End Function
Function SendMessage(AccessToken As String, AgentId As String, ToUser As String, Message As String) As String
Dim url As String
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" & AccessToken
Dim PostData As String
PostData = "{""touser"":""" & ToUser & """,""msgtype"":""text"",""agentid"":" & AgentId & ",""text"":{""content"":""" & Message & """},""safe"":0}"
With CreateObject("MSXML2.XMLHTTP")
.Open "POST", url, False
.SetRequestHeader "Content-Type", "application/json"
.Send PostData
If .Status = 200 Then
'MsgBox "消息发送成功"
SendMessage = "成功"
Else
'MsgBox "消息发送失败: " & .responseText
SendMessage = "失败"
End If
End With
End Function
Function ParseJson(jsonString As String) As Object
Dim obj As Object
Set obj = CreateObject("Scripting.Dictionary")
' 移除JSON字符串首尾的大括号
jsonString = Mid(jsonString, 2, Len(jsonString) - 2)
' 分割字符串成为键值对数组
Dim keyValues() As String
keyValues = Split(jsonString, ",")
Dim i As Integer
Dim keyValue As String
Dim key As String
Dim value As String
Dim colonPos As Integer
Dim quotePos1 As Integer
Dim quotePos2 As Integer
' 遍历每个键值对
For i = LBound(keyValues) To UBound(keyValues)
keyValue = Trim(keyValues(i))
' 找到键和值之间的冒号位置
colonPos = InStr(keyValue, ":")
' 提取键(去除多余的引号)
key = Trim(Mid(keyValue, 1, colonPos - 1))
key = Mid(key, 2, Len(key) - 2) ' 移除两侧的双引号
' 提取值
value = Trim(Mid(keyValue, colonPos + 1))
' 检查值是否被双引号包围
quotePos1 = InStr(value, """")
quotePos2 = InStrRev(value, """")
If quotePos1 > 0 And quotePos2 > quotePos1 Then
' 值是一个字符串,移除双引号
value = Mid(value, quotePos1 + 1, quotePos2 - quotePos1 - 1)
Else
' 值是一个数字或其他类型的值,不做改变
value = Replace(value, "}", "")
End If
' 将键值对添加到字典对象中
obj.Add key, value
Next i
Set ParseJson = obj
End Function
-------------------------------------------------------js 版 代码少一点---------------------------------------
function SendWX(toUser, message) {
const agentId = "应用ID";
const corpSecret = "应用的密钥";
const corpId = "企业ID";
let result = "";
// 获取access_token
function getAccessToken(corpId, corpSecret) {
const url = `https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpId}&corpsecret=${corpSecret}`;
let xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.send();
if (xhr.status === 200) {
const response = JSON.parse(xhr.responseText);
return response.access_token;
} else {
return "";
}
}
// 发送消息
function sendMessage(accessToken, agentId, toUser, message) {
const url = `https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${accessToken}`;
const postData = `{"touser":"${toUser}","msgtype":"text","agentid":${agentId},"text":{"content":"${message}"},"safe":0}`;
let xhr = new XMLHttpRequest();
xhr.open("POST", url, false);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(postData);
if (xhr.status === 200) {
//MsgBox "消息发送成功";
result = "成功";
} else {
//MsgBox "消息发送失败: " + xhr.responseText;
result = "失败";
}
}
if (getAccessToken(corpId, corpSecret) !== "") {
sendMessage(getAccessToken(corpId, corpSecret), agentId, toUser, message);
} else {
result = "获取access_token失败";
}
return result;
}

3万+

被折叠的 条评论
为什么被折叠?



