← 返回目录

3. 使用方法

第 2 / 4 卷

3. 使用方法

\1.选中需要处理的文字

\2.点击"生成"按钮

\3.等待大模型响应

4. 创建模板

\1.将你的文档另存为Word模板(.dotm):

-点击"文件" → "另存为"

-选择保存类型为"Word启用宏的模板(.dotm)"

-保存到Word的模板文件夹(通常是C:\Users\用户名 \AppData\Roaming\Microsoft\Word\STARTUP)

-这样每次打开Word时,宏就会自动可用

相关代码

DeepSeekV3代码

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-chat"", ""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 SendTxt status_code = .Status response = .responseText End With

' 弹出窗口显示 API 响应(调试用)

' MsgBox "API Response: " & response, vbInformation, "Debug Info"

If status_code = 200 Then CallDeepSeekAPI = response Else

Set Http = Nothing End Function

Sub DeepSeekV3() Dim api_key As String Dim inputText As String Dim response As String Dim regex As Object Dim matches As Object Dim originalSelection As Object

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 regex = CreateObject("VBScript.RegExp") With regex .Global = True .MultiLine = True .IgnoreCase = False .Pattern = """content"":""(.*?)"""

End With Set matches = regex.Execute(response) If matches.Count > 0 Then response = matches(0).SubMatches(0) response = Replace(Replace(response, """", Chr(34)), """", Chr(34))

' 取消选中原始文本 Selection.Collapse Direction:=wdCollapseEnd

' 将内容插入到选中文字的下一行 Selection.TypeParagraph ' 插入新行 Selection.TypeText text:=response

' 将光标移回原来选中文本的末尾 originalSelection.Select Else MsgBox "Failed to parse API response.", vbExclamation

Else MsgBox response, vbCritical

全站分卷索引 · 4 卷
  1. 1全网最详细:DeepSeek-R1大模型嵌入Word教程,让你的办公效率起飞!
  2. 23. 使用方法
  3. 3deepseek-reasoner代码
  4. 4qwen2.5-max代码