01 — 申请DeepSeek的API Key 打开DeepSeek官网,点击网站右上角【API开放平台】,进入DeepSeek开放平台; ![]() 点击左侧的【API Keys】, 再点击“创建API Key”。 ![]() 02 — 将DeepSeek装进word 一、在将DeepSeek装进word前,我们首先需要打开打开word的“开发工具”功能。 1、点击【文件】-【选项】,在随后弹出的【word选项】窗口中,选择【自定义功能区】-【开发工具】,勾选后点击【确定按钮】; ![]() 2、随后,在打开的word中,即可出现【开发工具】相关栏目。 ![]() 二、在【信任中心】中【启用所有宏】,并【信任对VBA工程对象模型的访问】。 1、同样是在【word选项】窗口,点击【信任中心】-【信息中心设置…】; ![]() 2、在弹出【信任中心】窗口,点击【宏设置】,勾选【启用所有宏】;在开发人员宏设置中,勾选【信任对VBA工程对象模型的访问】,点击【确定】后返回,即可生效; ![]() 三、编辑DeepSeek模块 1、点击【开发工具】-【Visual Basic】按钮; ![]() 2、在打开的VBA编辑器中,点击【插入】-【模块】; ![]() 3、将给定代码复制到【模块】的编辑窗口,完成后,关闭Visual Basic编辑器; ![]() 具体代码如下: Option Explicit
Sub DeepSeekV3()
Const api_Url As String = 'https://api./chat/completions'
Const api_Key As String = '替换为自己申请的API Key'
Dim inputText As String
Dim response As String
Dim originalSelection As Range
On Error GoTo ErrorHandler
' 检查API密钥内容
If api_Key = '' Then
MsgBox '请输入您的API Key。', vbExclamation
Exit Sub
End If
' 检查是否选中文本
If Selection.Type <> wdSelectionNormal Then
MsgBox '请选中要分析的文本。', vbExclamation
Exit Sub
End If
' 保存原始选中的文本
Set originalSelection = Selection.Range.Duplicate
' 处理选中的文本
inputText = ReplaceSpecialCharacters(Selection.text)
' 调用Deepseek API
response = CallDeepseekAPI(api_Key, api_Url, inputText)
' 处理API响应
If IsJsonResponse(response) Then
response = FormatJson(response)
InsertResponseToDocument response
MsgBox 'API分析内容已插入文档!', vbInformation
Else
MsgBox response, vbCritical
End If
Exit Sub
ErrorHandler:
MsgBox '发生错误:' & Err.Description, vbCritical
End Sub
' 将API响应插入文档
Sub InsertResponseToDocument(response As String)
With ActiveDocument.Content
.InsertAfter vbCrLf & '分析内容:' & vbCrLf & response
End With
End Sub
' 调用DeepseekAPI函数
Function CallDeepseekAPI(api_Key As String, api_Url As String, inputText As String) As String
Dim http As Object
Dim requestBody As String
Dim response As String
Dim status_Code As Integer
On Error GoTo ErrorHandler
' 构造请求体
requestBody = BuildRequestBody(inputText)
' 创建HTTP对象
Set http = CreateObject('MSXML2.XMLHTTP')
' 发送请求
With http
.Open 'POST', api_Url, False
.setRequestHeader 'Content-Type', 'application/json'
.setRequestHeader 'Authorization', 'Bearer ' & api_Key
.send requestBody
status_Code = .Status
response = .responseText
End With
' 检查HTTP状态码
If status_Code = 200 Then
CallDeepseekAPI = response
Else
CallDeepseekAPI = 'API 请求失败,状态码:' & status_Code & '-' & response
End If
ExitFunction:
Set http = Nothing
Exit Function
ErrorHandler:
CallDeepseekAPI = '调用API时发生错误:' & Err.Description
Resume ExitFunction
End Function
' 构造API请求体
Function BuildRequestBody(inputText As String) As String
BuildRequestBody = '{''model'': ''deepseek-chat'', ''messages'': [{''role'':''system'', ''content'':''You are a Word assistant''}, {''role'':''user'', ''content'':''' & inputText & '''}], ''stream'': false}'
End Function
' 检查是否为JSON响应
Function IsJsonResponse(response As String) As Boolean
IsJsonResponse = (Left(response, 1) = '{')
End Function
' 辅助函数:格式化 JSON 响应
Function FormatJson(json As String) As String
Dim i As Long
Dim indentLevel As Long
Dim result As String
Dim char As String
indentLevel = 0
result = ''
For i = 1 To Len(json)
char = Mid(json, i, 1)
Select Case char
Case '{', '['
result = result & char & vbCrLf & Space((indentLevel + 1) * 4)
indentLevel = indentLevel + 1
Case '}', ']'
indentLevel = indentLevel - 1
result = result & vbCrLf & Space(indentLevel * 4) & char
Case ','
result = result & char & vbCrLf & Space(indentLevel * 4)
Case ':'
result = result & char & ' '
Case Else
result = result & char
End Select
Next i
FormatJson = result
End Function
' 辅助函数:替换特殊字符
Function ReplaceSpecialCharacters(text As String) As String
ReplaceSpecialCharacters = Replace(Replace(Replace(Replace(Replace(text, '\', '\\'), vbCrLf, ''), vbCr, ''), vbLf, ''), Chr(34), '\''')
End Function
需要注意的是,请将【替换为自己申请的API Key】部分按实际情况予以替换。 四、添加DeepSeek模块按钮 1、在【开发工具】中新建组。选中【开发工具】,右键选择【添加新组】; ![]() 2、按下图顺序,向新建组中添加模块; ![]() 3、对模块进行重命名。选中上图标注5项目,右键点击【重命名】; ![]() 修改【显示名称】及图标; ![]() 我们也可采用同样方法将【新建组】改名,如DeepSeek。 ![]() 03 — 在word中调用DeepSeek 在Word中,我们可以通过按钮直接调用DeepSeek。在word中输入需要提问的内容,选中后,点击【DeepSeek】按钮; ![]() 经过短暂的等待,完成后,系统提示【API分析内容已插入文档!】; ![]() 回答问题如下: ![]() 04 — 安全提示 一、谨慎启用宏 仅从官方渠道安装插件,避免运行未知来源的代码。 二、保护API密钥 不要将密钥明文写入VBA代码或分享给他人。 05 — 结语 通过以上方法,你已成功将DeepSeek“装进”Word!无论是撰写报告、论文还是日常办公,AI助手都能成为你的高效助手。赶紧去试试吧~ 互动提问:你在Word中最想用AI解决什么问题?欢迎留言讨论! |
|
来自: 汉无为 > 《DeepSeek》