VS2005 vb.net DLL - interop EXCEL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Delano

    VS2005 vb.net DLL - interop EXCEL

    Hello All

    I am working a on web application created in Visual Studio 2005 VB.
    It has to do some work with Excel files, so I created a DLL in VS2005.

    My development machine is XP SP3.
    When my web app runs it opens a few excel files and fills certain locations
    and then the excel spreadsheet is calculated.
    (there are many calculations)

    To ensure I was closing only the files I had opened during this process I
    use code like this:

    Dim ClassName As String = "XLMain"
    Dim XLNumbers As Int16
    Dim WindowHandle As Int32
    Dim ReturnVal As Int32
    Const WM_QUIT = &H12

    For XLNumbers = 12 To 17
    WindowHandle = FindWindow(Clas sName, "Microsoft Excel - ROI_" &
    Request.QuerySt ring("ROIHeader ID") & "_" & CStr(XLNumbers) & ".xls")
    If WindowHandle Then
    ReturnVal = PostMessage(Win dowHandle, WM_QUIT, 0, 0)
    End If
    Next

    WindowHandle = FindWindow(Clas sName, "Microsoft Excel - ROI_" &
    Request.QuerySt ring("ROIHeader ID") & "_ALL.xls")
    If WindowHandle Then
    ReturnVal = PostMessage(Win dowHandle, WM_QUIT, 0, 0)
    End If

    This works perfectly on my development machine.

    The deployment machine is Windows Server 2003.
    None of the excel files close using the code above. The only thing that
    works there is using something like this:

    ' check for any EXCEL processes that stuck
    Dim objWMIcimv2 = GetObject("winm gmts:" _
    & "{impersonation Level=impersona te}!\\.\root\ci mv2")
    'Connect to CIMV2 Namespace

    Dim objList = objWMIcimv2.Exe cQuery _
    ("select * from win32_process where name='EXCEL.EXE '") 'Find the
    process to terminate

    Dim objProcess As Object
    Dim intRslt As Int16
    For Each objProcess In objList
    intRslt = objProcess.Term inate 'Terminates a process and all of
    its threads.
    Next

    Now while this removes the excel files in memory, it removes ALL of them.
    As this is meant to work with a web site and more then 1 (but maybe 5 total
    concurrent users) person may be using the site, this isn't a viable
    solution.

    The files are opened like this:
    (there is a reference to excel in the DLL project)

    Dim xlAppl As Excel.Applicati on
    Dim xlSheet As Excel.Worksheet

    xlAppl = New Excel.Applicati on
    xlAppl.Workbook s.Open(strFileN ame)
    xlAppl.Workbook s(1).Activate()

    strSheetName = "Input Sheet"
    xlSheet = xlAppl.Sheets(s trSheetName)

    *** code fills in some values

    xlAppl.Workbook s(1).Save()
    xlAppl.Quit()

    xlAppl = Nothing

    Some files are left open using this code, which lead me to find something
    that allows me to clean up after this.

    Is it possible to target process that have certain file names open and shut
    them down, in windows server 2003?
    Maybe it would just be better for the web app to fill a queue of excel files
    that need processing .. and have a desktop application running under a
    username?
    (though they really require feed back of the changes the new calculations
    made .. which are saved to a database)

    Any ideas about getting excel to actually close when its asked to?

    Thanks



Working...