excel strange behavior

ElieAintabi

New member
Joined
Oct 6, 2008
Messages
3
Programming Experience
1-3
Excel.Application / RunningObjectTable

I have a project where I create a new Excel.Application (version 11) and write to it. The problem is, after creating the excel.application if a user tries to open a previously saved Excel file (from their desktop for instance) then my excel.application's visibility is set to true automatically, which reveals my table and I do not want that.

I was told this is because my excel.application is the first entry in the RunningObjectTable, so my question is how do I implement RevokeActiveObject to get my object out of the ROT if I never used RegisterActiveObject?


VB.NET:
Option Explicit On
Imports System.Runtime.InteropServices.ComTypes
Module Module1

      Private Declare Function CreateBindCtx Lib "ole32" ( _
      ByVal dwReserved As Integer, ByRef pBindCtx As IBindCtx) As Integer

      Private Declare Sub RevokeActiveObject Lib "oleaut32" ( _
      ByVal dwregister As Long, ByRef pvReserved As Long)

      Private Declare Unicode Function CLSIDFromProgID Lib "ole32" ( _
      ByVal lpszProgId As String, ByRef pclsid As Guid) As Integer
public sub test()
         Dim excelObj As New Microsoft.Office.Interop.Excel.Application
            excelObj.Workbooks.Add()
            excelObj.Workbooks(1).Worksheets.Add()
            excelObj.Workbooks(1).Worksheets(1).Cells(1, 1) = "a"
     ...
     Dim bindctx As IBindCtx
            CreateBindCtx(0, bindctx)
            Dim clsid As Guid
            Dim hresult As Integer = CLSIDFromProgID("Excel.Application", clsid)
            Dim pprot As IRunningObjectTable
            bindctx.GetRunningObjectTable(pprot)
            Dim ppenumMoniker As IEnumMoniker
            pprot.EnumRunning(ppenumMoniker)
            Dim rgelt(0) As IMoniker
            Dim pceltFetched As Integer
            Do While ppenumMoniker.Next(rgelt.Length, rgelt, pceltFetched) = 0
                  Dim ppszDisplayName As String
                  rgelt(0).GetDisplayName(bindctx, Nothing, ppszDisplayName)
                  If (ppszDisplayName = "Book2" Or ppszDisplayName = "Book1") Then
                        'RemoveActiveObject(excelObj, vnROTID)
                        Console.WriteLine("var is {0}", excelObj.ToString)
                        Console.WriteLine("guid of excel is {0}", clsid.ToString("B").ToUpper())
                  End If
            Loop
            'excelObj.Visible = True
      End Sub
 

ElieAintabi

New member
Joined
Oct 6, 2008
Messages
3
Programming Experience
1-3
Ok so Im going to repeat my last post and ignore everything about the ROT; maybe someone has a different workaround.

I am making an application which creates a new Excel.Application.

The problem; if someone opens an existing Excel workbook after I create my Excel.Application, it's visibility is set to true. Is there a way to keep my visibility false?
 

ElieAintabi

New member
Joined
Oct 6, 2008
Messages
3
Programming Experience
1-3
For those utterly interested, all you have to do is excelObj.IgnoreRemoteRequests = True

this will cause a second instance of excel to be opened on file open or through start menu.
 

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Um..

I'm not sure if this is the same between 2003 and 2007, but with 2003, what I've always done is:

VB.NET:
private _app as Excel.Application

public sub StartApp()
  _app = New Excel.Application
  _app.Visible = False
end sub

from there you can utilize _app.Open() and the excel app never becomes visible, but like I said that is 2003, i don't know if it is the same.
 
Top Bottom