Hi
I have below code & I want to convert it into module. 2 Parameters API & Port No will be passed.
It should return Image which is to be shown on Form.
Private Sub Command1_Click()
Dim objP As Object
Set objP = CreateObject("MSXML2.XMLHP")
Dim url As String
Dim username As String
Dim password As String
rl = "192.168.1.2:80/ISAPI/streaming/channels/01/picture"
username = "admin"
password = "12345."
objHP.Open "GET", rl, False, username, password
objHP.Send
Dim responseBody As Variant
responseBody = objHP.responseBody
' Save the image to a file
Dim outputStream As Object
Set outputStream = CreateObject("ADODB.Stream")
outputStream.Type = 1 ' Binary
outputStream.Open
outputStream.Write responseBody
outputStream.SaveToFile "C:\image.jpg", 2 ' 2: Overwrite if exists
outputStream.Close
' Dim img As StdPicture
' Set img = LoadPicture("C:\image.jpg") ' Load your image here
'
' Dim newWidth As Integer
' Dim newHeight As Integer
' newWidth = 5# * Screen.TwipsPerPixelX * 5#
' newHeight = 5 * Screen.TwipsPerPixelY * 5#
'
' Set Picture1.Picture = img
' Picture1.AutoRedraw = True
' Picture1.PaintPicture img, 0, 0, newWidth, newHeight
' Picture1.AutoRedraw = False
CropAndSave Picture1, 500, 500, 1000, 1000, "C:\image.jpg"
End Sub
Thanks
Sophia CarterPosted Mar 19, 2025, 10:13 AM
To convert your existing code into a reusable module in VBA, you can follow these steps:
1. Navigate to your Visual Basic for Applications (VBA) editor.
2. Insert a new module by clicking "Insert" in the menu and choosing "Module."
3. Copy and paste your code into the new module.
4. Modify the code to accept the API URL and Port No as parameters for flexibility.
Here's an example of how you can structure your code into a module function that returns the image:
With this module function, you can call `RetrieveImageFromAPI(apiUrl, portNo)` from any part of your code by providing the API URL and Port No as parameters. It will return the image as a `StdPicture` object.
This modular approach allows you to reuse the image retrieval functionality without duplicating code and makes it easier to maintain and manage your VBA project.