← 返回目录

步骤3:设置VB模块

第 3 / 10 卷

图 4-1
图 4-1

选中右边两项,并确认。

步骤3:设置VB模块

图 4-2
图 4-2

选择【插入】-【模块】,将如下代码填写到右侧。

使用DeepSeek-V3模型(下图以硅基流动平台为例):

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

2Dim API As String

3DimSendTxtAsString
4DimHttpAsObject
5Dimstatus_codeAsInteger
6DimresponseAsString

pletions"

9SendTxt = "{""model"": ""deepseekai/DeepSeek-V3"", ""messages"":

[{""role"":""system"", ""content"":""You are a Word

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

""stream"": false}"

10'想用R1模型,就把上面的model的 deepseek-ai/DeepSeek-V3换成deepseek-

ai/DeepSeek-R1 11

12Set Http = CreateObject("MSXML2.XMLHTTP")

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

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

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

17.send SendTxt

18status_code=.Status
19response=.responseText
20EndWith
21Ifstatus_code=200Then
22CallDeepSeekAPI=response
23Else
24CallDeepSeekAPI="Error:"&

status_code & " - " & response 25End If

30Sub DeepSeekV3()

31Dimapi_keyAsString
32DiminputTextAsString
33DimresponseAsString
34DimregexAsObject
35DimmatchesAsObject
36DimoriginalSelectionAsRange

37 38' API Key

39api_key = ""'填写你自己的API KEY' 40If api_key = "" Then

41MsgBox "Please enter the API key.", vbExclamation

42Exit Sub 43End If

44 45' 检查是否有选中文本

46If Selection.Type <> wdSelectionNormal Then

47MsgBox "Please select text.", vbExclamation

48Exit Sub 49End If

50 51' 保存原始选区

52Set originalSelection = Selection.Range.Duplicate

53 54' 处理特殊字符

55inputText = Selection.Text 56inputText = Replace(inputText, "\",

58inputText = Replace(inputText, vbCr, " ")

59inputText = Replace(inputText, vbLf, " ")

60inputText = Replace(inputText, """", "\""") ' 转义双引号

61 62' 发送 API 请求

63response = CallDeepSeekAPI(api_key, inputText)

64 65' 处理 API 响应

66If Left(response, 5) <> "Error"

Then 67' 解析 JSON

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

69With regex

70.Global=True
71.MultiLine=True
72.IgnoreCase=False
73.Pattern="""content"":""

(.*?)""" ' 匹配 JSON 的 "content" 字段 74End With

75Set matches = regex.Execute(response)

76 77If matches.Count > 0 Then

78' 提取 API 响应的文本内容 79response =

matches(0).SubMatches(0)

Replace(response, "\n", vbCrLf)

83response = Replace(response, "\\", "\") ' 处理

JSON 里的反斜杠 84response =

Replace(response, "&", "") ' 过滤 `&`,防止意外符号

85 86' 让光标移动到文档末尾,防止覆盖

已有内容 87Selection.Collapse

Direction:=wdCollapseEnd 88Selection.TypeParagraph

89Selection.TypeText Text:=response

90 91' 将光标移回原来选中文本的末尾

92originalSelection.Select 93

94Else 95MsgBox "Failed to parse API

response.", vbExclamation 96End If

97Else 98MsgBox response, vbCritical

99End If 100End Sub

101

使用DeepSeek-R1模型(下图以硅基流动平台为例):

4Dim Http As Object 5Dim status_code As Integer

6Dim response As String 7

8API = "https://api.siliconflow.cn/v1/chat/com

pletions"

9SendTxt = "{""model"": ""deepseekai/DeepSeek-R1"", ""messages"":

[{""role"":""system"", ""content"":""You are a Word

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

""stream"": false}"

10'想用V3模型,就把上面的model的 deepseek-ai/DeepSeek-R1换成deepseek-

ai/DeepSeek-V3 11

12Set Http = CreateObject("MSXML2.XMLHTTP")

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

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

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

17.send SendTxt

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

24 25

26Ifstatus_code=200Then
27CallDeepSeekAPI=response
28Else
29CallDeepSeekAPI="Error:"&

status_code & " - " & response 30End If

31 32Set Http = Nothing

33End Function 34

35Sub DeepSeekV3()

36Dimapi_keyAsString
37DiminputTextAsString
38DimresponseAsString
39DimregexAsObject
40DimmatchesAsObject
41DimoriginalSelectionAsRange

42 43' API Key

44api_key = ""'填写你自己的API KEY' 45If api_key = "" Then

46MsgBox "Please enter the API key.", vbExclamation

47Exit Sub 48End If

49 50' 检查是否有选中文本

51If Selection.Type <> wdSelectionNormal Then

53Exit Sub 54End If

55 56' 保存原始选区

57Set originalSelection = Selection.Range.Duplicate

58 59' 处理特殊字符

60inputText = Selection.Text 61inputText = Replace(inputText, "\",

"\\")

62inputText = Replace(inputText, vbCrLf, " ")

63inputText = Replace(inputText, vbCr, " ")

64inputText = Replace(inputText, vbLf, " ")

65inputText = Replace(inputText, """", "\""") ' 转义双引号

66 67' 发送 API 请求

68response = CallDeepSeekAPI(api_key, inputText)

69 70' 处理 API 响应

71If Left(response, 5) <> "Error"

Then 72' 解析 JSON

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

74With regex

78.Pattern = """content"":""

(.*?)""" ' 匹配 JSON 的 "content" 字段 79End With

80Set matches = regex.Execute(response)

81 82If matches.Count > 0 Then

83' 提取 API 响应的文本内容 84response =

matches(0).SubMatches(0)

85 86' 处理转义字符

87response = Replace(response, "\n", vbCrLf)

88response = Replace(response, "\\", "\") ' 处理

JSON 里的反斜杠 89response =

Replace(response, "&", "") ' 过滤 `&`,防止意外符号

90 91' 让光标移动到文档末尾,防止覆盖

已有内容 92Selection.Collapse

Direction:=wdCollapseEnd 93Selection.TypeParagraph

94Selection.TypeText Text:=response

96' 将光标移回原来选中文本的末尾 97originalSelection.Select

response.", vbExclamation 101End If

102Else 103MsgBox response, vbCritical

104End If 105End Sub

使用千问2.5模型(以阿里云百炼平台为例):

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

2Dim API As String

3DimSendTxtAsString
4DimHttpAsObject
5Dimstatus_codeAsInteger
6DimresponseAsString

7 8API =

"https://dashscope.aliyuncs.com/compati ble-mode/v1/chat/completions"

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

[{""role"":""system"", ""content"":""You are a Word

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

""stream"": false}"

10 11Set Http =

CreateObject("MSXML2.XMLHTTP")

12With Http

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

16.send SendTxt

17status_code=.Status
18response=.responseText
19EndWith
20Ifstatus_code=200Then
21CallDeepSeekAPI=response
22Else
23CallDeepSeekAPI="Error:"&

status_code & " - " & response 24End If

25 26Set Http = Nothing

27End Function 28

29Sub DeepSeekV3()

30Dimapi_keyAsString
31DiminputTextAsString
32DimresponseAsString
33DimregexAsObject
34DimmatchesAsObject
35DimoriginalSelectionAsRange

36 37' API Key

38api_key = ""'填写你自己的API KEY' 39If api_key = "" Then

40MsgBox "Please enter the API key.", vbExclamation

41Exit Sub 42End If

wdSelectionNormal Then 46MsgBox "Please select text.",

vbExclamation 47Exit Sub

48End If 49

50' 保存原始选区 51Set originalSelection =

Selection.Range.Duplicate 52

53' 处理特殊字符 54inputText = Selection.Text

55inputText = Replace(inputText, "\", "\\")

56inputText = Replace(inputText, vbCrLf, " ")

57inputText = Replace(inputText, vbCr, " ")

58inputText = Replace(inputText, vbLf, " ")

59inputText = Replace(inputText, """", "\""") ' 转义双引号

60 61' 发送 API 请求

62response = CallDeepSeekAPI(api_key, inputText)

63 64' 处理 API 响应

65If Left(response, 5) <> "Error"

Then 66' 解析 JSON

68With regex

69.Global=True
70.MultiLine=True
71.IgnoreCase=False
72.Pattern="""content"":""

(.*?)""" ' 匹配 JSON 的 "content" 字段 73End With

74Set matches = regex.Execute(response)

75 76If matches.Count > 0 Then

77' 提取 API 响应的文本内容 78response =

matches(0).SubMatches(0)

79 80' 处理转义字符

81response = Replace(response, "\n", vbCrLf)

82response = Replace(response, "\\", "\") ' 处理

JSON 里的反斜杠 83response =

Replace(response, "&", "") ' 过滤 `&`,防止意外符号

84 85' 让光标移动到文档末尾,防止覆盖

已有内容 86Selection.Collapse

Direction:=wdCollapseEnd 87Selection.TypeParagraph

88Selection.TypeText

91originalSelection.Select 92

93Else 94MsgBox "Failed to parse API

response.", vbExclamation 95End If

96Else 97MsgBox response, vbCritical

98End If 99End Sub

100

从如下链接获取:

https://bailian.console.aliyun.com

填写以后:

图 17-1
图 17-1

保存:

全站分卷索引 · 10 卷
  1. 1DeepSeek+Word的配置与使用
  2. 2步骤2:添加启用所有宏
  3. 3步骤3:设置VB模块
  4. 4步骤4:添加到开发工具
  5. 5步骤5:测试
  6. 6步骤6:创建模板
  7. 7方式2:office-AI助手
  8. 8步骤2:word内设置
  9. 9步骤3:交互测试
  10. 10补充:OfficeAI助手使用文档