Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MSWord VSTO button functionality

Posted on 2006-10-27
6
Medium Priority
?
798 Views
Last Modified: 2008-03-10
I have created a new button which I added to the toolbar in MSWord with C#.  Now the functionality I want to give that button is when someone clicks the button I created it will act as if they clicked save and then act as if they had clicked the open button on the toolbar how do I do this?

ex..

 void OnClick_Button(Office.CommandBarButton ctrl, ref bool cancel)
        {
               .....
               .....
               //what can I put to get same effect as the save button being hit
              //I've tried calling event handlers directly but it hasn't given me the same effect as hitting the button.
             //I've tried calling wordApplication.activeDocument.Save() but it produces the error "command failed" and gives no other description
              .....
        }
0
Comment
Question by:ICPooreman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 11

Accepted Solution

by:
Mihai Stancescu earned 2000 total points
ID: 17829908
What version of Office are you using?
Because if it is 2000 you don't have a chance... :(

You can try:
...
using Word = Microsoft.Interop.Office.Word;
...

Word.Document doc = wordApplication.activeDocument;
if (!doc.Saved)
{
     doc.Save();
    // or you can call save as... to specify the file name, used for the open later...
}
doc.Close(/*here there are some ref missing.Value depending on the Office version...*/);

wordApplication.Documents.Open(filename, ref missing.Value....);

Hope this helps.
Regards,
Mishu
0
 

Author Comment

by:ICPooreman
ID: 17833922
I'm using Office 2003 so this should be possible I believe.  

I did exactly as you wrote but I still get the error "command failed" on the save() and close() calls.  What's weird about it is that if I put a try-catch block around it and ignore the errors it does save and close the documents correctly (as far as I can tell).  Should I just ignore the errors??  What could they possibly be??  I have event handlers for save and close however I've traced through them and they produce no errors.

Something important is how do I get the new fileName after calling either save() or saveAs().  If I set filename to doc.Name after save() is called the value getting passed in is Document1 (or whatever it was before) not what I save the file as, how do I get that value??  

The open works with no errors, but I need to be able to get the fileName to use it.

On a side note this isn't as important, doc.close() produces warnings do you have any idea how to make them go away.
0
 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 17834028
To get the new file name you can use the property that the Document class has : doc.FullName;

I use the same procedure as above and I didn't get any errors, this is probably because I use VSTO...

But I think you can ignore the error as long as the application continues to run normally, this error may occur because of some serialization methods used by the framework. You can try installing the SP1 for MS Office 2003... it might solve this problem...

Can you put here those warnings?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ICPooreman
ID: 17834313
doc.FullName isn't giving me the new name of what the document is being saved as I'm having the same problem as before.  


I believe the error is because C# doesn't want methods to have the same name, I don't know how to get rid of it but I'm sure there must be a way.

Warning      2      Ambiguity between method 'Microsoft.Office.Interop.Word._Document.Close(ref object, ref object, ref object)' and non-method 'Microsoft.Office.Interop.Word.DocumentEvents2_Event.Close'. Using method group.      

0
 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 17834713

You can use the filename object that you use as a parameter in the saveas method to know the file name... if you use the save as method...

The first Microsoft.Office.Interop.Word._Document.Close(ref object, ref object, ref object) is used by the class Word.Document, and second is a Delegate used for the event.

You can use Word.ApplicationEvents4_DocumentBeforeCloseEventHandler to execute an event when the document is about to close,
and ApplicationEvents4_DocumentBeforeSaveEventHandler to execute an event when the document is about to save...

The warning will probably disappear if you put in the using clause referring to Word interop only:
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;

Hope this helps.
Regards,
Mishu
0
 

Author Comment

by:ICPooreman
ID: 17836491
Thanks a lot the reason the filename wasn't changing was an error on my side in my save event handler.  This was quite helpful.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question