Hi
I am getting error - Object variable or with blockvariable not set
picoriginal.Picture = LoadPicture("c:\image.jpg")
Dim picoriginal As PictureBox
Call SaveImageFromAPI(apiUrl, UsrName, Pword, tno, picoriginal)
Public Sub SaveImageFromAPI(apiUrl As String, UserName As String, Pwd As String, TrNo As String, picoriginal As PictureBox)
picoriginal.Picture = LoadPicture("c:\image.jpg")
Thanks
Thanks
Sangeetha SPosted Mar 21, 2025, 5:47 AM
The error "Object variable or With block variable not set" typically occurs when you try to use an object that hasn't been properly initialized. In your code, the
picoriginalobject needs to be initialized before you can use it.Here's how you can modify your code to ensure
picoriginalis properly initialized:In this code:
Set picoriginal = New PictureBoxinitializes thepicoriginalobject before passing it to theSaveImageFromAPIsubroutine.Sophia CarterPosted Mar 20, 2025, 1:24 PM
The "Object variable or with block variable not set" error typically occurs in Visual Basic when you are trying to access properties or methods of an object that has not been initialized or set to an instance. In your provided code snippet, the issue lies in the `picoriginal As PictureBox` declaration in the beginning, where you declare `picoriginal` as type `PictureBox` but do not assign an actual `PictureBox` object to it before trying to access its properties.
To resolve this error, you need to ensure that `picoriginal` is properly initialized with a `PictureBox` object before accessing its properties like `Picture`. In your specific code, you should create a new instance of `PictureBox` and assign it to `picoriginal` before calling the `SaveImageFromAPI` function. Here's an example of how you can modify your code:
By initializing `picoriginal` as a new `PictureBox`, you ensure that it is no longer a null object, and you should be able to set properties like `Picture` without encountering the "Object variable or with block variable not set" error.
This correction should help you resolve the error you are facing. If you encounter any further issues or have additional questions, feel free to ask!