Hello all,
I'm a newbie of .NET world and sorry for my english.
I'm trying to do the "Lab2: Adding Functionality to Word Document Solutions in Visual Studio .NET 2003" and I have a problem about this code fragment:
"Private Sub ThisApplication_DocumentBeforeClose(ByVal Doc As Microsoft.Office.Interop.Word.Document, ByRef Cancel As Boolean) Handles ThisApplication.DocumentBeforeClose
If Doc Is ThisDocument And Doc.Type = _
Word.WdDocumentType.wdTypeDocument Then
If Doc.Saved Then
' the document is already saved; allow the document
' to close without prompting the user
.....
Else ...."
When I run the project, the condition "If Doc.Saved" results always true, also when the document is produced for the first time. Maybe is a setting of my Word environment (I have Office 2003 professional in italian)
Thanks for your help
Mauro

VSTO lab2: Doc.Saved always true
Heathcliff
Hi Cindy,
it works with Me.InnerObject!
Thank you very much!
Best regards,
Mauro
sasman
Hi George,
unfortunately I can only use italian version of Office 11 (company policies...).
But now, as I wrote to CIndy Meister, Doc.Saved works properly after I've installed VSTO2005.
Thanks and regards,
Mauro
REDLINE
Hi Cindy
this is the error message:
"'ThisDocument' is a type and cannot be used as an expression"
I tried also to replace "ThisDocument" with "Me", because I'm in ThisDocument Class. In this case, I don't get an error, but the statement "If Doc Is Me" results false, I don't understand why.
In the meantime, I will try to install the update fot both VSTO versions.
Thanks and regards,
Mauro
The Werewolf
Hi Cindy,
Thanks for your answers.
1. When I normally use Word, it works correctly on my PC.
2. The good news ... I installed VSTO2005 and I tried to "translate" the code from VSTO2003. Now "....Doc.Saved" works properly (I have only a question: why the statement "Doc Is ThisDocument" in ThisApplication_DocumentBeforeClose of ThisDocument Class give me an error )
The bad news... After I've installed VSTO2005, this lab doesn't work in VSTO2003, because when I run the project, Word starts in safe-mode.
Thans and regards,
Mauro
Fahad Hussain
Thanks for your help.
I just tried typing some text in the document and then close without saving, but Word doesn't ask me if I want to save.
I put a breakpoint on that code ("If Doc.Saved Then....")
and I can see that "Doc.Saved" is always true.
This is the whole code for "before_close" event:
Private
Sub ThisApplication_DocumentBeforeClose(ByVal Doc As Microsoft.Office.Interop.Word.Document, ByRef Cancel As Boolean) Handles ThisApplication.DocumentBeforeCloseIf Doc Is ThisDocument And Doc.Type = _
Word.WdDocumentType.wdTypeDocument Then
If Doc.Saved Then
' the document is already saved; allow the document
' to close without prompting the user
da.Close()
da = Nothing
Else
' the document is not saved; prompt the user
' to confirm the close
Dim ret As DialogResult = MessageBox.Show( _
"Are you sure you want to close without" & _
" saving this document ", _
"ProductCatalog", MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Error)
If ret = DialogResult.Yes Then
Doc.Saved = True
da.Close()
da = Nothing
Else
Cancel = True
End If
End If
End If
End Sub
Thanks,
Mauro
p.s. I will try to install VSTO 2005
lodjikz
Hi Mauro,
Welcome to world of VSTO :-) I just want to point out that the labs you are looking at are target VSTO 2003. The newer version of VSTO is 2005. Almost all of what you learn in the labs is applicable to .NET and Office customization in general.
I think you will find the following book useful for learning VSTO and office development: http://www.amazon.com/gp/product/0321334884/104-1276230-5515917 v=glance&n=283155
As for your specific question ...
I dont't think its a setting for Word. Most likely there are no changes in the document to save. You can just try typing some text in the document and then close Word without saving.
In this case the Doc.Saved property should be false and you should see the message box asking you for confirmation.
Hopefully this is helpful.
Sovman
Hi Mauro
Unfortunately, I don't know the answers to these "off the top of my head" and I'm just before "disappearing" for a week or so. It would probably be best for you to take these two questions and make a new post for each, so that "everyone" sees them. It's possible that people who know an answer won't read this deeply in a conversation that began with another topic.
For the Doc question, in VSTO 2005 I'd try: Me.InnerObject. Something else you could try is comparing doc.FullName with Me.FullName (FullName returns the entire document path.)
Sharjeel.Ahmed
I tried on English version of Office11 and the Doc.Saved returns true when the doc is not changed, and returns false after you make it dirty (by typing).
Could you please double-check your solution
Thanks,
George
Tegs
Hi Cindy,
about the office update:
I installed the update as you suggested to me, but I still have the problem with Word and VSTO2003.
This is the behaviour of Word, with more details:
when I run the project Word appear and immediately disappear, and if I try to run one more time Word ask to me to start in safe mode.
Thanks and regards,
Mauro
Joel C
Hi Mauro
"2. The good news ... I installed VSTO2005 and I tried to "translate" the code from VSTO2003. Now "....Doc.Saved" works properly (I have only a question: why the statement "Doc Is ThisDocument" in ThisApplication_DocumentBeforeClose of ThisDocument Class give me an error )"
What's the error
"The bad news... After I've installed VSTO2005, this lab doesn't work in VSTO2003, because when I run the project, Word starts in safe-mode""
I think there's an update you have to install for Office 11 to enable both VSTO versions to run on a single machine. See if this helps:
http://www.microsoft.com/downloads/details.aspx familyid=1B0BFB35-C252-43CC-8A2A-6A64D6AC4670&displaylang=en
Chris Durkin
Hi Mauro
1. On your development machine. If you open a document "manually" in Word, type some text, then File/Close: do you get prompted to save it The question is whether the problem you're seeing is in VSTO, or in Word. You may have a virus or another addin in Word that's causing the behavior you report.
2. When using an event that provides a document object, you should manipulate that object (in this case Doc), rather than use your own. Instead of da.Close, Doc.Close