黑科技!用腳本將DeepSeek嵌入Word中,5分鐘搞定(附保姆級教程)
大家好,我是岳哥。
岳哥最近建了幾個DeepSeek交流群,大家五花八門的玩法和需求一大堆。
有小伙伴問DeepSeek能不能接入Office辦公軟件,岳哥研究了一番還真可以。
今天教大家如何用VB宏命令將DeepSeek集成到Word中。
獲取API-Key
這一步是我們操作的關(guān)鍵,目前我們可以直接只用官網(wǎng)的API來實現(xiàn)。
登錄官網(wǎng)的API平臺:
沒注冊的注冊完登錄一下,我們點擊到左側(cè)菜單的“APIKeys”按鈕,然后點擊右側(cè)的“創(chuàng)建API Key”
圖片
在彈出的對話框中備注一下,以防忘記,這里我們備注為“Word”,表名是在Word上使用的。
圖片
點擊創(chuàng)建,這樣我們就獲得了一個API Key了。記得先將密鑰復(fù)制存放一下。一旦關(guān)閉就無法復(fù)制,只能刪了重新創(chuàng)建。
圖片
這樣我們就獲得了一個API key。
開啟宏命令
要將DeepSeek集成到Word中必須先使用到“宏”,但是Office默認(rèn)是禁用宏的,所以咱們得先開啟一下宏命令的功能。
打開Word文檔,點擊“文件”——“選項”——“自定義功能”——“信任中心”——“信任中心設(shè)置”,勾選如下兩個功能,并確定。
圖片
這樣我們就啟用了宏命令功能。
啟動開發(fā)工具
一般的Word因為禁用了“宏命令”,對應(yīng)的“開發(fā)工具”菜單也沒開啟,我們也需要開啟一下。
按如下操作啟動“開發(fā)工具”菜單:“文件”——“選項”——“自定義功能”,勾選啟動“開發(fā)工具”菜單
圖片
創(chuàng)建VB宏命令
前面的設(shè)置主要是為了方便大家能在菜單欄里找到我們即將設(shè)置的VB宏,接下來才是重點內(nèi)容,跟著我操作。
點擊“開發(fā)工具”下的VB編輯器
圖片
進(jìn)入后選擇菜單中的“插入”-“模塊”,命名為“DeepSeek”。
圖片
然后將下面的一串代碼復(fù)制粘貼到模塊中。
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助手,請根據(jù)用戶的問題給出詳細(xì)的解答。""}, {""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 響應(yīng)(調(diào)試用)
' 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
' 創(chuàng)建正則表達(dá)式對象來分別匹配推理內(nèi)容和最終回答
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
' 提取推理內(nèi)容
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
' 將光標(biāo)移回原來選中文本的末尾
originalSelection.Select
Else
MsgBox "Failed to parse API response.", vbExclamation
End If
Else
MsgBox response, vbCritical
End If
End Sub
并將其中需要填充API-Key的地方替換成我們前面準(zhǔn)備好的API-Key。
圖片
保存VB腳本并關(guān)閉窗口,再點擊“文件”-“選項”-“自定義功能區(qū)”,我們選擇這里的宏,就可以看到我們剛才創(chuàng)建的宏命令了。
圖片
新建宏命令組
我們在右側(cè)繼續(xù)右鍵“開發(fā)工具”,在它下面新建一個組。
圖片
然后將這個組重命名為“AI”或者你喜歡的名稱,并且選一個你喜歡的圖標(biāo)。
圖片
將上面的宏添加到這個下面即可。
圖片
點擊“確定”,我們就可以在菜單欄的“開發(fā)工具”中看到這個按鈕了。
圖片
我們在下面的Word文檔中測試一下這個功能,在Word里輸入一段文字,點擊這個按鈕。
等待一會兒后兒(DeepSeek R1在推理),就會出現(xiàn)DeepSeek的思考過程和最終結(jié)果。
圖片
這樣之后我們要寫什么內(nèi)容,要檢查錯別字,擴(kuò)寫,翻譯等等需求都可以直接在Word里完成。
另存為模板
上面的操作只是針對當(dāng)前這個Word文檔,如果要一勞永逸,每次打開都有這個腳本存在,我們只需要將這個Word文檔另存為模板。
圖片
這樣下次打開的時候,文檔還是會附帶這個宏命令。