一、獲取API-Key
目前我們可以直接只用官網的API來實現,申請這一步是關鍵
也可以直接訪問官網的API平臺:https://platform.deepseek.com/ ,沒注冊的注冊完登錄一下,我們點擊到左側菜單的“APIKeys”按鈕,然后點擊右側的“創建API Key”
在彈出的對話框中備注一下,以防忘記,點擊創建,這樣我們就獲得了一個API Key了。記得先將密鑰復制存放一下。一旦關閉就無法復制,只能刪了重新創建。
二:WORD開啟宏命令
要將DeepSeek集成到Word中必須先使用到“宏”,但是Office默認是禁用宏的,需先開啟一下宏命令的功能。
-
啟動Word應用程序。
-
在Word界面的頂部,找到并點擊“文件”菜單。
-
在下拉菜單中,選擇“選項”按鈕以進入設置界面。
-
在彈出的“Word選項”窗口中,找到并點擊左側的“信任中心”設置。
-
在“信任中心”設置中,點擊“信任中心設置...”按鈕。
-
在“信任中心”對話框中,切換到“宏設置”選項卡。
-
在這里,您可以選擇以下選項之一:
-
“禁用所有宏,帶通知”:所有宏都將被禁用,但會通知您宏的存在。
-
“禁用所有宏,不通知”:所有宏都將被禁用,且不會通知您。
-
“啟用所有宏”:所有宏都將被允許運行,但請注意,這可能會帶來安全風險。
-
“禁用所有宏,除了簽名的宏”:所有未簽名的宏都將被禁用,但簽名的宏將被允許運行。
-
-
根據您的需求選擇合適的宏設置,然后點擊“確定”保存設置。
-
再次點擊“確定”關閉“信任中心”對話框,然后關閉“Word選項”窗口。
完成以上步驟后,您就可以在Word中使用宏功能了。如果您需要啟用特定的宏,確保選擇了允許宏運行的設置,并確保宏是來自可信的來源。啟用宏后,您可以按照DeepSeek的集成指南,繼續進行DeepSeek與Word的集成操作。
三、啟動開發工具
?一般Word因為禁用了“宏命令”,對應的“開發工具”菜單沒開啟,我們需要開啟一下。按如下操作啟動 “開發工具”菜單:“文件”——“選項”——“自定義功能”,勾選啟動“開發工具”菜單
四、創建VB宏命令
點擊“開發工具”下的Visual Basic編輯器
進入后點擊菜單中的“插入”-“模塊”,命名為“DeepSeek”
如下代碼復制粘貼到模塊中。
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-reasoner"", ""messages"": [{""role"":""system"", ""content"":""你是一個樂于助人的AI助手,請根據用戶的問題給出詳細的解答。""}, {""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 DeepSeekR1()
Dim api_key As?String
Dim inputText As?String
Dim response As?String
Dim regex As?Object
Dim reasoningRegex As?Object
Dim contentRegex As?Object
Dim matches As?Object
Dim reasoningMatches As?Object
Dim originalSelection As?Object
Dim reasoningContent As?String
Dim finalContent As?String
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 = CallDeepSeekAPI(api_key, inputText)
If Left(response, 5) <> "Error" Then
? ? '?創建正則表達式對象來分別匹配推理內容和最終回答
? ??Set?reasoningRegex = CreateObject("VBScript.RegExp")
? ? With reasoningRegex
? ? ? ? .Global = True
? ? ? ? .MultiLine = True
? ? ? ? .IgnoreCase = False
? ? ? ? .Pattern =?"""reasoning_content"":""(.*?)"""
? ? End With
? ??
? ??Set?contentRegex = CreateObject("VBScript.RegExp")
? ? With contentRegex
? ? ? ? .Global = True
? ? ? ? .MultiLine = True
? ? ? ? .IgnoreCase = False
? ? ? ? .Pattern =?"""content"":""(.*?)"""
? ? End With
? ??' 提取推理內容
? ? Set reasoningMatches = reasoningRegex.Execute(response)
? ? If reasoningMatches.Count > 0 Then
? ? ? ? reasoningContent = reasoningMatches(0).SubMatches(0)
? ? ? ? reasoningContent = Replace(reasoningContent, "\n\n", vbNewLine)
? ? ? ? reasoningContent = Replace(reasoningContent, "\n", vbNewLine)
? ? ? ? reasoningContent = Replace(Replace(reasoningContent, """", Chr(34)), """", Chr(34))
? ? End If
? ? '?提取最終回答
? ??Set?matches = contentRegex.Execute(response)
? ? If matches.Count >?0?Then
? ? ? ? finalContent = matches(0).SubMatches(0)
? ? ? ? finalContent = Replace(finalContent,?"\n\n", vbNewLine)
? ? ? ? finalContent = Replace(finalContent,?"\n", vbNewLine)
? ? ? ? finalContent = Replace(Replace(finalContent,?"""", Chr(34)),?"""", Chr(34))
? ? ? ??' 取消選中原始文本
? ? ? ? Selection.Collapse Direction:=wdCollapseEnd
? ? ? ? '?插入推理過程(如果存在)
? ? ? ? If Len(reasoningContent) >?0?Then
? ? ? ? ? ? Selection.TypeParagraph
? ? ? ? ? ? Selection.TypeText?"推理過程:"
? ? ? ? ? ? Selection.TypeParagraph
? ? ? ? ? ? Selection.TypeText reasoningContent
? ? ? ? ? ? Selection.TypeParagraph
? ? ? ? ? ? Selection.TypeText?"最終回答:"
? ? ? ? ? ? Selection.TypeParagraph
? ? ? ? End If
? ? ? ??' 插入最終回答
? ? ? ? Selection.TypeText finalContent
? ? ? ? '?將光標移回原來選中文本的末尾
? ? ? ? originalSelection.Select
? ? Else
? ? ? ? MsgBox?"Failed to parse API response.", vbExclamation
? ? End If
Else
? ? MsgBox response, vbCritical
End If
End Sub
上述代碼這替換為你自己的Key:
保存VB腳本并關閉窗口,再點擊“文件”-“選項”-“自定義功能區”,我們選擇這里的宏,就可以看到我們剛才創建的宏命令了。
五、新建宏命令組
我們在右側繼續右鍵“開發工具”,在它下面新建一個組。
然后將這個組重命名為“AI”或者你喜歡的名稱,再選一個你喜歡的圖標。
將上面的宏添加到這個下面就可完成。
點擊“確定”,我們就可以在菜單欄的“開發工具”中看到這個按鈕了。
我們在下面的Word文檔中測試下這個功能,在Word里輸入段文字,點擊這個按鈕。等待一會兒(DeepSeek R1在推理),就會出現DeepSeek的思考過程和最終結果
這樣以后我們要寫什么內容,要檢查錯別字,翻譯、文章、方案等等需求都可以直接在Word里完成。
六、另存為模板
?由于上面的操作只是針對當前這個Word文檔,如果想每次打開都有這個腳本存在,我們只需要將這個Word文檔另存為模板
這樣下次打開文檔還是會附帶這個宏命令,就這樣整個操作完成了,