Community discussion forum

Internet explorer control

Tags: csharp Iran
  • 1 year ago

    Hi,

    I am trying to write a program that will control internet explorer as part of another program but I am writing this part first.  I got a sample code from online but I am missing a using directive? Here are the errors that I got when I compiled the program.

    error CS0246: The type or namespace name 'InternetExplorer' could not be found (are you missing a using directive or an assembly reference?)

    error CS0103: The name 'NSLibStatic' does not exist in the current context

    This is the code that I ran.

    using System;
    using System.Collections.Generic;
    using System.Text;


    namespace IECONNECTION
    {
        class Program
        {
            public static bool IEOpenOnURL(string sURL)
            {
                InternetExplorer oIE = (InternetExplorer)NSLibStatic.COMCreateObject
                                                                  ("InternetExplorer.Application");

                if (oIE != null)
                {
                    object oEmpty = String.Empty;
                    object oURL = sURL;
                    oIE.Visible = false;
                    oIE.Navigate2(ref oURL, ref oEmpty, ref oEmpty, ref oEmpty, ref oEmpty);
                }
                return true;
            }
            static void Main(string[] args)
            {
            }
        }
    }


  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 1 year ago

     I'm down to one error now.

    error CS0117: 'IEConnection.NSLibStatic' does not contain a definition for 'COMCreateObject'

    namespace IEConnection
    {

        class NSLibStatic
        {
            
            private static bool IEOpenOnURL(string sURL)
            {
                InternetExplorer oIE = (InternetExplorer)NSLibStatic.COMCreateObject
                                                                  ("InternetExplorer.Application");

                if (oIE != null)
                {
                    object oEmpty = String.Empty;
                    object oURL = sURL;
                    oIE.Visible = false;
                    oIE.Navigate2(ref oURL, ref oEmpty, ref oEmpty, ref oEmpty, ref oEmpty);
                }
                return true;
            }
            static void Main(string[] args)
            {
            }
        }
    }

  • 3 months ago

    You've probably figured this out by now, but I came across the same problem today.

    Forget about calling your class NSLibStatic, what you need is the COMCreateObject method, which is:

    /// <summary>
    /// Creates a COM object given it's ProgID.
    /// </summary>
    /// <param name="sProgID">The ProgID to create</param>
    /// <returns>The newly created object, or null on failure.</returns>
    public static object COMCreateObject (string sProgID)
    {
        // We get the type using just the ProgID
        Type oType = Type.GetTypeFromProgID (sProgID);
        if (oType != null)
        {     
           return Activator.CreateInstance(oType);
        }
       
        return null;
    }

    I found it here: http://www.novicksoftware.com/TipsAndTricks/tip-csharp-create-com-object-by-progid.htm

Post a reply

Enter your message below

Sign in or Join us (it's free).