优秀的编程知识分享平台

网站首页 > 技术文章 正文

Word接入DeepSeek方法:第二版解决宏消失保存失败。

nanyue 2025-05-25 14:33:26 技术文章 3 ℃

前言

DeepSeek+Word 体验智能办公时代,实现了AI技术与文档处理的深度融合。通过智能写作辅助、内容生成、语法纠错等功能,大幅提升文档创作效率和质量,为用户带来更高效、智能的办公体验,开启文档处理的智慧新时代。

Word接入DeepSeek使用效果图示:


下面是Word接入DeepSeek方法。

1、获取API

1.1 打开DeepSeek官网:

路径:https://www.deepseek.com/

1.2 创建API

路径:官网 -> API开发平台 -> 注册用户 -> 登录 -> API keys -> 创建API key




2、Word配置DeepSeek

2.1 Word文档,打开 文件 -> Word选项 -> 信任中心 -> 信任中心设置

宏设置:选择 ”启用所有宏“

开发人员宏设置:勾选 ”信任对 VBA 工程对象模型的访问“


2.2 Word文档,打开 文件 -> 选项 -> 自定义功能区,勾选”开发者工具“


2.3 Word文档,配置Visual Basic,打开菜单:开发者工具 -> Visual Basic -> 插入


2.4 配置Visual Basic界面,点击弹出编辑器,把下面代码复制到编辑区中。注意不要忘记替换成第1.2 中创建的你自己的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.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!!!!!】"

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

' 步骤1:保存原始选中文本

Set originalSelection = Selection.Range.Duplicate

inputText = Replace(Replace(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"":\s*""([\s\S]*?)"""

End With

Set matches = regex.Execute(response)

If matches.Count > 0 Then

response = matches(0).SubMatches(0)

' 步骤2:处理Unicode转义字符(如\u003c -> <)

response = Replace(response, "\u003c", "<")

response = Replace(response, "\u003e", ">")

' 步骤3:删除标签及其内容

With regex

.Global = True

.MultiLine = True

.IgnoreCase = True

.Pattern = "[\s\S]*?"

End With

response = regex.Replace(response, "")

' 步骤4:转换\n为实际换行符

response = Replace(response, "\n", vbCrLf)

' 步骤5:移除Markdown格式

With regex

.Global = True

.Pattern = "(#+\s*|\*\*|__||\*{1,2}|_{1,2}|~~|^>\s)"

response = .Replace(response, "")

End With

response = regex.Replace(response, "")

' 步骤6:取消选中原始文本

Selection.Collapse Direction:=wdCollapseEnd

' 步骤7:将内容插入到选中文字的下一行

Selection.TypeParagraph

' 步骤8:插入新行

Selection.TypeText Text:=response

' 步骤9:将光标移回原来选中文本的末尾

originalSelection.Select

Else

MsgBox "Failed to parse API response.", vbExclamation

End If

Else

MsgBox response, vbCritical

End If

End Sub

完成后,可直接关闭弹窗。

2.5 Word文档,文件 -> Word选项 -> 自定义功能区 -> 右键 -> 开发工具 -> 添加新组。


2.6 选中添加的新建组 -> 点击右键 -> 点击重命名。命名成:DeepSeek,后选择图标,点击”确定“。


2.7 选中DeepSeek(自定义)-> ”从下列位置选择命令 -> “为“宏”,找到我们添加的DeepSeekV3 -> 选中后点击添加。


2.8 选中添加的命令 -> 右键点击重命名 -> 选择开始符号作为图标 -> 重命名为“生成” -> 确定。


3、宏异常消失设置

3.1 点击 开发工具 -> -> 选中DeepSeekV3 -> 点击管理器 -> 选中左侧”模块1“ -> 点击复制 -> 右侧”模块1“ -> 点击关闭。

配置完之后重启word配置的宏异常消失,解决办法如下:



重启Word后,宏消失问题解决完成。

至此,Word DeepSeek模型成功接入,设置完毕。

4、Word DeepSeek使用。

4.1 使用方法:

Word输入问题并选中文字,点击”DeepSeek 生成”键,就发送给DeepSeek AI大模型,大模型将做出应答,并将DeepSeek应答内容自动生成到Word。


全文结束。

Tags:

最近发表
标签列表