procedure TfrmWord.btnOleVariantClick(Sender: TObject);
var
WordApp, WordDoc, WordParagraph, WordRange: OleVariant;
Template, NewTemplate, documenttype: OleVariant;
begin
try
WordApp := GetActiveOleObject('Word.Application');
except
try
WordApp := CreateOleObject('word.application');
except
ShowMessage('打开Word文件时出错!');
exit;
end;
end;
WordApp.Visible := True;
template := ExtractFilePath(Application.ExeName) + 'Normal.dot';
newtemplate := false;
documenttype := emptyparam; //默认参数
visible := true;
WordDoc := WordApp.Documents.Add(template, newtemplate, documenttype, Visible);
WordParagraph := WordApp.activedocument.paragraphs.add; //新增一行
WordRange := WordParagraph.range;
WordRange.Collapse(wdCollapseEnd);
WordRange.text := 'M';
WordRange.Font.Size := '10';
WordRange.Font.Name := '宋体';
WordRange.Collapse(wdCollapseEnd);
WordRange.text := '3';
WordRange.Font.Size := '10';
WordRange.Font.Name := '宋体';
WordRange.Font.Superscript := True;
WordRange.Collapse(wdCollapseEnd);
end;
记得use ComObj