1.打开”Word”,点击“文件”。
2.点击“选项”。
3.点击“信任中心”——“信任中心设置”。
4. 勾选”启用所有宏“,点击”确定“。
5.点击“自定义功能区”,勾选上“开发工具”,点击“确定”。
6.返回“文件——开发工具“下的”Visual Basic"。
7.点击“插入”,选择“模块”。
8.打开硅基流动官网(SiliconFlow, Accelerate AGI to Benefit Humanity)
9.选择“API密钥”。
10.点击右上角:“新建API密钥”。
11.随便输入一个名字,新建密钥。
12.复制刚建立的API密钥。
13.替换以下代码中:api_key="" 中的内容。
Function CallDeepSeekAPI(api_key As String, inputText As String) As String
Dim API As String
Dim SendTxt As String
Dim Http As Object
Dim status_code As Integer
Dim response As String
API = "https://api.siliconflow.cn/v1/chat/completions"
SendTxt = "{""model"": ""deepseek-ai/DeepSeek-V3"", ""messages"": [{""role"":""system"", ""content"":""You are a Word assistant""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": false}"
'想用R1模型,就把上面的model的deepseek-ai/DeepSeek-V3换成deepseek-ai/DeepSeek-R1
Set Http = CreateObject("MSXML2.XMLHTTP")
With Http
.Open "POST", API, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "Bearer " & api_key
.send SendTxt
status_code = .Status
response = .responseText
End With
If status_code = 200 Then
CallDeepSeekAPI = response
Else
CallDeepSeekAPI = "Error: " & status_code & " - " & response
End If
Set Http = Nothing
End Function
Sub DeepSeekV3()
Dim api_key As String
Dim inputText As String
Dim response As String
Dim regex As Object
Dim matches As Object
Dim originalSelection As Range
' API Key
api_key = "sk-esykfihseaarfitflbmcxmhxlevppfiublbnlpmlaeywdnaf"
If api_key = "" Then
MsgBox "Please enter the API key.", vbExclamation
Exit Sub
End If
' 检查是否有选中文本
If Selection.Type <> wdSelectionNormal Then
MsgBox "Please select text.", vbExclamation
Exit Sub
End If
' 保存原始选区
Set originalSelection = Selection.Range.Duplicate
' 处理特殊字符
inputText = Selection.Text
inputText = Replace(inputText, "\", "\\")
inputText = Replace(inputText, vbCrLf, " ")
inputText = Replace(inputText, vbCr, " ")
inputText = Replace(inputText, vbLf, " ")
inputText = Replace(inputText, """", "\""") ' 转义双引号
' 发送 API 请求
response = CallDeepSeekAPI(api_key, inputText)
' 处理 API 响应
If Left(response, 5) <> "Error" Then
' 解析 JSON
Set regex = CreateObject("VBScript.RegExp")
With regex
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = """content"":""(.*?)""" ' 匹配 JSON 的 "content" 字段
End With
Set matches = regex.Execute(response)
If matches.Count > 0 Then
' 提取 API 响应的文本内容
response = matches(0).SubMatches(0)
' 处理转义字符
response = Replace(response, "\n", vbCrLf)
response = Replace(response, "\\", "\") ' 处理 JSON 里的反斜杠
response = Replace(response, "&", "") ' 过滤 `&`,防止意外符号
' 让光标移动到文档末尾,防止覆盖已有内容
Selection.Collapse Direction:=wdCollapseEnd
Selection.TypeParagraph
Selection.TypeText Text:=response
' 将光标移回原来选中文本的末尾
originalSelection.Select
Else
MsgBox "Failed to parse API response.", vbExclamation
End If
Else
MsgBox response, vbCritical
End If
End Sub
14.粘贴以上代码到“模块1”中。
15.再次回到选项,点击自定义功能区。
16.在“开发工具”下,点击新建组。“命令”下拉选择“宏”。
17.把我们刚才写的模块,拖拽到右侧。
18.点击“确定”。
19.这样我们就可以在Word里使用"DeepSeek"。选中指令(如:五行有哪5个?),点击上侧“DeepSeekV3”模块。
20.等待一段时间,就出答案了。
注:在Word里反应比较慢。有时候问复杂问题还会报:Error: 504-{"code":50501,"message":"Model service timeoutPlease try again later.","data":null}错误(服务器繁忙,请稍后再试)。
大家可以去DeepSeek官网(DeepSeek | 深度求索),申请自己的API后,充钱。
21.申请API的意义
有道、WPS等软件(这些大公司也付费了或企业间合作)已经免费集成了DeepSeek,在这些软件里面我们可以直接使用。
但如果我们开发自己的软件、可视化平台、物联网等,需要引用DeepSeek,就需要自己的API。