Recent

Author Topic: Problems with Word  (Read 504 times)

w click

  • Full Member
  • ***
  • Posts: 183
Problems with Word
« on: May 13, 2025, 11:10:39 am »
I've been developing a program that fires up Word, and does a few clever things like a wordcount to keep a record of progress.  It uses this:-

Code: Pascal  [Select][+][-]
  1. WordApp:=CreateOleObject('Word.Application');

It's been working fine for, I don't know, a year or two, but now it won't close the document (and therefore won't quit).

Code: Pascal  [Select][+][-]
  1.       labelinfo.caption:='Saving...'; sourcedoc.save;
  2.       labelinfo.caption:='Closing...'; sourcedoc.close;
  3.       labelinfo.caption:='Quitting...'; wordapp.quit;
  4.       labelinfo.caption:='Done. ';
  5.  

Instead, Word flashes and requires it to be quit manually without save/don't save/cancel!  (Luckily, the line before this is the sourcedoc.save, which does still work.)  I've tried putting the sourcedoc.close in a try...except, but it still locks up waiting for Word.

I suspect a Word issue, but I can't find out what it could be.  Anyone got any ideas, please.

Thaddy

  • Hero Member
  • *****
  • Posts: 17178
  • Ceterum censeo Trump esse delendam
Re: Problems with Word
« Reply #1 on: May 13, 2025, 11:17:27 am »
It may help if you do a call to OleUninitialize(). Otherwise it make still be intact/hold by the OS.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

w click

  • Full Member
  • ***
  • Posts: 183
Re: Problems with Word
« Reply #2 on: May 13, 2025, 05:00:50 pm »
It may help if you do a call to OleUninitialize(). Otherwise it make still be intact/hold by the OS.
I'm not sure how to do that and can't find anything online.  (And it doesn't explain why it worked for so long and suddenly stopped working.)

w click

  • Full Member
  • ***
  • Posts: 183
Re: Problems with Word
« Reply #3 on: May 15, 2025, 01:03:01 pm »
What would happen if I skipped the sourcedoc.close and just wordapp.quit?  Slightly terrified of testing it in case I lose the document, which is important.

loaded

  • Hero Member
  • *****
  • Posts: 866
Re: Problems with Word
« Reply #4 on: May 15, 2025, 02:17:56 pm »
Code: Pascal  [Select][+][-]
  1. uses ComObj,shlobj;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4.     function GetSpecialFolderPath(CSIDLFolder: Integer): string;
  5.     var
  6.        FilePath: array [0..MAX_PATH] of char;
  7.     begin
  8.       SHGetFolderPath(0, CSIDLFolder, 0, 0, FilePath);
  9.       Result := FilePath;
  10.     end;
  11.  
  12.   var
  13.     Word, oPara1 : Variant;
  14.     exs:widestring;
  15.   begin
  16.       exs:=UTF8Decode('Example');
  17.       Word := CreateOleObject('Word.Application');
  18.       Word.Visible := True;
  19.       Word.Documents.Add;
  20.       oPara1 := Word.ActiveDocument.Content.Paragraphs.Add;
  21.       exs:=UTF8Decode(#13+#13+#13+' Problems with Word ');
  22.       oPara1.Range.Text := exs;
  23.       oPara1.Range.Font.Name:= 'Times New Roman';
  24.       oPara1.Range.Font.Bold := True;
  25.       oPara1.Range.Font.Size := 14;
  26.       oPara1.Range.ParagraphFormat.Alignment := 1; // Center
  27.       oPara1.Range.InsertParagraphAfter();
  28.       exs:=UTF8Decode(GetSpecialFolderPath(CSIDL_DESKTOP) + '\Example.docx');
  29.       showmessage('Document saved and closed');
  30.       Word.ActiveDocument.SaveAs2(exs);
  31.       Word.ActiveDocument.close;
  32.       Word.quit;
  33.   end;
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

 

TinyPortal © 2005-2018