这里写目录标题
DeepSeek 是超强大语言模型,接入 Word 后相当于为文档配备了一个智能高效助手,创作效率翻倍,质量精进,查找资料、优化表达轻松搞定。
一、ApiKey获取
官方ApiKey获取方法参考文章《官方ApiKey获取》;白嫖ApiKey获取方式《白嫖DeepSeek全量模型》。
二、配置Word
-
新建一个word,点击左上角的“文件”。
-
选择侧边菜单栏的“选项”。
-
勾选“自定义功能区”的“开发工具”。
-
选择“信任中心”->“信任中心设置”, 选择“宏设置”,勾选“启动所有宏”以及“信任对VBA工程对象模型的访问”。
-
点击开发工具的Visual Basic。
-
点击插入,选择模块。
-
在模块里粘贴代码,记得将自己的API Keys复制,替换:
FunctionCallDeepSeekAPI(api_keyAsString, inputTextAsString)
Dim API As String
Dim SendTxt As String
Dim Http As Object
Dim status_code As Integer
Dim response As String
API = "https://api.siliconflow.cn/v1/chat/completions"
SendTxt = "{""model"": ""deepseek-ai/DeepSeek-R1"", ""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 = "你自己的ApiKey"
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))
' 处理换行符和制表符
response = Replace(response, "\n", vbCr)
response = Replace(response, "\t", Chr(9))
' 取消选中原始文本
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
- 关闭代码区,进入第三步的“开发工具”,添加新组,并右键新组重命名。
- 选择刚刚的新组->选择宏->选择刚刚创建的模块,添加到新组里面,并右键重命名,此时已经配置完成,我们可以重命名为“智能写作”。
- 此时已经配置完成,选中你的问题,点击智能写作,即可将DeepSeek输出的内容写到你的word里。
文末福利
关注我的公众号,更多资料可免费获取。