← 返回目录

步骤3:设置VB模块

第 3 / 11 卷

步骤3:设置VB模块

选择【开发工具】

图 5-1
图 5-1

选择【VB编辑器】

图 5-2
图 5-2

Normal选项下,插入-模块:

图 5-3
图 5-3

这里以添加阿里云百练的千问2.5大模型为例:

1Function CallDeepSeekAPI(api_key As String, inputText As String) As String

2Dim API As String

3DimSendTxtAsString
4DimHttpAsObject
5Dimstatus_codeAsInteger

"https://dashscope.aliyuncs.com/compatib le-mode/v1/chat/completions"

9SendTxt = "{""model"": ""qwen-max- 0125"", ""messages"":

[{""role"":""system"", ""content"":""请根据用户的问题给出详细的解答。""},

{""role"":""user"", ""content"":""" & inputText & """}]}"

10 11Set Http =

CreateObject("MSXML2.XMLHTTP")

12With Http 13.Open "POST", API, False

14.setRequestHeader "Content- Type", "application/json"

15.setRequestHeader "Authorization", "Bearer " & api_key

16.send SendTxt

17status_code=.Status
18response=.responseText
19EndWith

20 21' 弹出窗口显示 API 响应(调试用)

22' MsgBox "API Response: " & response, vbInformation, "Debug Info"

23

24Ifstatus_code=200Then
25CallDeepSeekAPI=response
26Else
27CallDeepSeekAPI="Error:"&

status_code & " - " & response

31End Function 32

33Sub qwen2_5()

34Dimapi_keyAsString
35DiminputTextAsString
36DimresponseAsString
37DimregexAsObject
38DimmatchesAsObject
39DimoriginalSelectionAsObject

40 41

42api_key = "填写你自己的API-KEY"'填写你自己的API-KEY

43If api_key = "" Then 44MsgBox "Please enter the API

key."

45Exit Sub 46ElseIf Selection.Type <>

wdSelectionNormal Then

47MsgBox"Pleaseselecttext."
48ExitSub
49EndIf

50 51' 保存原始选中的文本

52Set originalSelection = Selection.Range.Duplicate

53 54inputText =

Replace(Replace(Replace(Replace(Replace( Selection.Text, "\", "\\"), vbCrLf, ""),

vbCr, ""), vbLf, ""), Chr(34), "\""")

56 57If Left(response, 5) <> "Error" Then

58Set regex = CreateObject("VBScript.RegExp")

59With regex

60.Global=True
61.MultiLine=True
62.IgnoreCase=False
63.Pattern="""content"":""

(.*?)"""

64End With 65Set matches =

regex.Execute(response)

66If matches.Count > 0 Then 67response =

matches(0).SubMatches(0)

68' 处理换行符和特殊字符 69response = Replace(response,

"\n\n", vbNewLine)' 双换行替换为真实的单换行

70response = Replace(response, "\n", vbNewLine)' 单换行替换为真实的换行

71response = Replace(Replace(response, """",

Chr(34)), """", Chr(34))

72 73' 取消选中原始文本

74Selection.Collapse Direction:=wdCollapseEnd

76' 将内容插入到选中文字的下一行

78Selection.TypeText Text:=response

79

80'将光标移回原来选中文本的末尾
81originalSelection.Select
82Else
83MsgBox"FailedtoparseAPI

response.", vbExclamation 84End If

85Else 86MsgBox response, vbCritical

87End If 88End Sub

89

API-KEY需要从如下网址获取:https://bailian.console.aliyun.co m/

注意:在获取key之前,需要注册、并完成个人实名认证。

图 9-1
图 9-1
全站分卷索引 · 11 卷
  1. 1DeepSeek+WPS的配置与使用
  2. 2步骤2:配置WPS
  3. 3步骤3:设置VB模块
  4. 4步骤4:添加模块(AI助手)
  5. 5步骤5:测试
  6. 6步骤6:创建模板
  7. 7方式2:office-AI助手
  8. 8步骤2:wps内设置
  9. 9步骤3:交互测试
  10. 10补充1:wps中添加受信任的加载项
  11. 11补充2:OfficeAI助手使用文档