Chat GPT in MS Word
MS Office Word மென்பொருளில் ChatGPT Artificial Intellegence வசதியை ஒருங்கிணைக்க உதவும் கூறி குறியீடு
Code to integrate the ChatGPT Artificial Intelligence feature into MS Office Word software
MS Word – View – macro - give add to
shortcut name to your macro -click on create button- copy the highlighted code
- paste the code in the macro editor – go to tools – reference of VB editor – save – go to reference – tick Microsoft runtime –ok- save –
now type a question – right click on it – and choose “Ask ChatGPT”
Alternative Code to Integrate ChatGPT into MS Office Word
Sub chatGPT()
Dim request As Object
Dim text As String, response As String, API As String, api_key As String, DisplayText As String, error_result As String
Dim startPos As Long, status_code As Long
Dim prompt As String
Dim selectedText As Range
'API Info
API = "https://api.openai.com/v1/chat/completions"
'API Key
api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxx"
If api_key = "" Then
MsgBox "Error: API key is blank!"
Exit Sub
End If
' Prompt the user to select text in the document
If Selection.Type <> wdSelectionIP Then
prompt = Trim(Selection.text)
Set selectedText = Selection.Range
Else
MsgBox "Please select some text before running this macro."
Exit Sub
End If
'Cleaning
text = Replace(prompt, Chr(34), Chr(39))
text = Replace(text, vbLf, "")
text = Replace(text, vbCr, "")
text = Replace(text, vbCrLf, "")
' Remove selection
Selection.Collapse
'Create an HTTP request object
Set request = CreateObject("MSXML2.XMLHTTP")
With request
.Open "POST", API, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "Bearer " & api_key
.send "{""model"": ""gpt-3.5-turbo"", ""messages"": [{""content"":""" & text & """,""role"":""user""}]," _
& """temperature"": 1, ""top_p"": 0.7}"
status_code = .Status
response = .responseText
End With
'Extract content
If status_code = 200 Then
DisplayText = ExtractContent(response)
'Insert response text into Word document
selectedText.InsertAfter vbNewLine & DisplayText
Else
startPos = InStr(response, """message"": """) + Len("""message"": """)
endPos = InStr(startPos, response, """")
If startPos > Len("""message"": """) And endPos > startPos Then
DisplayText = Mid(response, startPos, endPos - startPos)
Else
DisplayText = ""
End If
'Insert error message into Word document
EDisplayText = "Error : " & DisplayText
selectedText.InsertAfter vbNewLine & EDisplayText
End If
'Clean up the object
Set request = Nothing
End Sub
Function ExtractContent(jsonString As String) As String
Dim startPos As Long
Dim endPos As Long
Dim Content As String
startPos = InStr(jsonString, """content"": """) + Len("""content"": """)
endPos = InStr(startPos, jsonString, "},") - 2
Content = Mid(jsonString, startPos, endPos - startPos)
Content = Trim(Replace(Content, "\""", Chr(34)))
Content = Replace(Content, vbCrLf, "")
Content = Replace(Content, vbLf, "")
Content = Replace(Content, vbCr, "")
Content = Replace(Content, "\n", vbCrLf)
If Right(Content, 1) = """" Then
Content = Left(Content, Len(Content) - 1)
End If
ExtractContent = Content
End Function
Procedures for using VBA code in MS Word.
- Alt+F11 will launch the VBA editor.
- To create a new module, select Insert > Module.
- Paste the VBA code inside the module.
- Change the API Key to your actual API key in api_key.
- VBA editor must be closed.
- Run the macro by hitting Alt+F8, choosing ChatGPT, and then pressing RUN.
To use ChatGPT-4, change gpt-3.5-turbo with gpt-4 in the VBA code.
.
கருத்துகள் இல்லை:
கருத்துரையிடுக