ChatGPT 憑藉其先進的文本生成能力,可以徹底改變您與 Microsoft Word 的互動方式,提升您的工作效率和學習體驗。本文提供了一份詳細的分步指南,幫助您將 ChatGPT 與 Microsoft Word 整合。此外,文章還展示了如何在 Word 中使用 ChatGPT 的實例,展現了這一強大組合的實際好處。
在本節中,我們將探討如何使用 VBA 代碼將 ChatGPT 整合到 Microsoft Word 中。這種整合有望革新文檔的創建和編輯,將 ChatGPT 的人工智能功能與 Word 的功能相結合。
步驟 1:將 VBA 代碼添加到 Word 文檔中
打開一個新的空白 Word 文件,然後按下 ALT + F11 鍵以打開 Microsoft Visual Basic for Applications 窗口。
然後,點擊 插入 > 模塊,複製並將以下代碼粘貼到打開的空白模塊中: VBA 代碼:將 ChatGPT 整合到 Word 中
Sub ChatGPT()
'Updateby Extendoffice
Dim status_code As Integer
Dim response As String
OPENAI = "https://api.openai.com/v1/chat/completions"
api_key = "sk-***************************** "
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
SendTxt = Replace(Replace(Replace(Replace(Selection.text, vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), Chr(39))
SendTxt = "{""model"": ""gpt-3.5-turbo"", ""messages"": [{""role"":""system"", ""content"":""You are a Word assistant""} ,{""role"":""user"", ""content"":""" & SendTxt & """}]}"
Set Http = CreateObject("MSXML2.XMLHTTP")
With Http
.Open "POST", OPENAI, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "Bearer " & api_key
.send SendTxt
status_code = .Status
response = .responseText
End With
If status_code = 200 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, "\n", vbCrLf), "\""", Chr(34))
Selection.Range.InsertAfter vbNewLine & response
End If
Else
Debug.Print response
End If
Set Http = Nothing
End Sub
注意事項:
在上述代碼中,您應該用自己的密鑰替換 API 密鑰 “sk-*****************************”。
解鎖 Microsoft Word 中的人工智能力量,Kutools AI 助手現已集成 ChatGPT!轉變您的寫作體驗,能夠以多種風格重寫內容,輕鬆生成定制內容,並即刻總結冗長文檔。借助 Kutools AI 助手,提升您的生產力並釋放創造力,讓每一個字都發揮作用。簡化您的工作流程,節省時間,實現無與倫比的寫作卓越。立即升級到帶有 ChatGPT 和 Kutools AI 助手的未來文檔編輯吧!