Search code examples
c#powershellms-accessdynamicobject

How to look if system.__comobject is open before I run that object?


I have this dynamic Object to get the system.__comobject from Access:

   dynamic app = Marshal.GetActiveObject("Access.Application");

And now I open a form like this:

    app.Run("gbOpenDataEditPart", "ediQuali.170013662.Wawi");

and that is working. It opens the right form from Access. Now what I want is to loop if this exactly already open. I thought maybe there is a method like if(IsOpen), but it isn't open. Is there another way to loop if this is open?

And I tried it with Powershell script with that but there is the Window not there too:

$accessApp = [System.Runtime.InteropServices.Marshal]::GetActiveObject("Access.Application")

$forms = $accessApp.CurrentProject.AllForms

foreach ($form in $forms) {
    Write-Host "Form name: $($form.Name)"
}

Example1

I can see my Application which I want to look if it is already open or not but when I look through AllForms with that:

CurrentProject.AllForms("HPGebote").IsLoaded

or

CurrentProject.AllForms("ediQuali.170013662.Wawi").IsLoaded

then there is no form which has the name I am searching for.

And no matter which Formular I will open in my Access:

Access

He will always detect frmSysAuskunft and thats It no matter how many Forms I will have Open he will always just detect one with always the same name


Solution

  • Ok, I just add this:

    using Microsoft.VisualBasic;

    And now in my code I can do this:

    button to open form, button to close the form, and button to test/check if form is open.

        private void button1_Click(object sender, EventArgs e)
        {
            dynamic app = Interaction.GetObject(Class: "Access.Application");
            app.DoCmd.Openform("tblHotelsA");
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            dynamic app = Interaction.GetObject(Class: "Access.Application");
    
            bool IsOpen = app.CurrentProject.AllForms("tblHotelsA").IsLoaded;
            MessageBox.Show($"Status of form tblHotelsA \n Open:{IsOpen.ToString()}");
        }
    
        private void button3_Click(object sender, EventArgs e)
        {
            dynamic app = Interaction.GetObject(Class: "Access.Application");
            app.DoCmd.Close(2,"tblHotelsA");
    
        }
    

    So, the result is this:

    enter image description here