免費deepseek的API獲取教程:
1 https://cloud.siliconflow.cn/中注冊時填寫邀請碼:GAejkK6X即可獲取2000 萬 Tokens;
2 按照圖中步驟進行操作
將API接入word或WPS中
1 打開一個word,文件-選項-自定義功能區-勾選開發工具-左側的信任中心-信任中心設置-宏設置-啟用所有宏-確定
2 開發工具-VB-插入-模塊
3 在調出的窗口中輸入如下代碼
Function CallDeepSeekAPI(api_key As String, inputText As String) As StringDim API As StringDim SendTxt As StringDim Http As ObjectDim status_code As IntegerDim response As StringAPI = "https://api.siliconflow.cn/v1/chat/completions"SendTxt = "{""model"": ""deepseek-ai/DeepSeek-V3"", ""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 SendTxtstatus_code = .Statusresponse = .responseTextEnd With' 彈出窗口顯示 API 響應(調試用)' MsgBox "API Response: " & response, vbInformation, "Debug Info"If status_code = 200 ThenCallDeepSeekAPI = responseElseCallDeepSeekAPI = "Error: " & status_code & " - " & responseEnd IfSet Http = Nothing
End FunctionSub DeepSeekV3()Dim api_key As StringDim inputText As StringDim response As StringDim regex As ObjectDim matches As ObjectDim originalSelection As Objectapi_key = "替換為你的api key"If api_key = "" ThenMsgBox "Please enter the API key."Exit SubElseIf Selection.Type <> wdSelectionNormal ThenMsgBox "Please select text."Exit SubEnd If' 保存原始選中的文本Set originalSelection = Selection.Range.DuplicateinputText = Replace(Replace(Replace(Replace(Replace(Selection.text, "\", "\\"), vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), "\""")response = CallDeepSeekAPI(api_key, inputText)If Left(response, 5) <> "Error" ThenSet regex = CreateObject("VBScript.RegExp")With regex.Global = True.MultiLine = True.IgnoreCase = False.Pattern = """content"":""(.*?)"""End WithSet matches = regex.Execute(response)If matches.Count > 0 Thenresponse = matches(0).SubMatches(0)response = Replace(Replace(response, """", Chr(34)), """", Chr(34))' 取消選中原始文本Selection.Collapse Direction:=wdCollapseEnd' 將內容插入到選中文字的下一行Selection.TypeParagraph ' 插入新行Selection.TypeText text:=response' 將光標移回原來選中文本的末尾originalSelection.SelectElseMsgBox "Failed to parse API response.", vbExclamationEnd IfElseMsgBox response, vbCriticalEnd If
End Sub
4 文件-選項-自定義功能區-選中開發工具-新建組(命名為deepseek)
5 中間框中依次選中“宏”-deepseek模型-(選中剛創建的deepseek組)添加
6 文件-另存為帶宏的模板-放入C:\Users<YourUsername>\AppData\Roaming\Microsoft\Word\STARTUP中,完成
7上述代碼為v3模型,r1模型代碼如下:
Function CallDeepSeekAPI(api_key As String, inputText As String) As StringDim API As StringDim SendTxt As StringDim Http As ObjectDim status_code As IntegerDim response As StringAPI = "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 SendTxtstatus_code = .Statusresponse = .responseTextEnd With' 彈出窗口顯示 API 響應(調試用)' MsgBox "API Response: " & response, vbInformation, "Debug Info"If status_code = 200 ThenCallDeepSeekAPI = responseElseCallDeepSeekAPI = "Error: " & status_code & " - " & responseEnd IfSet Http = Nothing
End FunctionSub DeepSeekR1()Dim api_key As StringDim inputText As StringDim response As StringDim regex As ObjectDim matches As ObjectDim originalSelection As Objectapi_key = "替換為你的api key"If api_key = "" ThenMsgBox "Please enter the API key."Exit SubElseIf Selection.Type <> wdSelectionNormal ThenMsgBox "Please select text."Exit SubEnd If' 保存原始選中的文本Set originalSelection = Selection.Range.DuplicateinputText = Replace(Replace(Replace(Replace(Replace(Selection.text, "\", "\\"), vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), "\""")response = CallDeepSeekAPI(api_key, inputText)If Left(response, 5) <> "Error" ThenSet regex = CreateObject("VBScript.RegExp")With regex.Global = True.MultiLine = True.IgnoreCase = False.Pattern = """content"":""(.*?)"""End WithSet matches = regex.Execute(response)If matches.Count > 0 Thenresponse = matches(0).SubMatches(0)response = Replace(Replace(response, """", Chr(34)), """", Chr(34))' 取消選中原始文本Selection.Collapse Direction:=wdCollapseEnd' 將內容插入到選中文字的下一行Selection.TypeParagraph ' 插入新行Selection.TypeText text:=response' 將光標移回原來選中文本的末尾originalSelection.SelectElseMsgBox "Failed to parse API response.", vbExclamationEnd IfElseMsgBox response, vbCriticalEnd If
End Sub
完整版見鏈接:https://rvq2yh94tm.feishu.cn/docx/HXLDdLmZcoE7hGxS1RqcomEanSd