Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
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 :
C#
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
Updated 11-Apr-11 4:51am
v3

Word does not have sheet names so you are basically on the wrong track at the moment.
 
Share this answer
 
Comments
mohit`12 11-Apr-11 10:52am    
woops! my bad it was TYPO i meant word document names.
Abhinav S 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 11-Apr-11 13:09pm    
Okay thanks :)
Abhinav S 11-Apr-11 13:10pm    
You are welcome.
mohit`12 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 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[^]
 
Share this answer
 
Comments
mohit`12 11-Apr-11 10:52am    
Woops ! sorry it was TYPO i meant word documents name.
Wendelius 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 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.
Wendelius 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 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 :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900