Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Excel programming Word , +
Hello All,
I was wondering if any of you know how to change property or Document name of Microsoft word application or specific documents from c#.
 
When i try :
 MyWord.Application aSheet;
    MyWord.Document sheet1;
    sheet1 = aSheet.Documents.Add(Missing.Value); 
    sheet1.Name = " Sheet 1";
 
it returns following error:
Error Property or indexer Microsoft.Office.Interop.Word._Document.Name; cannot be assigned to -- it is read only.
 
Thanks..
Posted 11-Apr-11 4:18am
Edited 11-Apr-11 5:51am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is a bit confusing. Sheets are used in Excel. Based on your code, you're trying to change the name of the Word document.
 
You can define the name of the (Word) document for example when saving the document. If you're trying to use Excel via Interop, try using Excel's corresponding class libraries. For more info, see: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel(v=office.14).aspx[^]
  Permalink  
Comments
mohit`12 at 11-Apr-11 10:52am
   
Woops ! sorry it was TYPO i meant word documents name.
Mika Wendelius at 11-Apr-11 11:01am
   
Ok, makes more sense now :)
 
The document gets it's name when you save it. Have a look at: http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word._document.saveas(v=office.11).aspx
mohit`12 at 11-Apr-11 13:09pm
   
okay thanks i was just looking for any other way of changing name without saving since i do not want my clients to save all files that i open. i have one more question. When i open new word application. app = new MyWord.Application(); then is there any way i can quit that if they are still open and also i will have to stop that process from task manager but i do not want to kill any other word files that are not related to this software so i just cannot killprocess(WORD.EXE) . thanks for your help.
Mika Wendelius at 11-Apr-11 13:30pm
   
You're welcome. If I inderstood your question correctly, you can use the close method and set save changes to false, see: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._document.close(VS.80).aspx. Also you can call the Quit method (and again set save changes to false), see: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._application.quit(v=Office.11).aspx.
 
However, be careful since if the Word instance contains documents that are opened by the user, you may accidentally close users documents without saving anything. This wouldn't make the user very happy (this behaviour depends on the version of Word).
mohit`12 at 11-Apr-11 13:48pm
   
that's exactly where my problem comes. User will have my application open which generates some microsoft word documents and excel sheets and then user will be printing those ONLY. so i donot want them to save them all bcz after printout files are useless for them also if they want to save it they will need proper name to save with. ( so i was trying to give those files proper name but since you said it's not possible without saving file) and now when user wants to generated next files means another files to print at that time all the old files has to be closed and word will be opening new application so WORD.EXE will open new instance in task manager and i m telling you i will be opening new application 100 times in hour so they will have 100 WORD.EXE instance in task manager.so what i am thinking to do is whenever they try to generate new files all the old files close without saving also it kills the word.exe instance but if they have another word file open outside from my application they do not have to be killed ( SO THAT's where i m stuck rightnow)
 
any idea or help would be much much appreciated. and i hope you understand my question :)
Mika Wendelius at 11-Apr-11 15:39pm
   
When you open a new instance of Word you'll have a 'handle' for the instance. So when you have done all the operations with your document, can't you simply call the Quit method I previously mentioned on that specific instance of Word (and discard all the changes)? Something like MyWord.Quit(...).
 
On the other hand if you want to speed up things, you could have the word instance open (just store the variable at proper level in your program so that you always have it) and after printing a document, call the Close method for the document. Next time you open a document, use the same instance. In this case you just have to remember to wire the ApplicationEvents3_Event_add_Quit event in case that the user closes the word so that you know that you have to create a new instance.
 
Or am I missing something?
Mika Wendelius at 11-Apr-11 15:47pm
   
Oops, first I got a message saying that the comment wasn't saved and I dodn't see it, and now it's twice :)
Mika Wendelius at 11-Apr-11 15:46pm
   
When you open a new Word instance, you'll have the 'handle' for that specific instance. So when you have finished the printing etc. can't you simply call the Quit method I mentioned earlier (and discard all the changes)? SOmething like MyWord.Quit(...)
 
On the other hand if you want to speed up things, you could open an instance of Word and store that instance into a variable, which is usable also the next time you open a document (store the variable at high enough level, window, application etc). Now when you have finished printing the document, call the Close method for that document. Next time you want to print something, use the same instance again. In this case you just have to remember to wire the ApplicationEvents3_Event_remove_Quit event in order to see if the user closes the Word instance so that you know if you have to create a new one.
 
Or am I missing something?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Word does not have sheet names so you are basically on the wrong track at the moment.
  Permalink  
Comments
mohit`12 at 11-Apr-11 10:52am
   
woops! my bad it was TYPO i meant word document names.
Abhinav S at 11-Apr-11 12:05pm
   
You wont be able to change the name of the document while it is open. Close it and then rename it using file renaming features.
mohit`12 at 11-Apr-11 13:09pm
   
Okay thanks :)
Abhinav S at 11-Apr-11 13:10pm
   
You are welcome.
mohit`12 at 11-Apr-11 13:18pm
   
i was just looking for any other way of changing name like without saving document since i do not want my clients to save all the unrequired files that i open.since i just want to open documents print them. well i have one more question. When i open new word application or excel application . app = new MyWord.Application(); then is there any way i can quit/kill/close that if they are still open and also i will have to stop that process from task manager but i do not want to kill any other word files that are not related to this software so i just cannot killprocess(WORD.EXE) . thanks for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 11 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100