DeepSeek - Word接入DeepSeek

807 阅读3分钟

一、前期准备

  1. 获取API密钥

    1. 访问 DeepSeek 官网:https://platform.deepseek.com
    2. 注册/登录账号
    3. 进入控制台:API Keys
    4. 创建新的API密钥并保存
    
  2. 启用Word开发工具

    1. 文件 → 选项 → 自定义功能区
    2. 右侧勾选「开发工具」
    3. 确定
    

二、创建VBA代码

  1. 打开VBA编辑器

    1. 点击「开发工具」选项卡
    2. 点击「Visual Basic」
    或
    按 Alt + F11
    
  2. 添加引用

    1. 工具 → 引用
    2. 勾选:
       ☑ Microsoft XML, v6.0
       ☑ Microsoft VBScript Regular Expressions 5.5
    3. 确定
    
  3. 创建新模块

    1. 插入 → 模块
    2. 粘贴以下代码
    

三、完整代码

Option Explicit

' API配置(替换为你的API密钥)
Private Const API_KEY As String = "your_api_key_here"
Private Const API_URL As String = "https://api.deepseek.com/v1/chat/completions"
Private Const MODEL_NAME As String = "deepseek-chat"

' 主函数
Sub CallDeepSeekAPI()
    On Error GoTo ErrorHandler
    
    ' 检查是否选择了文本
    If Selection.Type = wdSelectionIP Then
        MsgBox "请先选择要处理的文本!", vbExclamation
        Exit Sub
    End If
    
    ' 创建HTTP对象
    Dim http As Object
    Set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    
    ' 构建请求体
    Dim requestBody As String
    requestBody = "{""model"":""" & MODEL_NAME & """," & _
                  """messages"":[{""role"":""user"",""content"":""" & _
                  SafeJsonEncode(Selection.Text) & """}]}"
    
    ' 发送请求
    With http
        .Open "POST", API_URL, False
        .setRequestHeader "Content-Type", "application/json; charset=UTF-8"
        .setRequestHeader "Authorization", "Bearer " & API_KEY
        .send requestBody
    End With
    
    ' 处理响应
    If http.Status = 200 Then
        Dim responseText As String
        responseText = ParseResponse(http.responseText)
        
        ' 在选中文本后插入响应
        Selection.InsertAfter vbCrLf & vbCrLf & responseText
    Else
        MsgBox "API请求失败:" & http.Status & " - " & http.statusText, vbCritical
    End If
    
    Exit Sub
    
ErrorHandler:
    MsgBox "发生错误:" & Err.Description, vbCritical
End Sub

' 安全JSON编码
Private Function SafeJsonEncode(ByVal InputText As String) As String
    Dim result As String
    result = Replace(InputText, "\", "\\")
    result = Replace(result, """", "\""")
    result = Replace(result, vbCr, "\r")
    result = Replace(result, vbLf, "\n")
    result = Replace(result, vbTab, "\t")
    SafeJsonEncode = result
End Function

' 解析API响应
Private Function ParseResponse(ByVal jsonText As String) As String
    Dim regex As Object
    Set regex = CreateObject("VBScript.RegExp")
    
    regex.Pattern = """content"":""([^""]*)"""
    regex.Global = True
    
    If regex.Test(jsonText) Then
        Dim matches As Object
        Set matches = regex.Execute(jsonText)
        
        ParseResponse = matches(0).SubMatches(0)
        ParseResponse = Replace(ParseResponse, "\""", """")
        ParseResponse = Replace(ParseResponse, "\n", vbCrLf)
    Else
        ParseResponse = "无法解析API响应"
    End If
End Function

四、使用方法

  1. 配置API密钥

    将代码中的 your_api_key_here 替换为你的实际API密钥
    
  2. 保存文档

    1. 文件 → 另存为
    2. 保存类型选择:Word 启用宏的文档 (*.docm)
    
  3. 使用步骤

    1. 选择要处理的文本
    2. 按 Alt + F8 打开宏对话框
    3. 选择 CallDeepSeekAPI
    4. 点击「运行」
    

五、常见问题解决

  1. 宏被禁用

    文件 → 选项 → 信任中心 → 信任中心设置 → 宏设置
    选择:启用所有宏
    
  2. 运行时错误

    1. 检查API密钥是否正确
    2. 确认网络连接正常
    3. 验证所有必要引用已添加
    
  3. 响应格式错误

    1. 检查API返回的具体内容
    2. 使用Debug.Print输出中间结果
    

六、安全建议

  1. API密钥保护

    - 不要分享包含API密钥的文档
    - 考虑使用环境变量存储密钥
    
  2. 文档保护

    1. 开发工具 → 保护文档
    2. 限制编辑和格式设置
    

七、进阶功能

  1. 添加进度提示

    Application.ScreenUpdating = False
    Application.StatusBar = "正在处理..."
    ' 代码执行完毕后
    Application.StatusBar = False
    Application.ScreenUpdating = True
    
  2. 添加快捷键

    1. 视图 → 宏 → 宏
    2. 选择CallDeepSeekAPI
    3. 选项 → 指定快捷键