Reputation: 85
I am writing a Windows Explorer extension that opens an existing .docx file, edits a few things, and saves it to a different path and name. It successfully opens the file, but when it hits the document.SaveAs()
command it just opens the Save As dialog with the original filename and path, rather than saving to the new path.
Here is the code section that handles the doc.
var application = new Microsoft.Office.Interop.Word.Application();
application.Visible = true;
var document = application.Documents.Open(
FileName: reviewRecordTemplatePath,
AddToRecentFiles: false,
Visible: true);
// Do stuff...
document.SaveAs(FileName: reviewRecordPath);
Upvotes: 0
Views: 2459
Reputation: 49397
Try to specify the filename and fileformat for the SaveAs2 method of the Document class.
Upvotes: 0
Reputation: 765
Why not create a copy of the document before you open it?
For example, lets say you're going to modify c:\docs\myfile.docx using your script, and you want to save the modified version as c:\newdocs\newfile.docx.
Before opening Word in your script, do a File.Move("c:\docs\myfile.docx", "c:\newdocs\newfile.docx"); and then open the target file, make your changes, and do a document.Save();
Upvotes: 2