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.
VB.NET
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 SubThanks

Sophia CarterRank #4,018 · 371 reputationPosted Mar 19, 2025, 6:13 AM EDT
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:
Public Function RetrieveImageFromAPI(apiUrl As String, portNo As Integer) As StdPicture Dim objHP As Object Set objHP = CreateObject("MSXML2.XMLHTTP") Dim username As String Dim password As String Dim url As String url = "http://" & apiUrl & ":" & portNo & "/ISAPI/streaming/channels/01/picture" username = "admin" password = "12345." objHP.Open "GET", url, 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 Set RetrieveImageFromAPI = img End FunctionWith 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.