前几天给大家分享了 DeepSeek 的资源包,可能很多人并没有本地部署 DeepSeek 的需求,只想使用它来提高一下工作效率。那今天来分享一下怎么直接在 Word 软件调用 DeepSeek,避免在 Word 软件和网页版 DeepSeek 里来回切换。
## 前置条件
1、有外网,通过 API 调用 DeepSeek。
2、Office 软件中的 Word,笔者是 Office 365。
3、有 DeepSeek 的 API key,这个可以免费在官网申请。官网地址:https://www.deepseek.com
在官网右上角有个“API 开发平台”的按钮,点进去再选左侧的 “API keys” 按提示就可以免费注册 API key 了。
很不幸的是,由于最近泼天的流量,官网已经暂停充值,连新用户的 10 元赠送余额也没有了。主要还是因为最近访问量太大了,等着官网再次开放。
创建的 API key 一定要记在记事本上保存好,关闭后就没法再次查看,只能再申请新的 API key。
## 在 Word 里配置 DeepSeek
1、打开 Word 软件,打开文件 -> 选项 -> 自定义功能区,勾选“开发者工具”,然后点确定。
2、打开文件 -> 选项->信任中心 ->信任中心设置,选择“启用所有宏”与“信任对 VBA......”,然后确定。
3、可以发现word选项卡中出现了一个“开发者工具”,点击开发者工具,然后点击左上角的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"":""You are a Word assistant""}, {""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
' 弹出窗口显示 API 响应(调试用)
' MsgBox "API Response: " & response, vbInformation, "Debug Info"
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 Object
api_key = "自己的API key"
If api_key = "" Then
MsgBox "Please enter the API key."
Exit Sub
ElseIf Selection.Type <> wdSelectionNormal Then
MsgBox "Please select text."
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))
' 取消选中原始文本
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
代码中的 Api_key = "自己的 API key",这里面把申请到的 API key 替换进去。
4、点击文件 -> 选项 -> 自定义功能区,选中开发工具点击右键,弹出的菜单里点击添加新组。然后把新组重命名为 DeepSeek。
自定义功能区->宏,找到 DeepSeekV3,添加到上面新建的DeepSeek 分组,然后把添加过去的宏改名为“开始对话”。如下图显示:
## 测试效果
完成上面的配置,在菜单里的开发工具里,会多一个“开始对话”的按钮,接下来就可以测试效果了。
打开文档,选中需要对话的内容,比如“请解释一下最近 DeepSeek 爆火的原因”,点开始对话。
哈哈,报错啦!提示余额不足。上面说过,DeepSeek 临时关了充值,赠送的 10 元也没有给我,等有钱了再用吧。
至于 WPS,大家可以自己动手去试试,应该大同小异。好了,今天的分享就到这里。