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
相关推荐
小宋10216 小时前
Dify 知识库实战:从 PDF 导入到带引用回答,完整搭建企业问答助手
人工智能·ai编程
ServBay10 小时前
NVIDIA NeMo Switchyard 与智能模型路由趋势下,如何统一管理多供应商 API 与协议转换
aigc·ai编程·nvidia
9i编程11 小时前
4. AI编写的SKILL,坑我一一试过,这次我自己改写:换个工具,照样不按SKILL写文档
人工智能·openai·ai编程
Code额12 小时前
Python 连接 DeepSeek API,OpenAI
开发语言·python·ai·ai编程
intQ713 小时前
个人博客搭建过程
程序员·ai编程
掘金酱13 小时前
Vibe作品广场首发挑战来啦!发布作品,赢富士拍立得等千元好礼
openai·ai编程·vibecoding
Lambert28113 小时前
Spring AI 2.0 升级实战:9 个破坏性变更逐条迁移
aigc·ai编程
打呵欠的猫13 小时前
我用 AI 写了一个"需求翻译器",产品的 PRD 直接变成开发任务清单
前端·ai编程
七牛云行业应用13 小时前
DeepSeek Harness vs Codex vs Claude Code:三款 AI 编程 Harness 深度对比
人工智能·agent·ai编程
AprChell14 小时前
DeepSeek Harness 开源了一套 Vibe Coding 工程流水线
ai编程·deepseek·vibecoding