w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Inserting text into new Word 2010 file not working (Delhi XE2)

It seems to me that the problem is likely that the document object WordDocument1 is not the document in which the text is added. The text is added fine, just to a different document. Here is a simple example that demonstrates how to do it:

var
  app: TWordApplication;
  doc: WordDocument;
....
app := TWordApplication.Create(nil);
try
  app.Visible := False;
  doc := app.Documents.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam);
  app.Selection.EndOf(wdStory, wdMove);
  app.Selection.InsertAfter('Hello, World!');
  app.Selection.EndOf(wdStory, wdMove);
  doc.SaveAs('C:desktopMyNewDocDup.doc', EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
  doc.SaveAs('C:desktopMyNewDocDup.pdf', 17, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam);
  doc.Close(EmptyParam, EmptyParam, EmptyParam);
  app.Quit;
finally
  app.Free;
end;

In real code you'd use the named file format constants rather than the magic number 17.

Note that NewDocument does not create a new document. Instead you need to use Documents.Add to make a new document. Note also the pain of early binding – all those EmptyParam arguments are no fun at all. If you are building a large amount of code on top of Office it pays to wrap up such messy details.

As for the documentation to the Office automation API, that can be found on MSDN: http://msdn.microsoft.com/en-us/library/office/ee861527.aspx





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.