Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,

My project is to generate word documents which is in the server. If I click the generate button, a word document will be generated where the file will be copied to a new document.

The issue I am facing here is , when i click generate and if some one else is also trying to use the portal to generate a document, then they ll get a message like "The file is already opened by someone else". and if they open after a mins, then they ll be able to copy the file..
This is the code i used ..
C#
public void UpdateTOC(string file1, string file2)
     {
         using (var mainDoc = WordprocessingDocument.Open(file1, false))
         using (var resultDoc = WordprocessingDocument.Create(file2, WordprocessingDocumentType.Document))
         {
             // copy parts from source document to new document
             foreach (var part in mainDoc.Parts) resultDoc.AddPart(part.OpenXmlPart, part.RelationshipId);
             // perform replacements in resultDoc.MainDocumentPart   // ... }
         }

     }


Now, I don want that message to display. Is there anyother option to overcome such an issue.. Any other code snippets would also be helpfull..

Please help,
Thanks,
Anusha.
Posted
Updated 26-Sep-12 20:01pm
v2

1 solution

On server's because they are multi-user it is best to use unique names for you file when you are creating them so you don't get these error messages.

The easiest way is to use Guid.NewGuid().ToString() for a filename.
 
Share this answer
 
Comments
Anusha Sridhar 27-Sep-12 2:38am    
hi, thanks for the reply.. Can u pls help me with the code snippet.
Thanku.
Mehdi Gholam 27-Sep-12 2:44am    
Just use the red statement above as your filename.
Anusha Sridhar 27-Sep-12 3:33am    
I can understand what you are saying.. But I have the base document which is same. If a person clicks generate, then the document will be saved with the base document name along with their ID.. Is it possible to write this code using thread??
Mehdi Gholam 27-Sep-12 3:45am    
Copy the template file to the Guid.NewGuid() filename then operate on that file.

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