Free Support Forum - aspose.com

Error opening template file

We're evaluating the Aspose.Word product and it's been working great, but we have encountered an error every so often that seems as if the template file we are using is locked and can't be accessed. Here is the exception error we are getting:

The process cannot access the file “\Invoices\Templates\Blank.dot” because it is being used by another process.

Here is the code that is opening the file. It has the clone command in it because I thought this would help in keeping the template file available, but it didn't help:

Public Class CNetAsposeWord<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Private doc As Document

Private m_Bookmarks As BookmarkCollection

Public Sub New()

End Sub

Public Sub CrateNewDoc_FromTemplate(ByVal FileName As String, ByVal TemplateName As String)

Dim srcDoc As New Document

srcDoc = New Document(TemplateName, LoadFormat.Doc, String.Empty)

doc = srcDoc.Clone()

doc.IsTemplate = False

srcDoc = Nothing

End Sub

Public Sub OpenAspose_ExistingFile(ByVal fileName As String)

doc = New Document(fileName, LoadFormat.Doc, String.Empty)

doc.IsTemplate = False

End Sub

Public Sub Save(ByVal FileName As String)

doc.Save(FileName)

End Sub

End Class

Any help you can offer is greatly appreciated!

Frank


This message was posted using Page2Forum from Cloning a Document - Aspose.Words for .NET and Java

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. This error occurs if document is already opened by another program like MS Word or OpenOffice. Please make sure that template is not opened by another program.

Best regards.

Alexey...thanks for the quick response.

We are pretty sure that Word does not have the template open since the location of the newly created file is in a different location. Is there a specific way that we should destroy the template instance in Aspose.word once we clone the document from the template file so that we can guarantee that the template is no longer in use?

Thank you for additional information. Aspose.Words doesn’t use file during processing, Aspose.Words use file only during opening. As workaround you can try reading your template into stream and then use in your application. For example see the following code.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Dim fileBytes As Byte() = IO.File.ReadAllBytes("template.dot")

Dim docStream As IO.MemoryStream = New IO.MemoryStream(fileBytes)

Dim document As Document = New Document(docStream)

Best regards.

ok - we will implement it and let you know

thanks