Deepseek 接入Word处理对话框(隐藏密钥)

硅基流动邀请码:1zNe93Cp

邀请链接:网页链接

亲测deepseek接入word,自由调用对话,看截图有兴趣的复用代码(当然也可以自己向deepseek提问,帮助你完成接入,但是提问逻辑不一样给出的答案是千差万别的)项目首页 - VBA-JSONVBA中的JSON转换与解析工具:VBA-JSON:VBA中的JSON转换与解析工具VBA-JSON 是一个专为 VBA(Visual Basic for Applications)设计的 JSON 转换和解析工具 - GitCode

建议:去硅基流动申请,API响应较快速准确稳定

复制代码
' 在用户窗体代码模块中添加以下代码
Private Sub btnSubmit_Click()
    On Error GoTo ErrorHandler
    
    Dim apiKey As String
    Dim apiUrl As String
    Dim jsonBody As String
    Dim responseText As String
    
    ' 配置API信息(需替换为实际值)
    apiKey = "sk-f5e25a3127de4e2e928d1800c7820e08"
    apiUrl = "https://api.deepseek.com/v1/chat/completions" ' 示例URL
    
    ' 构建请求体
    jsonBody = "{"
    jsonBody = jsonBody & """model"": ""deepseek-chat"","
    jsonBody = jsonBody & """messages"": [{""role"": ""user"", ""content"": """ & Me.txtQuery.text & """}]"
    jsonBody = jsonBody & "}"
    
    ' 发送请求
    responseText = SendAPIRequest(apiUrl, apiKey, jsonBody)
    
    ' 解析并插入结果
    InsertFormattedResponse (ParseResponse(responseText))
    
    Me.lblStatus.Caption = "处理成功!"
    Exit Sub

ErrorHandler:
    Me.lblStatus.Caption = "错误: " & Err.Description
End Sub

' API请求函数
Private Function SendAPIRequest(url As String, key As String, body As String) As String
    Dim http As Object
    Set http = CreateObject("MSXML2.XMLHTTP")
    
    http.Open "POST", url, False
    http.setRequestHeader "Content-Type", "application/json"
    http.setRequestHeader "Authorization", "Bearer " & key
    
    http.send body
    
    If http.Status = 200 Then
        SendAPIRequest = http.responseText
    Else
        Err.Raise vbObjectError + 1, , "API请求失败: " & http.Status & " - " & http.StatusText
    End If
End Function

' JSON响应解析(简单实现)
Private Function ParseResponse(json As String) As String
    Dim result As String
    Dim startPos As Long
    Dim endPos As Long
    
    startPos = InStr(json, """content"":""") + 10
    endPos = InStr(startPos, json, """,""")
    
    If startPos > 10 And endPos > startPos Then
        result = Mid(json, startPos, endPos - startPos)
        result = Replace(result, "\n", vbCrLf)
        result = Replace(result, "\""", """")
        ParseResponse = result
    Else
        Err.Raise vbObjectError + 2, , "响应解析失败"
    End If
End Function

' 排版输出函数
Private Sub InsertFormattedResponse(text As String)
    With ActiveDocument.Range
        .InsertAfter vbCrLf & "【DeepSeek响应】" & vbCrLf
        .Font.Bold = True
        .Font.Color = RGB(0, 102, 204)
        
        .InsertAfter text & vbCrLf
        .Font.Bold = False
        .Font.Color = RGB(0, 0, 0)
        
        ' 添加分隔线
        .InsertAfter String(50, "=") & vbCrLf
        .Font.Color = RGB(169, 169, 169)
    End With
End Sub
相关推荐
xiayutian_c42 分钟前
Claude code不能直接写ABAP?
ai编程
RANCE_atttackkk1 小时前
Springboot+langchain4j的RAG检索增强生成
java·开发语言·spring boot·后端·spring·ai·ai编程
undsky_4 小时前
【RuoYi-SpringBoot3-Pro】:将 AI 编程融入传统 java 开发
java·人工智能·spring boot·ai·ai编程
渔阳节度使4 小时前
SpringAI RAG全链路讲解
ai编程
取个鸣字真的难5 小时前
简单快速的用 Claude Code 帮你创建 PPT 生成 Skills
vscode·powerpoint·ai编程
Stuomasi_xiaoxin5 小时前
Windows 安装 OpenCode 完全指南:从 npm 踩坑到 Chocolatey 完美部署
python·ai编程·opencode
GISer_Jing1 天前
Memory、Rules、Skills、MCP如何重塑AI编程
前端·人工智能·aigc·ai编程
github.com/starRTC1 天前
Claude Code中英文系列教程24:使用钩子hooks扩展 Claude Code 的行为
人工智能·ai编程
Testopia1 天前
AI编程实例 - 爆款文章预测:K-Means聚类与分类算法的实践
人工智能·分类·kmeans·ai编程·聚类
ssxueyi1 天前
AI编程之智谱+claude code详细安装使用教程
人工智能·ai编程·智谱·claude code