Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I have to write a code in C#.NET which uses MS Office 2010 library and takes input as .dot file and convert it to .dotm or .docm. Please help me out.
Posted
Comments
Richard MacCutchan 22-Oct-15 4:03am    
Help with what? And surely it would be simpler to use MS Word to do the job for you.
rahulsri19 22-Oct-15 4:13am    
Hey...I tried with this code:

string dotFileName = @"C:\MyFile.dot";
string docmFileName = @"C:\Test.docm";
_Application myWordApp = new Application();
_Document myDoc = myWordApp.Documents.Open(FileName: dotFileName, ReadOnly: true);
myDoc.SaveAs2(FileName: docmFileName, FileFormat: WdSaveFormat.wdFormatTemplate, CompatibilityMode: WdCompatibilityMode.wdWord2010);

myDoc.Close();
myWordApp.Quit();

But its not working. Gives run time error. This code works fine when we run to convert from doc to docx and set WdSaveFormat.wdFormatDocumentDefault. I played with different options but failed.
Richard MacCutchan 22-Oct-15 4:57am    
Gives run time error
What error?
rahulsri19 22-Oct-15 5:29am    
ComException was unhandled
Incompatible file type and file extension
Richard MacCutchan 22-Oct-15 5:49am    
OK, so now you know what is wrong.

A .dot is a template file. You do not convert templates to documents. You use a template to create a new document and the template does not change.

See the Add method of Documents, https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documents.add.aspx[^]. Notice how you pass in the name of the template to use for your new document.
 
Share this answer
 
If you're able to create document nased on template, all you need to do now is to use Document.SaveAs method[^] with proper FileFormat (WdSaveFormat)[^] as an argument of method.
 
Share this answer
 
Comments
rahulsri19 22-Oct-15 10:47am    
Can you please provide me sample code?
Maciej Los 22-Oct-15 14:37pm    
Is there something unclear? SaveAs takes no. of parameters, which you have to pass into it. Try!
rahulsri19 26-Oct-15 7:02am    
I used below code but still the file generated giving error: "Word was unable to read this document. It may be corrupt."

oDoc.SaveAs(FileName: dotmFileName, FileFormat: WdSaveFormat.wdFormatFlatXMLTemplateMacroEnabled);
Got the solution:

string docFileName = @"C:\MyFolder\Test.dot";
string dotmFileName = @"C:\MyFolder\Test.dotm";
_Application myWordApp = new Application();
_Document myDoc = myWordApp.Documents.Open(FileName: docFileName, ReadOnly: true);


myDoc.SaveAs2(FileName: dotmFileName, FileFormat:WdSaveFormat.wdFormatXMLTemplateMacroEnabled);
myDoc.Close();
myWordApp.Quit();
 
Share this answer
 

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