I need to be able to open and edit a Word template from Delphi (using Word) I can open the template OK but Word assumes it to be a document and not a template.
The problem is that I need to edit the template and not use it as template for a new document. I use the templates as part of a documenthandling system and I want the users to be able to edit the templates from my app. As it is now they have to open Word and then open the template from there and edit it - doing it from my app would be easier and more secure.
Experimental code
fWordApp: WordApplication;
TempName: OleVariant;
WordDoc: WordDocument;
TemplateFile: string;
begin
TemplateFile := Settings.Directories.RootInsert(qryTemplates.FieldByName('fldtemplate_path').AsString);
if TemplateFile <> '' then
begin
if not Assigned(fWordApp) then
begin
fWordApp := CreateOleObject('Word.Application') as WordApplication;
while fWordApp.Templates.Count = 0 do
Sleep(200); // Normal.dot must be loaded
end;
if Assigned(fWordApp) then
fWordApp.Visible := True
else
raise Exception.Create('Cannot initialize Word application');
TempName := TemplateFile;
WordDoc := fWordApp.Documents.Add(TempName, EmptyParam, wdFormatTemplate, EmptyParam);