目录
一、问题描述
二、解决方法
三、代码
四、注意事项
五、总结
一、问题描述
如何在Word里面嵌入DeepSeek?
二、解决方法
1、新建文档,按 Alt+F11,进入VB界面。
2、选中文档,右键->插入->模块。
3、进入模块,粘入VBA代码。(代码见下文)
4、保存为带宏的word文档。
5、回到文档,打开文件->选项->信任中心->宏设置->启用所有宏。
不同版本word可能界面有所不同,但大同小异,启用宏即可。
6、(1)在左侧选择宏,显示已保存的程序模块;
(2)在右侧,主选项卡->开发工具->新建组deepseek;
(3)左侧选中模块1,右侧选中新建的组deepseek,点击中间的 添加,确定。
7、回到word界面,开发工具的选项卡下多了一个DeepSeek模块
8、 测试程序运行情况,看一下效果(响应时间稍微有点长)
三、代码
代码来源(如有侵权,请联系博主删除):如何在wps中加载deepseek?(文章后附VBA代码)_wps接入deepseek的vba代码-CSDN博客
DeepSeek嵌入Word之VBA | 编程语言前沿技术分享
Word接入DeepSeek - 王鹏鑫 - 博客园
Function CallDeepSeekAPI(api_key As String, inputText As String) As StringDim API As StringDim SendTxt As StringDim Http As ObjectDim status_code As IntegerDim response As StringAPI = "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 SendTxtstatus_code = .Statusresponse = .responseTextEnd With' 弹出窗口显示 API 响应(调试用)' MsgBox "API Response: " & response, vbInformation, "Debug Info"If status_code = 200 ThenCallDeepSeekAPI = responseElseCallDeepSeekAPI = "Error: " & status_code & " - " & responseEnd IfSet Http = Nothing
End FunctionSub DeepSeekV3()Dim api_key As StringDim inputText As StringDim response As StringDim regex As ObjectDim matches As ObjectDim originalSelection As Objectapi_key = "输入你的deepseek api-key"If api_key = "" ThenMsgBox "Please enter the API key."Exit SubElseIf Selection.Type <> wdSelectionNormal ThenMsgBox "Please select text."Exit SubEnd If' 保存原始选中的文本Set originalSelection = Selection.Range.DuplicateinputText = Replace(Replace(Replace(Replace(Replace(Selection.Text, "\", "\\"), vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), "\""")response = CallDeepSeekAPI(api_key, inputText)If Left(response, 5) <> "Error" ThenSet regex = CreateObject("VBScript.RegExp")With regex.Global = True.MultiLine = True.IgnoreCase = False.Pattern = """content"":""(.*?)"""End WithSet matches = regex.Execute(response)If matches.Count > 0 Thenresponse = matches(0).SubMatches(0)response = Replace(Replace(response, """", Chr(34)), """", Chr(34))' 取消选中原始文本Selection.Collapse Direction:=wdCollapseEnd' 将内容插入到选中文字的下一行Selection.TypeParagraph ' 插入新行Selection.TypeText Text:=response' 将光标移回原来选中文本的末尾originalSelection.SelectElseMsgBox "Failed to parse API response.", vbExclamationEnd IfElseMsgBox response, vbCriticalEnd If
End Sub
四、注意事项
1、API地址与密钥
API 地址,https://api.deepseek.com/chat/completions
密钥,deepseek官网,创建,初次注册可以免费体验几次
2、返回报错与解决方法
参考官网报错代码:错误码 | DeepSeek API Docs
3、使用硅基流动网址与密钥
网址:https://api.siliconflow.cn/v1/chat/completions
密钥:官网注册创建,硅基流动统一登录
(注册邀请码:ogasTAfR )
SiliconFlow, Accelerate AGI to Benefit Humanity
模型路径:deepseek-ai/DeepSeek-V3
4、代码优化
直接第三步骤的代码,运行的结果,回车符\n会保留在文档中,如下图所示,
返回VBA代码,在过程Sub DeepSeekV3()中,在response 赋值语句下方添加一行代码:
'把\n换成换行符
response = Replace(response, "\n", vbCrLf)
其中,vbCrLf或Chr(10) 表示换行。
五、总结
在Word里面嵌入使用deepseek工具,可以通过VB程序来访问API地址来获取内容,优势是减少从网站里Ctrl+C和Ctrl+V 的操作,简化了操作步骤,实现文档内容一步到位。劣势是响应时间较长。
日期:2025年02月14日