黑科技!用脚本将DeepSeek嵌入Word中,5分钟搞定(附保姆级教程)
大家好,我是岳哥。岳哥最近建了几个DeepSeek交流群,大家五花八门的玩法和需求一大堆。
有小伙伴问DeepSeek能不能接入Office办公软件,岳哥研究了一番还真可以。
今天教大家如何用VB宏命令将DeepSeek集成到Word中。
获取API-Key
这一步是我们操作的关键,目前我们可以直接只用官网的API来实现。
登录官网的API平台:
https://platform.deepseek.com/
没注册的注册完登录一下,我们点击到左侧菜单的“APIKeys”按钮,然后点击右侧的“创建API Key”
图片
在弹出的对话框中备注一下,以防忘记,这里我们备注为“Word”,表名是在Word上使用的。
图片
点击创建,这样我们就获得了一个API Key了。记得先将密钥复制存放一下。一旦关闭就无法复制,只能删了重新创建。
图片
这样我们就获得了一个API key。
开启宏命令
要将DeepSeek集成到Word中必须先使用到“宏”,但是Office默认是禁用宏的,所以咱们得先开启一下宏命令的功能。
打开Word文档,点击“文件”——“选项”——“自定义功能”——“信任中心”——“信任中心设置”,勾选如下两个功能,并确定。
图片
这样我们就启用了宏命令功能。
启动开发工具
一般的Word因为禁用了“宏命令”,对应的“开发工具”菜单也没开启,我们也需要开启一下。
按如下操作启动“开发工具”菜单:“文件”——“选项”——“自定义功能”,勾选启动“开发工具”菜单
图片
创建VB宏命令
前面的设置主要是为了方便大家能在菜单栏里找到我们即将设置的VB宏,接下来才是重点内容,跟着我操作。
点击“开发工具”下的VB编辑器
图片
进入后选择菜单中的“插入”-“模块”,命名为“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-reasoner"", ""messages"": [{""role"":""system"", ""content"":""你是一个乐于助人的AI助手,请根据用户的问题给出详细的解答。""}, {""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 = .responseTextEnd With' 弹出窗口显示 API 响应(调试用)' MsgBox "API Response: " & response, vbInformation, "Debug Info"If status_code = 200 Then CallDeepSeekAPI = responseElse CallDeepSeekAPI = "Error: " & status_code & " - " & responseEnd IfSet Http = NothingEnd FunctionSub DeepSeekR1()Dim api_key As StringDim inputText As StringDim response As StringDim regex As ObjectDim reasoningRegex As ObjectDim contentRegex As ObjectDim matches As ObjectDim reasoningMatches As ObjectDim originalSelection As ObjectDim reasoningContent As StringDim finalContent As Stringapi_key = "替换为你的api key"If api_key = "" Then MsgBox "Please enter the API key." Exit SubElseIf Selection.TypewdSelectionNormal Then MsgBox "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" Then ' 创建正则表达式对象来分别匹配推理内容和最终回答 Set reasoningRegex = CreateObject("VBScript.RegExp") With reasoningRegex .Global = True .MultiLine = True .IgnoreCase = False .Pattern = """reasoning_content"":""(.*?)""" End With Set contentRegex = CreateObject("VBScript.RegExp") With contentRegex .Global = True .MultiLine = True .IgnoreCase = False .Pattern = """content"":""(.*?)""" End With ' 提取推理内容 Set reasoningMatches = reasoningRegex.Execute(response) If reasoningMatches.Count > 0 Then reasoningContent = reasoningMatches(0).SubMatches(0) reasoningContent = Replace(reasoningContent, "\n\n", vbNewLine) reasoningContent = Replace(reasoningContent, "\n", vbNewLine) reasoningContent = Replace(Replace(reasoningContent, """", Chr(34)), """", Chr(34)) End If ' 提取最终回答 Set matches = contentRegex.Execute(response) If matches.Count > 0 Then finalContent = matches(0).SubMatches(0) finalContent = Replace(finalContent, "\n\n", vbNewLine) finalContent = Replace(finalContent, "\n", vbNewLine) finalContent = Replace(Replace(finalContent, """", Chr(34)), """", Chr(34)) ' 取消选中原始文本 Selection.Collapse Direction:=wdCollapseEnd ' 插入推理过程(如果存在) If Len(reasoningContent) > 0 Then Selection.TypeParagraph Selection.TypeText "推理过程:" Selection.TypeParagraph Selection.TypeText reasoningContent Selection.TypeParagraph Selection.TypeText "最终回答:" Selection.TypeParagraph End If ' 插入最终回答 Selection.TypeText finalContent ' 将光标移回原来选中文本的末尾 originalSelection.Select Else MsgBox "Failed to parse API response.", vbExclamation End IfElse MsgBox response, vbCriticalEnd IfEnd Sub
[*]1.
[*]2.
[*]3.
[*]4.
[*]5.
[*]6.
[*]7.
[*]8.
[*]9.
[*]10.
[*]11.
[*]12.
[*]13.
[*]14.
[*]15.
[*]16.
[*]17.
[*]18.
[*]19.
[*]20.
[*]21.
[*]22.
[*]23.
[*]24.
[*]25.
[*]26.
[*]27.
[*]28.
[*]29.
[*]30.
[*]31.
[*]32.
[*]33.
[*]34.
[*]35.
[*]36.
[*]37.
[*]38.
[*]39.
[*]40.
[*]41.
[*]42.
[*]43.
[*]44.
[*]45.
[*]46.
[*]47.
[*]48.
[*]49.
[*]50.
[*]51.
[*]52.
[*]53.
[*]54.
[*]55.
[*]56.
[*]57.
[*]58.
[*]59.
[*]60.
[*]61.
[*]62.
[*]63.
[*]64.
[*]65.
[*]66.
[*]67.
[*]68.
[*]69.
[*]70.
[*]71.
[*]72.
[*]73.
[*]74.
[*]75.
[*]76.
[*]77.
[*]78.
[*]79.
[*]80.
[*]81.
[*]82.
[*]83.
[*]84.
[*]85.
[*]86.
[*]87.
[*]88.
[*]89.
[*]90.
[*]91.
[*]92.
[*]93.
[*]94.
[*]95.
[*]96.
[*]97.
[*]98.
[*]99.
[*]100.
[*]101.
[*]102.
[*]103.
[*]104.
[*]105.
[*]106.
[*]107.
[*]108.
[*]109.
[*]110.
[*]111.
[*]112.
[*]113.
[*]114.
[*]115.
[*]116.
[*]117.
[*]118.
[*]119.
并将其中需要填充API-Key的地方替换成我们前面准备好的API-Key。
图片
保存VB脚本并关闭窗口,再点击“文件”-“选项”-“自定义功能区”,我们选择这里的宏,就可以看到我们刚才创建的宏命令了。
图片
新建宏命令组
我们在右侧继续右键“开发工具”,在它下面新建一个组。
图片
然后将这个组重命名为“AI”或者你喜欢的名称,并且选一个你喜欢的图标。
图片
将上面的宏添加到这个下面即可。
图片
点击“确定”,我们就可以在菜单栏的“开发工具”中看到这个按钮了。
图片
我们在下面的Word文档中测试一下这个功能,在Word里输入一段文字,点击这个按钮。
等待一会儿后儿(DeepSeek R1在推理),就会出现DeepSeek的思考过程和最终结果。
图片
这样之后我们要写什么内容,要检查错别字,扩写,翻译等等需求都可以直接在Word里完成。
另存为模板
上面的操作只是针对当前这个Word文档,如果要一劳永逸,每次打开都有这个脚本存在,我们只需要将这个Word文档另存为模板。
图片
这样下次打开的时候,文档还是会附带这个宏命令。
页:
[1]