Word 里面嵌入DeepSeek

目录

一、问题描述

二、解决方法

三、代码

四、注意事项

五、总结


一、问题描述

如何在Word里面嵌入DeepSeek?


二、解决方法

1、新建文档,按 Alt+F11,进入VB界面。

2、选中文档,右键->插入->模块。

3、进入模块,粘入VBA代码。(代码见下文)

4、保存为带宏的word文档。

5、回到文档,打开文件->选项->信任中心->宏设置->启用所有宏。

不同版本word可能界面有所不同,但大同小异,启用宏即可。

6、(1)在左侧选择宏,显示已保存的程序模块;

(2)在右侧,主选项卡->开发工具->新建组deepseek;

(3)左侧选中模块1,右侧选中新建的组deepseek,点击中间的 添加,确定。

7、回到word界面,开发工具的选项卡下多了一个DeepSeek模块

8、 测试程序运行情况,看一下效果(响应时间稍微有点长)


三、代码

代码来源(如有侵权,请联系博主删除):如何在wps中加载deepseek?(文章后附VBA代码)_wps接入deepseek的vba代码-CSDN博客

DeepSeek嵌入Word之VBA | 编程语言前沿技术分享

Word接入DeepSeek - 王鹏鑫 - 博客园

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
        CallDeepSeekAPI = "Error: " & status_code & " - " & response
    End If
 
    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 = "输入你的deepseek 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
        End If
    Else
        MsgBox response, vbCritical
    End If
End Sub

四、注意事项

1、API地址与密钥

API 地址,https://api.deepseek.com/chat/completions

密钥,deepseek官网,创建,初次注册可以免费体验几次

2、返回报错与解决方法

参考官网报错代码:错误码 | DeepSeek API Docs

3、使用硅基流动网址与密钥

网址:https://api.siliconflow.cn/v1/chat/completions

密钥:官网注册创建,硅基流动统一登录

(注册邀请码:ogasTAfR )

SiliconFlow, Accelerate AGI to Benefit Humanity

模型路径:deepseek-ai/DeepSeek-V3

4、代码优化

直接第三步骤的代码,运行的结果,回车符\n会保留在文档中,如下图所示,

返回VBA代码,在过程Sub DeepSeekV3()中,在response 赋值语句下方添加一行代码:

            '把\n换成换行符
            response = Replace(response, "\n", vbCrLf)

其中,vbCrLf或Chr(10) 表示换行。


五、总结

在Word里面嵌入使用deepseek工具,可以通过VB程序来访问API地址来获取内容,优势是减少从网站里Ctrl+C和Ctrl+V 的操作,简化了操作步骤,实现文档内容一步到位。劣势是响应时间较长。

日期:2025年02月14日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值