[Solved] Cannot create ActiveX component in Vb.net | Wave the world

[Solved] Cannot create ActiveX component in Vb.net

Hi Friends,
                
Today I'll tell you how to solve "ActiveX component can't create object" Exception in VB.net. You can use the same logic in any other application also.

So I'm getting the this exception when I try to create a obect of  Internet Explorer. When creting the object for first time, it works fine, I can see the given link in the browser, but for second time if I again create the object I got this error.

Here is the code :

Dim browser As Object = CreateObject("InternetExplorer.Application")
browser = CreateObject("InternetExplorer.Application")
And I'm getting the Cannot create ActiveX component error.




So I searched and find a solution, and it is working like charm. Here is the solution.

Dim browser As Object = CreateObject("InternetExplorer.Application")
Try
   browser = CreateObject("InternetExplorer.Application")
Catch ex As Exception
    For Each explorer_process As Process In Process.GetProcesses 
        If String.Compare(explorer_process.ProcessName, "iexplore", True) = 0 Then 
            Try 
                explorer_process.Kill() 
            Catch exp As Exception 
                Exit For 
            End Try 
        End If 
    Next 
    browser = Nothing     
    browser = CreateObject("InternetExplorer.Application")
End Try

Enjoy :)


8 comments:

  1. thanks bro solved my problem

    ReplyDelete
  2. out of box logic great

    ReplyDelete
  3. It errors on TheBrowser because it is not defined

    ReplyDelete
    Replies
    1. Thanks for pointing out the typo error. I have updated the code.

      Delete
  4. That's great stuff! Thank you very much :)

    ReplyDelete
  5. Wouldn't it be better to call GetObject() first, and only call CreateObject() if there isn't an instance already running?

    ReplyDelete
    Replies
    1. Problem with the getObject is that it is already running with old data.

      Delete

 

Pro

About