使用VB宏来实现,目前暂时没有将结果中的markdown转换为word格式
新建一个开启宏的Word文档:
文件->选项
打开“开发工具”选项卡
选择:Visual Basic
创建模块
输入以下代码:
Sub DeepSeek()
On Error GoTo ErrorHandler
Dim selectedText As String
Dim apiKey As String
Dim response As Object, re As String
Dim midString As String
Dim ans As String
Dim URL As String
' 检查是否有选择的文本
If Selection.Type = wdSelectionNormal Then
selectedText = Selection.Text
selectedText = Replace(selectedText, ChrW$(13), "")
apiKey = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
URL = "https://api.deepseek.com/chat/completions"
' 显示等待提示
Application.StatusBar = "正在处理中,请稍候..."
' 发送 API 请求
Set response = CreateObject("MSXML2.XMLHTTP")
response.Open "POST", URL, True
response.setRequestHeader "Content-Type", "application/json"
response.setRequestHeader "Authorization", "Bearer " + apiKey
response.Send "{""model"":""deepseek-chat"", ""messages"":[{""role"":""user"",""content"":""" & selectedText & """}], ""stream"":false}"
' 等待响应
Do While response.readyState <> 4
DoEvents ' 允许界面响应,保持状态更新
Loop
' 处理返回的结果
re = response.responseText
midString = Mid(re, InStr(re, """content"":""") + 11)
ans = Split(midString, """")(0)
ans = Replace(ans, "\n", "")
' 更新文本内容
Selection.Text = selectedText & vbNewLine & ans
' 清除等待提示
Application.StatusBar = False
Else
MsgBox "请先选择一段文本"
End If
Exit Sub
ErrorHandler:
' 清除等待提示
Application.StatusBar = False
MsgBox "发生错误:" & Err.Description
End Sub
将VB添加到选项卡中,方便使用:
选中文字后,点击选项卡中DeepSeek: