1、进入官网地址。
DeepSeek
2、进入DeepSeek的API文档
3、点击DeepSeek开放平台左侧的“API Keys”, 再点击“创建API Key”
4、在弹出的对话框中,输入自己的API Key名称,点击创建。
sk-0385cad5e19346a0a4ac8b7f0d7be428
5、打开Word文档。
6、Word找到文件。
7、文件-选项
8、在“word选项”对话框中,点击选择“自定义功能区”,然后在右侧选中“开发工具”,使“开发工具”前面的方框中出现“”,word的菜单栏中就会出现“开发工具”项目。
9、打开开发工具。
10、信任中心-信任中心设置。
11、信任中心-宏的设置-全部启用。
12、在“信任中心设置”对话框中,点击“宏设置”,在左侧选择“启用所有宏……”,然后选中“信任对VBA工程对象模型的访问”。
13、在word菜单栏,点击“开发工具”项,选择“Visual Basic”。
14、进入Visual Basic编辑界面,如下图,选择“Normal”下的“模块”。
Option ExplicitSub DeepSeekV3()Const api_Url As String = "https://api.deepseek.com/chat/completions"Const api_Key As String = "替换为自己申请的API Key"Dim inputText As StringDim response As StringDim originalSelection As RangeOn Error GoTo ErrorHandler' 检查API密钥内容If api_Key = "" ThenMsgBox "请输入您的API Key。", vbExclamationExit SubEnd If' 检查是否选中文本If Selection.Type <> wdSelectionNormal ThenMsgBox "请选中要分析的文本。", vbExclamationExit SubEnd If' 保存原始选中的文本Set originalSelection = Selection.Range.Duplicate' 处理选中的文本inputText = ReplaceSpecialCharacters(Selection.text)' 调用Deepseek APIresponse = CallDeepseekAPI(api_Key, api_Url, inputText)' 处理API响应If IsJsonResponse(response) Thenresponse = FormatJson(response)InsertResponseToDocument responseMsgBox "API分析内容已插入文档!", vbInformationElseMsgBox response, vbCriticalEnd IfExit SubErrorHandler:MsgBox "发生错误:" & Err.Description, vbCriticalEnd Sub' 将API响应插入文档Sub InsertResponseToDocument(response As String)With ActiveDocument.Content.InsertAfter vbCrLf & "分析内容:" & vbCrLf & responseEnd WithEnd Sub' 调用DeepseekAPI函数Function CallDeepseekAPI(api_Key As String, api_Url As String, inputText As String) As StringDim http As ObjectDim requestBody As StringDim response As StringDim status_Code As IntegerOn 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 requestBodystatus_Code = .Statusresponse = .responseTextEnd With' 检查HTTP状态码If status_Code = 200 ThenCallDeepseekAPI = responseElseCallDeepseekAPI = "API 请求失败,状态码:" & status_Code & "-" & responseEnd IfExitFunction:Set http = NothingExit FunctionErrorHandler:CallDeepseekAPI = "调用API时发生错误:" & Err.DescriptionResume ExitFunctionEnd Function' 构造API请求体Function BuildRequestBody(inputText As String) As StringBuildRequestBody = "{""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 BooleanIsJsonResponse = (Left(response, 1) = "{")End Function' 辅助函数:格式化 JSON 响应Function FormatJson(json As String) As StringDim i As LongDim indentLevel As LongDim result As StringDim char As StringindentLevel = 0result = ""For i = 1 To Len(json)char = Mid(json, i, 1)Select Case charCase "{", "["result = result & char & vbCrLf & Space((indentLevel + 1) * 4)indentLevel = indentLevel + 1Case "}", "]"indentLevel = indentLevel - 1result = result & vbCrLf & Space(indentLevel * 4) & charCase ","result = result & char & vbCrLf & Space(indentLevel * 4)Case ":"result = result & char & " "Case Elseresult = result & charEnd SelectNext iFormatJson = resultEnd Function' 辅助函数:替换特殊字符Function ReplaceSpecialCharacters(text As String) As StringReplaceSpecialCharacters = Replace(Replace(Replace(Replace(Replace(text, "\", "\\"), vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), "\""")End Function
15、模块代码编辑完成后,进入“word选项”对话框,选择“开发工具”,在其上右击,选择“添加新组”。
16、选中新添加的组,在“下列位置选择命令”的下拉框中,选择“宏”。在出现的模块列表中,选择新编辑的模块,点击“添加”按钮
17、测试。