请看优化前后对比:

1. 下载 WPS Office

https://www.wps1.com/windows

2. 新建一个wps文档,点击左上角的文件,再点击选项

3. 在选项中点击自定义功能区,切换为所有选项卡,确保开发工具勾选上。 4 进入信任中心,点击宏安全性,将安全性设置为,点击确定

5.  回到文件主页,点击菜单栏的开发工具,点击VB编辑器

  • 没有VB编辑器的话可以在下面的链接获取,下载了exe之后直接安装载自己的电脑上就好了。(然后重启wps)
https://gitee.com/cursorai/wps.vba

 6. 在VB编辑器中点击插入,再点击模块

7. 复制下面的代码到弹出的窗口中,但是在这之前要替换apikey(下一步我将介绍怎么获取apikey)

Function CallCursorAiAPI(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.cursorai.art/v1/chat/completions"
    SendTxt = "{""model"": ""deepseek-r1"", ""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
        ' 替换转义字符,优化用户体验
        response = Replace(response, "\u003c", "<")
        response = Replace(response, "\u003e", ">")
        CallCursorAiAPI = response
    Else
        CallCursorAiAPI = "Error: " & status_code & " - " & response
    End If

    Set Http = Nothing
End Function

Sub DeepSeekR1()
    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 = "替换成你自己的apikey"
    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 = CallCursorAiAPI(api_key, inputText)
    response = Replace(response, "\n", "")

    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))
            ' 替换转义字符,优化用户体验
            response = Replace(response, "\u003c", "<")
            response = Replace(response, "\u003e", ">")
            response = Replace(response, "</think>", "</think>||NEWLINE||")

            ' 取消选中原始文本
            Selection.Collapse Direction:=wdCollapseEnd

            ' 将内容插入到选中文字的下一行
            Selection.TypeParagraph ' 插入新行
            ' 按特殊标记分割 response
            Dim parts() As String
            parts = Split(response, "||NEWLINE||")

            ' 按部分插入到 Word 文档
            Dim i As Integer
            For i = LBound(parts) To UBound(parts)
                Selection.TypeText text:=parts(i)
                Selection.TypeParagraph ' 插入新行
            Next i

            ' 将光标移回原来选中文本的末尾
            originalSelection.Select
        Else
            MsgBox "Failed to parse API response.", vbExclamation
        End If
    Else
        MsgBox response, vbCritical
    End If
End Sub

8. 进入CURSOR API官网里面注册好账号之后,点击API令牌,点击复制即可,将复制下来的内容ctrl+v完整的替换掉替换成你自己的apikey,要保留代码里面的引号。例如下面的:

api_key = "sk-123456789xxxxxxxx"

 9. 将VB编辑器关闭,进入选项页面,自定义功能区,点击新建组,并重命名组为Deepseek。

10. 在左侧面板下拉位置选择选中我们刚刚用VB编辑器创建的宏程序,将其添加到我们新建的deepseek组里,点击确定 11. 进入文档编辑页面,点击开发工具,再选中文本的状态下,点击deepseek模块

12. 最后另存模板文件deepseek.dotm到下面的目录(其中Administrator要改成你自己的用户名),就可以在以后的wps中使用啦!

C:\Users\Administrator\AppData\Roaming\kingsoft\wps\startup

生成完毕!祝您发财!

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐