Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Ive tried multiple codes from stackoverflow but didnt really work for me

What I have tried:

C#
Word.Application word = null;
                Application wd;
                if (path.ToLower().EndsWith(".doc"))
                {
                    var sourceFile = new System.IO.FileInfo(path);

                    word = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");

                    var doc = word.Documents.Open(path);

                    //var document = word.Documents.Open(filename);

                    string newFileName = sourceFile.FullName.Replace(".doc", ".docx");
                    doc.SaveAs2(newFileName, Word.WdSaveFormat.wdFormatDocument,
                                     CompatibilityMode: Word.WdCompatibilityMode.wdWord2010);

                    word.ActiveDocument.Close();
                    word.Quit();
                    System.IO.FileInfo getExt = new System.IO.FileInfo(newFileName);
                    fileExt = getExt.Extension;
                    
                }
Posted
Updated 25-Jan-19 17:02pm
v2
Comments
F-ES Sitecore 25-Jan-19 12:42pm    
"Doesn't work" doesn't give anyone enough information to help you. You wouldn't phone a mechanic and say "My car doesn't work, how do I fix it?"
Kuthuparakkal 25-Jan-19 14:53pm    
Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
Document document = winword.Documents.Open(@".docPath");
//Save the document
object filename = @"c:\temp1.docx";
document.SaveAs2(ref filename);
document.Close(ref missing, ref missing, ref missing);
document = null;
winword.Quit(ref missing, ref missing, ref missing);
winword = null;

1 solution

Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
Document document = winword.Documents.Open(@".docPath");
 //Save the document
object filename = @"c:\temp1.docx";
document.SaveAs2(ref filename);
document.Close(ref missing, ref missing, ref missing);
document = null;
winword.Quit(ref missing, ref missing, ref missing);
winword = null;
 
Share this answer
 
Comments
Maciej Los 6-Feb-19 6:41am    
5ed!
Kuthuparakkal 11-Feb-19 13:07pm    
Thank you ML

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