【Word内置DeepSeek大模型】
一 、效果演示
以Word为例,PPT与EXCEL同样适用。
选中文本即可实现模型回复,例如,在Word文档中提问写作文的技巧。
二 、各大AI模型 API Key 获取地址
- Qwen2.5 模型:获取 API Key 👉 阿里云百炼平台
- DeepSeek V3 模型:获取 API Key 👉 DeepSeek 官方平台
- ChatGPT(国内加速):获取 API Key 👉 ChatAnywhere
⚡ 点击对应链接,快速申请 API Key,畅享 AI 强大算力!
三 、配置Word
-
启用开发者工具
-
新建Word,点击
文件
->选项
->自定义功能区
-
勾选"开发者工具"
-
-
配置信任中心
- 点击
信任中心
->信任中心设置
- 点击
- 选择"启用所有宏"与"信任对VBA…"
-
添加模块
-
点击开发者工具,点击
Visual Basic
-
在新窗口中点击
插入
,选择模块
-
将代码复制到编辑区中(注意替换你自己的API key)
-
-
自定义功能区
-
点击
文件
->选项
->自定义功能区
-
右键开发工具,点击
添加新组
-
- 右键新建组,点击
重命名
- 右键新建组,点击
-
将其命名为"DeepSeek",选择心仪的图标
-
点击确定
-
-
添加命令
-
选择DeepSeek(自定义)
-
在左侧命令中选择"宏"
-
找到并选中"DeepSeekV3",点击添加
-
右键添加的命令,点击重命名
-
选择开始符号作为图标
-
重命名为"DeepSeekV3"
-
四、 使用方法
-
选中需要处理的文字
-
点击"DeepSeekV3"按钮
-
等待大模型响应
五、 创建模板
- 将文档另存为 Word 模板 (.dotm):
-
点击"文件" → “另存为”
-
选择保存类型为"Word 启用宏的模板 (.dotm)"
-
保存到 Word 的模板文件夹(通常是 C:\Users\用户名\AppData\Roaming\Microsoft\Word\STARTUP)
-
这样每次打开 Word 时,宏就会自动可用
-
六 、相关代码
主要介绍DeepSeekV3、deepseek-reasoner、qwen2.5-max、gpt-4o-mini这四种大模型嵌入到word的代码。
1 DeepSeekV3代码
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 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 =</