Help with Excel and Interop

C

cduden

I'm linking an excel workbook programmatically to a powerpoint presentation
via an add-in written in C#. When opening the presentation, I check the
document to see if it is the latest version, refresh the object if it isn't,
and powerpoint spawns an Excel process that never goes away, which
effectively prevents a user from viewing the object in Excel. I get "file is
already open" errors. I close ppt, and the excel process hangs about.

I am using ReleaseComObject, GC.Collect, and GC.WaitForPendingFinalizers to
release any references I have to a Shape, LinkFormat, Slide, Presentation,
etc.

Any ideas other than horrible hacks(I have those well in hand) to get rid of
that process?

Thanks
CMD
 
S

Siew Moi Khor [MS]

Hi,
Could you try calling GC.Collect() followed by GC.WaitForPendingFinalizers()
*twice* like below and see if it helps:

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

Siew Moi
 
C

cduden

Hello Siew, we did try this and it didn't resolve the issue.

Couple of other related things:

Is there a way to determine if the hosting application (excel) is in
application mode or whether it is in OLE server mode and if it is in OLE
Server mode is there a way to prevent an Office Addin from loading
programmattically?

When I call "Update" on a linkformat object it never actually updates
anything -- so I attempted to manually update by setting the sourcefullname
== to the sourcefullname in order to refresh and this causes a problem. The
OLE server that the document link is pointed to throws an error saying that
there cannot be two instances of the same document open which in turn causes
Excel to hang. Is there a way to force an update or to get the Update
method to work? This is Office 2k so I can't work with PIA's (grrr) but I
still need to get this working.
 
M

Matthew

CMD,

Next time, code might be helpful.

Make sure you Release all intermediate / temporary objects. Compare
the following in VB.NET to see what I mean:

Option Explicit On
Option Strict On

Imports interop.Excel
Imports System.Runtime.InteropServices.Marshal

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Test1() 'doesn't close Excel instantly
Test2() 'closes Excel instantly
End Sub

Private Sub Test1()

Dim excelApp As New interop.Excel.Application()
Dim excelBook As interop.Excel.Workbook

excelBook = excelApp.Workbooks.Add
MsgBox(excelBook.Name)

excelBook.Close(False)
ReleaseComObject(excelBook)
excelBook = Nothing

excelApp.Quit()
ReleaseComObject(excelApp)
excelApp = Nothing

End Sub

Private Sub Test2()

Dim excelApp As New interop.Excel.Application()
Dim excelBook As interop.Excel.Workbook
Dim excelBooks As interop.Excel.Workbooks

excelBooks = excelApp.Workbooks
excelBook = excelBooks.Add
MsgBox(excelBook.Name)

ReleaseComObject(excelBooks)
excelBooks = Nothing

excelBook.Close(False)
ReleaseComObject(excelBook)
excelBook = Nothing

excelApp.Quit()
ReleaseComObject(excelApp)
excelApp = Nothing

End Sub

End Class

Seeya
Matthew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top