0

I have got an application which worked fine when saving just as a .doc file. I have since tried to get it so the program will save as both .doc and .pdf, this is done using the WordXP unit.

procedure TDocCreator.CloseDocument(ASaveAs : string);
var
  LFileName : OleVariant;
begin
  LFileName := ASaveAs;
  FDoc.SaveAs(LFileName, EmptyParam,EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
     EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
  LFileName := ChangeFileExt(ASaveAs, '.pdf');
  FDoc.SaveAs(LFileName,PDFFileFormat,EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
     EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
  FWordApp.NormalTemplate.Saved := true;
  FWordApp.Quit(True);
  FWordApp.Disconnect;
end; 

If I delete the second saveas the code works fine. The 'EOleException Command Failed' exception is not popping up every time a document is saved but the point in which the break occurs is when the SaveAs is for the .pdf file.

Note I only have 2 saving procedures available SaveAs(Used above) and SaveAs2000. Also I am away from a machine which can compile delphi.

If I have missed anything or need to clear anything up I will be able to respond, however I will not be able to test any code changes till Tuesday.

Thank you.

Edit: Okay the program is being remote debugged ( The machine which does all the code does not have access to office as it is on a VM). The application goes through a set of templates which get mailmerged, once done the saving process begins. So I did 2 documents (This process is done in a for loop). This worked fine. I then tried to do 5 on the third file it brought up the 'Command Failed' error. Which then doesn't close word and cancels the remaining process. When looking at LFileName its contents were '???'. This is sometimes foreign characters. Though it still manages to make the .doc file with the correct filenam. This filename is set up outside the procedure if that makes any difference 'ASaveAs'. Another thing is that the file is not being opened using any other applications. It does the whole process using the word application the program opens.

Edward
  • 25
  • 5
  • How is PDFFileFormat defined? Which version of Word is installed? – David Heffernan Apr 03 '15 at 17:33
  • @DavidHeffernan PDFFileFormat is a Constant set to 17. Word 2010, for me however I can not guarantee which version of word will be used, I think it will be 2010 for anyone using the application but I am not 100% sure. – Edward Apr 03 '15 at 18:32
  • It relies on an add-in for Office 97. Please find out which version is running when it fails. – David Heffernan Apr 03 '15 at 18:33
  • No one using the application would be using office 97. The earliest version would be 2007. But to return to the question it is office 2010 which is failing. – Edward Apr 03 '15 at 19:36
  • Oh, what am I saying? Office 2007 I meant not 97. – David Heffernan Apr 03 '15 at 19:39
  • What add-on would it require out of curiosity? In the event I need to make sure the other systems are able to run the application in the event they are not running office 2010? – Edward Apr 03 '15 at 19:44
  • The save as pdf add on. Optional for 2007, built in thereafter. – David Heffernan Apr 03 '15 at 19:46
  • I will double check what version of office will be running and if they are using 2007 I will make sure the add-on is installed. However the 'Command Failed' error will still pop-up I think as the system I am using to test the program is running office 2010. – Edward Apr 05 '15 at 06:38
  • Is the pdf file open in another program? – David Heffernan Apr 05 '15 at 07:23
  • Not entirely sure of what you mean. The file which is opened is a .doc file which has some text replacements occur. This file is then attempting to save as the .doc and .pdf files. It is defaulted to open in a different program but will also open in word 'IF' the file saves. – Edward Apr 05 '15 at 13:08
  • The file might be locked if another process (eg pdf viewer) has it open. Anyway, I think you could need to get better diagnostics. – David Heffernan Apr 05 '15 at 14:04
  • Some form of change of luck... I added a 'Sleep(1000)' before the pdf save and I managed to get 5 docs saved then 7, will try a couple of bigger ones. If this solves the problem I don't know why so would love an explanation on that. Will get back to here once I finished the bigger saves. Edit: Spoke to soon it happened again. on a variant of 7 documents, however it was the last document to be completed. – Edward Apr 07 '15 at 08:15
  • Increased the sleep timer to 2000ms and not seen the Command Failed error since. Gone up to 10 documents to be mailmerged and it seems to have gone file. It might be like @DavidHeffernan stated, word probably hadn't finished saving the .doc file before moving onto the .pdf file. Though I can not say for sure, like before I will do more tests but it seems a bit more reliable to say the least. – Edward Apr 07 '15 at 08:42

1 Answers1

0
  LFileName := ASaveAs;
  try
    try
      FDoc.SaveAs(LFileName,EmptyParam, EmptyParam, EmptyParam, ...);
    except
      on E : Exception do
        ShowMessage(E.ClassName + ' error creating document');
    end;
    try
      if FileExists(LFileName) then
      begin
        Sleep(5000);
        LFileName := ChangeFileExt(ASaveAs, '.pdf');
        FDoc.SaveAs(LFileName,PDFFileFormat,EmptyParam, EmptyParam, ...;
      end;
    except
      on E : Exception do
        ShowMessage(E.ClassName + ' error creating PDF');
    end;
  finally
    FWordApp.Quit(True);
    FWordApp.Disconnect;
  end;

Could be better and I am still unsure of the try and except but it works without crashing thanks to a tactical sleep. Could be better and would love it to but for now this seems to be an okay solution. Thanks David Heffernan for pointing out the file was probably in use.

Edward
  • 25
  • 5