DeepSeek 接入 Word 后,可以實現翻譯、內容擴寫以及檢查錯別字等操作。用戶可通過啟用并創建宏命令等步驟進行接入。下面,小編為大家整理了詳細的操作教程,感興趣的小伙伴千萬不要錯過。
DeepSeek 怎么接入 Word
- 首先需要打開 Word 文檔,然后依次點擊【文件】、【更多】、【Word 選項】、【自定義功能區】,勾選【開發工具】。
- 完成后需要按照下方步驟進行信任中心界面,找到宏設置后點擊【啟用所有宏】。
- 依次點擊【開發工具】、【Visual Basic】按鈕。
- 選擇菜單后點擊【插入】、【模塊】,然后將圖中紅框處命名為【DeepSeek】。
- 之后將下方代碼粘貼至 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服務地址
API = "https://apiemp.baystoneai.com/cognihub/service/v1/chat/completions"
' API模型名稱
SendTxt = "{""model"": ""deepseek-r1-distill-qwen"", ""messages"": [{""role"":""system"", ""content"":""You are a helpful 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 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密鑰:替換為你的api key
api_key = "YOUR_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, "nn", 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, "nn", 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 - 保存后將這個彈框關閉,然后依次點擊【文件】、【更多】、【選項】、【自定義功能區】,然后勾選宏。
- 接著根據圖中順序進行操作。
- 點擊下方按鈕后,點擊繼續根據圖中提示進行操作。
- 就能在開發工具界面看到下方按鈕。
- 最后大家只需在 Word 文檔中輸入一段文字,然后點擊新建的按鈕,就能進行內容擴寫、翻譯、檢查錯別字等操作了。
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END
暫無評論內容