Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-02-2023, 05:05 PM
Milqn Vuk Milqn Vuk is offline Word macro api call Windows 10 Word macro api call Office 2016
Novice
Word macro api call
 
Join Date: Jan 2023
Posts: 13
Milqn Vuk is on a distinguished road
Default Word macro api call

Hello,

I want to connect with an outside API service. I try to make an API cole with the following code below:

Code:
Function GT(prompt As String) As String
  ' I Replace MY_API_KEY with my actual API key
  Dim apiKey As String: apiKey = "API_KEY"
  Dim apiEndpoint As String: apiEndpoint = "https://api.url.com/v1/"
  
  Dim params As String: params = "prompt=" & prompt & "&max_tokens=2048&model=text-davinci-002&stop=.!?"
  
  Dim apiCall As String: apiCall = apiEndpoint & "?" & params
  
  Dim xmlHttp As Object: Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
  
  xmlHttp.Open "POST", apiCall, False
  xmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  xmlHttp.setRequestHeader "Authorization", "Bearer " & apiKey
  
  xmlHttp.send
  
  Dim response As String: response = xmlHttp.rText
  
  Dim responseObject As Object: Set responseObject = JsonConverter.ParseJson(response)
  Dim generatedText As String: generatedText = responseObject("data")(0)("text")
  
  GT = generatedText
End Function
I get the following error: "Compile error" with explanation "Invalid use of new keyword"
Debuging:
Set responseObject = JsonConverter.ParseJson(response)

The error "Set responseObject = JsonConverter.ParseJson(response)" typically indicates that the JsonConverter object or the ParseJson method is not recognized but I think that my API call is not right since I didn't manage to make any successfull API requests. I see this on API endpoint usage to which I have access.

Also, I have added these libraries and references in my project:JSONConverter, Microsoft Scripting Runtime library and Microsoft XML, v6.0 library.

Could anyone give me any advice how to properly call an API?

Would be much appropriated!


Thanks

Last edited by macropod; 01-09-2023 at 04:00 PM. Reason: Added code tags
Reply With Quote
  #2  
Old 01-04-2023, 11:27 AM
Milqn Vuk Milqn Vuk is offline Word macro api call Windows 10 Word macro api call Office 2016
Novice
Word macro api call
 
Join Date: Jan 2023
Posts: 13
Milqn Vuk is on a distinguished road
Default

Can someone offer a kind advice?
Reply With Quote
  #3  
Old 01-05-2023, 04:57 PM
Guessed's Avatar
Guessed Guessed is offline Word macro api call Windows 10 Word macro api call Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,161
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I can't get to your error because the path provided is not valid for me. I presume this is the apiEndpoint although it could be the params or a non-existent Reference.

What References (besides the Scripting Runtime) need to be set to run your code?
Do you have an example path or JSON file that others can test?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 01-05-2023, 05:28 PM
Milqn Vuk Milqn Vuk is offline Word macro api call Windows 10 Word macro api call Office 2016
Novice
Word macro api call
 
Join Date: Jan 2023
Posts: 13
Milqn Vuk is on a distinguished road
Default

Thanks for helping me.

I will try to give your prompt reply to all your questions.

The endpoint to which I am trying to connect is "https://api.openai.com/v1/texts/generations"
I suppose I thought that I will simplify my Issue for reproduction with adding non-existing endpoint, but seems that is not the case.

You can sign up for an OpenAI API key at the following link: https://beta.openai.com/signup.

Regarding your references question, I think I have included all necessary, but could be mistaken.

I don't have any JSON file for testing, doing this first time too.

An example path might look something like this:

The user selects some text in a Word document and runs the VBA macro.

The VBA macro sends a POST request to the OpenAI Vinci API endpoint, along with a prompt that instructs the API to generate a summary of the selected text.

The OpenAI Vinci API receives the request and processes it using the specified model.
The API returns a response with the generated summary in JSON format.

The VBA macro parses the JSON response and extracts the generated summary.

The VBA macro inserts the generated summary into the Word document at the current cursor position.
Reply With Quote
Reply

Tags
api, office 2016, vba word

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to call a common vba macro on a commandbutton ? josh2008 Word VBA 2 02-08-2019 02:02 AM
Find word then call signature macro Dave T Word VBA 4 03-28-2016 11:18 PM
Word macro api call Word 2013: Hotkey to call a macro arjfca Word 2 12-04-2014 12:53 AM
Word macro api call Call Macro from Input Box String Paulsh Word VBA 1 11-01-2011 03:15 AM
Word macro api call How do I call/execute an Excel macro from Access 2003 KramerJ Excel 2 05-11-2009 11:33 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:04 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft