Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NET
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 ..
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 26 Sep '12 - 18:53
Edited 26 Sep '12 - 20:01


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.
  Permalink  
Comments
Anusha Sridhar - 27 Sep '12 - 2:38
hi, thanks for the reply.. Can u pls help me with the code snippet. Thanku.
Mehdi Gholam - 27 Sep '12 - 2:44
Just use the red statement above as your filename.
Anusha Sridhar - 27 Sep '12 - 3:33
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:45
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 355
1 Sergey Alexandrovich Kryukov 338
2 Arun Vasu 315
3 Maciej Los 208
4 Aarti Meswania 180
0 Sergey Alexandrovich Kryukov 9,755
1 OriginalGriff 7,549
2 CPallini 4,018
3 Rohan Leuva 3,362
4 Maciej Los 2,951


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid