Answered by:
COM error when saving VSTO Excel worksheet

Question
-
Hi,
In a VSTO Excel worksheet-level application I tried to save
the content using either- Save()
- SaveCopyAs()
methods, but always get error like showing below:
Does this error relate to COM Interop? How should I fix it?
Bob
Saturday, March 3, 2012 11:59 AM
Answers
-
Hi Bob,
OK, I understand you situation now. I think you snippet must in the ThisWorkbook.vb. Is it right? At that please
Me.Application.ActiveWorkbook().Save()
will work.
Have a good day,
Tom
Tom Xu [MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by 许阳(无锡) Friday, March 23, 2012 5:35 AM
Tuesday, March 6, 2012 7:18 AM -
Hi Bob,
My code will work under sheet1. See it:
Public Class Sheet1 Private Sub Sheet1_Startup() Handles Me.Startup End Sub Private Sub Sheet1_Shutdown() Handles Me.Shutdown End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Me.Application.ActiveWorkbook.Save() End Sub End Class
Have a good day,
Tom
Tom Xu [MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by 许阳(无锡) Friday, March 23, 2012 5:35 AM
Tuesday, March 20, 2012 6:54 AM
All replies
-
Hi Bob,
Thanks for posting in the MSDN Forum.
Would you please tell me what's mean of "worksheet-level" application? Is it a document-level application? As far as I know there have two document-level application's templates in Visual studio, which template you used? Excel Template or Excel Workbook?
As far as I know, that Me.Application.ThisWorkbook is a instance of you document-level application. It isn't a Excel mode of the application.
It seems that you need use following statement "Me.Application.ThisWorkbook.Application.ActiveWorkbook.Save()" to save the document. The following snippet comes form a Excel Workbook application. This is a event of ribbon button click.
Imports Microsoft.Office.Tools.Ribbon Public Class Ribbon1 Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click Dim XlApp As Excel.Application = Globals.ThisWorkbook.Application XlApp.ActiveWorkbook.Save() End Sub End Class
I hope it can help you.
Have a good day,
Tom
Tom Xu [MSFT]
MSDN Community Support | Feedback to us
Monday, March 5, 2012 5:08 AM -
Hi Bob,
Thanks for posting in the MSDN Forum.
Would you please tell me what's mean of "worksheet-level" application? Is it a document-level application? As far as I know there have two document-level application's templates in Visual studio, which template you used? Excel Template or Excel Workbook?
As far as I know, that Me.Application.ThisWorkbook is a instance of you document-level application. It isn't a Excel mode of the application.
Tom,
1st, it is "document-level application" rather than "worksheet-level" application. Sorry for the confusion.
2nd, could you elaborate more on what is the difference betwen "a instance" and "Excel mode"? I don't really get that.
Bob
Monday, March 5, 2012 9:47 PM -
Tom,
Unfortunately I didn't find Me.Application.ThisWorkbook.Application.ActiveWorkbook.Save()" method work, with the same error message as in the 1st post.
Bob
Monday, March 5, 2012 9:51 PM -
Hi Bob,
OK, I understand you situation now. I think you snippet must in the ThisWorkbook.vb. Is it right? At that please
Me.Application.ActiveWorkbook().Save()
will work.
Have a good day,
Tom
Tom Xu [MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by 许阳(无锡) Friday, March 23, 2012 5:35 AM
Tuesday, March 6, 2012 7:18 AM -
Tom,
No, the code is in sheet1.vb, and it seems that
- Intellisense doesn’t even recognize the existence of “ActiveWorkbook” property
- If I manually type it in, only the five most generic methods are prompted as by Intellisense, but not any of the supposed
specificproperties/methods which should include “Save”, “SaveCopyAs” and so on.
Bob
- Edited by Bob Sun Tuesday, March 6, 2012 8:29 AM
Tuesday, March 6, 2012 8:25 AM -
Hi Bob,
My code will work under sheet1. See it:
Public Class Sheet1 Private Sub Sheet1_Startup() Handles Me.Startup End Sub Private Sub Sheet1_Shutdown() Handles Me.Shutdown End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Me.Application.ActiveWorkbook.Save() End Sub End Class
Have a good day,
Tom
Tom Xu [MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by 许阳(无锡) Friday, March 23, 2012 5:35 AM
Tuesday, March 20, 2012 6:54 AM