Click here to Skip to main content
12,894,708 members (57,825 online)
Rate this:
Please Sign up or sign in to vote.
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,
Posted 26-Sep-12 18:53pm
Updated 26-Sep-12 20:01pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Anusha Sridhar 27-Sep-12 2:38am
hi, thanks for the reply.. Can u pls help me with the code snippet.
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2017
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