将DeepSeek接入Word,不仅提升了文档编辑的效率和体验,还为用户提供了智能化、个性化的创作支持。这种创新整合,将在未来的办公和学习场景中发挥越来越重要的作用。
一、注册deepseek获取API_KEY
登录https://www.deepseek.com/,注册登录后点击左上角"API开放平台";
充值后点击左侧"API keys"
初次DS注册,免费赠送10元人民币的用量。(不知道现在还有没有这个活动)
点击创建API key,填写名称,自动生成key,复制Key备用(下文中要用到)
二、在word中添加deepseek的VBA脚本
打开word,文件—选项–自定义功能区中设置
勾选-开发工具
这样,工具栏中就会显示 开发者工具
在开发者工具中点选Visual Basic
在窗口 Visual Basic中 ,在Normal的模块下右键新增模块,输入代码后保存
在新建的模块中插入一下代码:
Function CallDeepSeekAPI(api_key As String, inputText 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.deepseek.com/chat/completions"
SendTxt = "{""model"": ""deepseek-chat"", ""messages"": [{""role"":""system"", ""content"":""你是word文案助手""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": false}"
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
Function CallDeepSeekRAPI(api_key As String, inputText 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.deepseek.com/chat/completions"
SendTxt = "{""model"": ""deepseek-reasoner"", ""messages"": [{""role"":""system"", ""content"":""你是word文案助手""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": false}"
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
CallDeepSeekRAPI = response
Else
CallDeepSeekRAPI = "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 Object
api_key = "自己的AK"
If api_key = "" Then
MsgBox "Please enter the API key."
Exit Sub
ElseIf Selection.Type <> wdSelectionNormal Then
MsgBox "请选择文本."
Exit Sub
End If
' 保存原始选中的文本
Set originalSelection = Selection.Range.Duplicate
inputText = Replace(Replace(Replace(Replace(Replace(Selection.Text, "\", "\\"), vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), "\""")
response = CallDeepSeekAPI(api_key, inputText)
If Left(response, 5) <> "Error" Then
Set regex = CreateObject("VBScript.RegExp")
With regex
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = """content"":""(.*?)"""
End With
Set matches = regex.Execute(response)
If matches.Count > 0 Then
response = matches(0).SubMatches(0)
response = Replace(Replace(response, """", Chr(34)), """", Chr(34))
response = Replace(response, "\n", vbCrLf)
response = Replace(response, "\n", vbCrLf)
response = Replace(response, "*", "")
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
Sub DeepSeekR()
Dim api_key As String
Dim inputText As String
Dim response As String
Dim regex As Object
Dim matches As Object
Dim originalSelection As Object
api_key = "自己的AK"
If api_key = "" Then
MsgBox "Please enter the API key."
Exit Sub
ElseIf Selection.Type <> wdSelectionNormal Then
MsgBox "请选择文本."
Exit Sub
End If
' 保存原始选中的文本
Set originalSelection = Selection.Range.Duplicate
inputText = Replace(Replace(Replace(Replace(Replace(Selection.Text, "\", "\\"), vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), "\""")
response = CallDeepSeekRAPI(api_key, inputText)
If Left(response, 5) <> "Error" Then
Set regex = CreateObject("VBScript.RegExp")
With regex
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = """content"":""(.*?)"""
End With
Set matches = regex.Execute(response)
If matches.Count > 0 Then
response = matches(0).SubMatches(0)
response = Replace(Replace(response, """", Chr(34)), """", Chr(34))
response = Replace(response, "\n", vbCrLf)
response = Replace(response, "*", "")
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
上述代码中修改两处 自己的DS AK,保存替换代码中的"你的APIKEY"。
三、生成功能配置
打开菜单文件-选项-自定义功能区,如下图进行设置
自定义功缆区和键盘快捷键
从下列位置选摇命
- 选择宏
- 开发工具新增 deepseek分组
- 依次选择两个宏,并添加到右侧
- 选择后重命名
两个模块分别是:
- DeepSeekR(正常对话)
- Deep5eekV3(深度思考)
设置完成后,菜单如图所示:
四、DS菜单生成功能测试
新建一个文档,输入如下内容,选择文字后,点击"对话
努力虽然不一定成功,但一定去尝试…