dephi中自带的组件Wordapplication1和WordDocument1,
如何能新建一个word文档.用上面这两个组件,不用ole。

解决方案 »

  1.   

    呵呵,看看这个.对了要给分哦!
    procedure TForm1.SetFont;
    begin
     WordFont1.ConnectTo(WordDocument1.Sentences.Get_Last.Font);
     WordFont1.Underline := 2;//下划线
     WordFont1.Bold := 1;//黑体
     WordFont1.Italic := 1;//斜体
     WordFont1.Emboss := 1;//浮雕
     WordFont1.Engrave := 1;//刻雕
     WordFont1.Shadow := 1;//阴影
     WordFont1.DoubleStrikeThrough := 1;//双删除线
     WordFont1.StrikeThrough := 1;//删除线
     WordFont1.Size := 10;//字体大小
    end;Procedure TForm1.Button1Click(Sender: TObject);//打开Word并写入内容
    var
     Template,NewTemplate,ItemIndex: OleVariant;
    begin
     Try
       Template := EmptyParam;
       NewTemplate := True;
       ItemIndex := 1;
       try
         WordApplication1.Connect;
       except
         MessageDlg('可能未安装Word!',mtInformation,[mbOk],0);
         Abort;
       end;
       WordApplication1.Visible := True;
       WordApplication1.Caption := 'Delphi';   Template := EmptyParam;
       NewTemplate := False;
       WordApplication1.Documents.Add(Template,NewTemplate);
       WordDocument1.ConnectTo(WordApplication1.Documents.Item(ItemIndex));   //关闭拼写检查和语法检查
       WordApplication1.Options.CheckSpellingAsYouType := False;
       WordApplication1.Options.CheckGrammarAsYouType := False;
        
       SetFont;
       //添加内容 
       WordDocument1.Range.InsertAfter('AAAAAAAAA'+ #13);
       WordDocument1.Range.InsertAfter('BBBBBBBBb' + #13);
       WordDocument1.Range.InsertAfter(' ' + #13);
       WordDocument1.Range.InsertAfter(' ' + #13);
       WordDocument1.Range.InsertAfter(' ' + #13);
     Except
       on E: Exception do
       begin
        ShowMessage(E.Message);
        WordApplication1.Disconnect;
       end; 
     end;
    end;procedure TForm1.Button2Click(Sender: TObject);
    var        //存盘
     SaveChanges,OriginalFormat,RouteDocument: OleVariant;
     SavePath: OleVariant;
    begin
     SaveChanges := WdDoNotSaveChanges;
     OriginalFormat := UnAssigned;
     RouteDocument := UnAssigned;
     try
      SavePath := 'd:\samp.doc';
      WordDocument1.SaveAs(SavePath);
      WordDocument1.Close;
      WordDocument1.Disconnect;
      WordApplication1.Quit(SaveChanges,OriginalFormat,RouteDocument);
      WordApplication1.Disconnect;
     except
      On E: Exception do
      begin
       ShowMessage(E.Message);
       WordApplication1.Disconnect;
      end;
     end;
    end;
      

  2.   

    procedure TMainForm.BtnInsertRecordClick(Sender: TObject);
    var Template,NewTemplate,ItemIndex:OleVariant;    procedure setfont;
        begin
          WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
          if ChkBoxUnderline.checked then WordFont.Underline := 2;
          if ChkBoxBold.checked then WordFont.Bold := 1;
          if ChkBoxItalic.Checked then WordFont.Italic := 1;
          if ChkBoxEmboss.Checked then WordFont.Emboss := 1;
          if ChkBoxEngrave.checked then WordFont.Engrave := 1;
          if ChkBoxShadow.checked then WordFont.shadow := 1;
          if ChkBoxDoublestrike.checked then WordFont.DoubleStrikeThrough := 1;
          if ChkBoxStrike.checked then WordFont.StrikeThrough := 1;
          WordFont.Size := StrToInt(Size.text);
          if Fonttype.Itemindex >= 0 then
             WordFont.Name := FontType.Items[FontType.Itemindex];
        end;begin
      try
        Template := EmptyParam;
        NewTemplate := True;
        ItemIndex := 1;
        try
          Wordapplication.Connect;
        except
          MessageDlg('Word may not be installed', mtError, [mbOk], 0);
          Abort;
        end;
        Wordapplication.Visible := True;
        WordApplication.Caption := 'Delphi automation';
        {Create new document}
        Template := EmptyParam;
        NewTemplate := False;
        if ChkBoxNewDoc.Checked then
        begin
          WordApplication.Documents.Add(Template, NewTemplate);
          {Assign WordDocument component}
          WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
        end;
        {Turn Spell checking of because it takes a long time if enabled and slows down Winword}
        WordApplication.Options.CheckSpellingAsYouType := False;
        WordApplication.Options.CheckGrammarAsYouType := False;
        {Insert data}
        DBImgFishImg.CopyToClipboard;
        WordDocument.Sentences.Last.Paste;
        WordDocument.Range.InsertAfter('Common Name: ' + Table.Fields.Fields[2].AsString + #13);
        SetFont;
        WordDocument.Range.InsertAfter('Species Name:' + Table.Fields.Fields[3].AsString + #13);
        WordDocument.Range.InsertAfter('Length: ' + Table.Fields.Fields[4].AsString + #13);
        WordDocument.Range.InsertAfter(' ' + #13);
        WordDocument.Range.InsertAfter(' ' + #13);
        WordDocument.Range.InsertAfter(' ' + #13);
        BtnCloseWord.Enabled := True;
        BtnPrint.Enabled := True;
        BtnPreview.Enabled := True;
      except
        on E: Exception do
        begin
          ShowMessage(E.Message);
          WordApplication.Disconnect;
        end;
      end;
    end;
      

  3.   

    WordApplication.Documents.Add(Template, NewTemplate);
      运行时说这行参数不足什么原因?(Error:not enough actual parameters)
      

  4.   

    //先打开word,建立wordapplication1对象同word应用程序的关联
    wordapplication1.connect;
    wordapplication1.visible:=true;
    //新建文档
    var
       T1,newT1,T1Index,Type1,v1:oleVariant;
    begin
       T1:=EmptyParam;
       newT1:=emptyParam;
       type1:=combobox1.ItemIndex;
       v1:=true;
       t1Index:=1;
       Fstart:=0;
       FEnd:=0;
       wordapplication1.Documents.Add(t1,newt1,type1,v1);  //在add函数中,共有4个参数,分别表示使用模板,新建模板,新建文档及是否可见。这4个参数都是olevariant类型,并且为传址形式的函数输入的,不能直接赋值,需要声明变量后在作为参数。
      //worddocument1连接上wordapplication
       wordDocument1.ConnectTo(wordapplication1.Documents.Item(T1index));
    end;
    //具体的操作用worddocument,如
    worddocument1.save;
    worddocument1.saveas(filename);
    worddocument1.printout;
    worddocument1.Range.InsertAfter();
    //等等
    //最后是关闭
    wordapplication1.disconnect;
    wordapplication.quit;
    大致是这么一个过程,具体操作楼上说得很清楚了,如还要其他功能,可以参考一些工具书。