0

When trying to save a document using the example code below it should save the document on the given location. If the file already exists, it will throw an exception. So far so good. But in a rare case the .SaveAs method blocks, and it will open the MS Word Save As Dialog (if Word is made visible, otherwise it must be activated using the task manager).

The situation only occurs on 1 PC with Windows 10 and Office 2016.

Problem is here, that I don't know when to expect a dialog, the documentation does not say anything about it (https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._document.saveas.aspx).

What are the conditions of prompting a dialog using SaveAs? In my case I never want to see this dialog. Tried both late and early binding, both same result. Is it a problem that could be fixed by code, or is it somehow a setting in Word?

procedure Example(Sender: TObject);
var
  lWord : TWordApplication;
  lDoc : TWordDocument;
begin
  try
    lWord := TWordApplication.Create(Self);
    lWord.ConnectKind := ckNewInstance;
    lWord.Disconnect;
    lWord.Connect;
    lWord.Visible := True;
    lDoc := TWordDocument.Create(lWord);
    lDoc.SaveAs('C:\Temp\test.doc');
  except
  on e: exception 
  do
    begin
    //reResults.Lines.Add(e.Message);
    //reResults.Lines.Add(e.StackTrace);
    end;
  end;

  try
    lWord.Quit;
  finally
  end; 
end;
renevondecafe
  • 129
  • 1
  • 7
  • I'm not familiar with Delphi - my expertise is in Word's object model. I notice, though, that you use the method "Create" for both the Word.Application and Word.Document objects. Assuming this maps to the VB world's "New" keyword then your problem could be coming from instantiating a document this way rather than via (again, in VB-speak) WordApplication.Documents.Add. While Word will compile "New Document" it cannot work with such an object correctly. A document should always be created/instantiated using the Documents.Add method. – Cindy Meister Oct 09 '15 at 17:10
  • 1
    Another problem could be the file name you're assigning in the SaveAs method. Natively, Word 2007 and later create Office Open XML documents (*.docx). The *.doc extension belongs to documents in the older, binary file format. Word 2007 and later REQUIRE the correct extension for the file type. If the two do not match then this could trigger the SaveAs dialog. If you want the old file type then you much also provide the FORMAT parameter (Word.WdSaveFormat enum) to the method. – Cindy Meister Oct 09 '15 at 17:13
  • In situations using Documents.Add the problem also exists, so I don't think this is the source of the problem. For saving I use in the example no parameter, for the real scenario EmptyParam. I assume both will be asserted as a default value (wdFormatDocument I guess, can't find the definitions). The other situation - with the same saving behaviour - opens a document and tries to save this on a different location. Both tested doc an docx, same result. I will try to add the appropiate formatting enum value, to find out if it affects. – renevondecafe Oct 12 '15 at 07:40
  • The default document format from Word's POV can vary from machine to machine (it's an option in the UI). The default file format as far as Word is concerned is NOT the old binary *.doc, it's the Office Open XML docx. So it's best to set the file format, specifically, especially when it's not the native docx. – Cindy Meister Oct 14 '15 at 16:20
  • I have tried using the WdSaveFormat enum, but it doesn't affect the case. The prompt dialog keeps showing up. – renevondecafe Oct 15 '15 at 09:06

0 Answers0