Hi
I am getting error - Object variable or with blockvariable not set
picoriginal.Picture = LoadPicture("c:\image.jpg")
VB.NET
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")
ThanksThanks

Sophia CarterRank #4,018 · 371 reputationPosted Mar 20, 2025, 9:24 AM EDT
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:
Dim picoriginal As New PictureBox ' Initialize the PictureBox object ' Call the function and pass the initialized PictureBox object Call SaveImageFromAPI(apiUrl, UsrName, Pword, tno, picoriginal) ' Your SaveImageFromAPI function remains the same Public Sub SaveImageFromAPI(apiUrl As String, UserName As String, Pwd As String, TrNo As String, picoriginal As PictureBox) picoriginal.Picture = LoadPicture("c:\image.jpg") End SubBy 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!
Sangeetha SRank #212 · 11.4K reputationPosted Mar 21, 2025, 1:47 AM EDT
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:Dim picoriginal As PictureBox Set picoriginal = New PictureBox ' Initialize the PictureBox object 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") End SubIn this code:
Set picoriginal = New PictureBoxinitializes thepicoriginalobject before passing it to theSaveImageFromAPIsubroutine.