Reputation: 2935
I'm using the unit Word2010
to create a very simple word document.
I've written this code and everything looks fine; but in action, the saved document is always empty. Do you have any idea about why this is happening?
Here's my code:
procedure TConverter.Convert;
var
A: TWordApplication;
W: WordDocument;
begin
A := TWordApplication.Create(Nil);
try
// Create document
A.Visible := False;
W := A.Documents.Add('', False, wdNewBlankDocument, True);
// Write in document
A.Selection.StartOf(wdStory, wdMove);
A.Selection.InsertAfter('Hello World!');
A.Selection.Collapse(wdCollapseEnd);
A.Selection.EndOf(wdStory, wdMove);
// Save document
W.SaveAs(Target, wdFormatDocument, False, '', False, '', False, False, False, True, False, msoEncodingUTF8, True,
False, wdCRLF, False);
W.Close(wdDoNotSaveChanges, wdWordDocument, False);
A.Quit;
finally
A.Free;
end;
end;
Upvotes: 1
Views: 544
Reputation: 612964
One of your parameters to SaveAs
is not correct. I've not bothered to check which one yet, but the best way to deal with this is to ask for default behaviour. Do that by passing EmptyParam
:
W.SaveAs(Target, wdFormatDocument, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam);
Pretty ugly isn't it?!
On the other hand, I would argue that this is more readable than the code in the question which forces you to count beyond 5, that is beyond the number of fingers on one hand. Which is always tricky!
OK, it looks like it is parameter number 10, SaveFormsData
. You passed True
. Changing that to be False
will also result in your document's content appearing in the saved file. However, I stand by what I said above. You only really care about the first two parameters here, so use EmptyParam
for all the others and thereby request default values for these parameters.
Upvotes: 1