Hi,
when we try to open a document which is already opened by Microsoft Word, we get an error ‘Cannot open…used by another process…’ in Aspose.Words
In the ‘old days’, when we used word automation, there was no error when opening a document, when it is opened by Word.
What can we do?
Thanks in advance
Manfred
We are using the following code
-----------------------------------------------------------------------------------------
Public Function mtAsposeTextOpen(ByVal sDateiName As String) As String
'fDokument = New Aspose.Words.Document(sDateiName)
' Open the stream. Read only access is enough for Aspose.Words to load a document.
Dim lStream As Stream
Try
lStream = File.OpenRead(sDateiName)
Catch ex As Exception
Return ex.Message
End Try
' Load the entire document into memory.
fDokument = New Aspose.Words.Document(lStream)
' You can close the stream now, it is no longer needed because the document is in memory.
lStream.Close()
Return ""
End Function
-----------------------------------------------------------------------------------------------
Hi Manfred,
Thanks for your inquiry.
Things differ a little using Aspose.Words, you are no longer opening a word document using Microsoft Word, instead you are opening the document from disk using the Aspose.Words component which is completly separate application (it does not use automation at all). This means when you have the document opened in MS WORD, Aspose.Words cannot access it as the file is locked by Microsoft Word.
To get around this you can load the document using a stream and specify FileShare parameter which allows you to open the file even when it’s locked for editing. Please see the code below:
// The specified file can be opened in MS Word when opening it with Aspose.Words here.
string fileName = @"C:\Users\philiph\AppData\Local\Temp\tmp77b2.doc";
// Open the document.
FileStream docStream = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// Create Aspose.Words Document from stream
Document doc = new Document(docStream);
// Close stream
docStream.Close();
// Do something with the document.
// Save the document.
doc.Save(@"C:\Temp\out.doc");
I hope, this will help.
Best regards.
Awais,
thanks for your answer. This is the solution I was searching for.
Best regards
Manfred