如何将word文档存入数据库再取出来然后在程序中打开! ( 积分: 100 )

  • 主题发起人 主题发起人 bbtwo33
  • 开始时间 开始时间
以前做毕设的时候也想个这个问题
处理文档多的时候,速度不行
 
现在向数据库里存取没有问题了!谢谢app2001 结帖时一定给分!

现在还有个问题就是 怎么把word嵌在我的程序里 来显示word文档 而且还需要把工具拦都显示出来 因为要支持在程序里编辑文档的功能!听说OLE方法能够做到 我看了不少例子都是弹出单独的word窗口 好象就是直接调用了word 程序 这样不行啊!


哪位高人做过 请指点下!谢谢了!
 
我再给你一个仙女,但是不要问我怎么搞,自己动手试去吧,成了以后,把全部结果贴出来,也不枉我一次又一次的送仙女给你了

如下代码:
OleContainer1.CreateObjectFromFile( 'C:/WIN95/Profiles/Rainy/Desktop/文档规范.DOC', false );
OleContainer1.doVerb( ovShow ); //激活Ole对象,使之可编辑

一段代码,这段代码是在一个ole容器中打开一个word文档,
并得到这个文档的页数
unit1.pas
----------------------------
代码:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, OleCtnrs,OleServer, word2000,ComCtrls,ComObj,activex,
  Menus;
type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Edit1: TEdit;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    OleContainer1: TOleContainer;
    MainMenu1: TMainMenu;
    est1: TMenuItem;
    OPen1: TMenuItem;
    Close1: TMenuItem;
    GetPages1: TMenuItem;
    procedure Button1Click(Sender: TObject);
    procedure OPen1Click(Sender: TObject);
    procedure GetPages1Click(Sender: TObject);
    procedure Close1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function Off_GetOleObject (const ClassName: string; var blCreate: Boolean): IDispatch;
{ 如果Word没有运行,则启动它,并返回一个自动化对象; 如果Word已经启动,就返回正在运行的实例的自动化对象。}
var  ClassID: TGUID;
     Unknown: IUnknown;
begin
  blCreate := False;
  ClassID := ProgIDToClassID(ClassName);
  if Succeeded (GetActiveObject(ClassID, nil, Unknown)) then
    OleCheck (Unknown.QueryInterface (IDispatch, Result))
  else
  begin
    blCreate := True;
    Result := CreateOleObject (ClassName);
  end;
end;

function Off_GetWordDocPages(v_Parawd: OleVariant; v_Paradocs: OleVariant;
    strFName: string; var strTitle: string;blOpenFile: Boolean): Boolean;
var v_Docs, v_wd, v_Name, v_Range, v_Text: OleVariant;
    blWd, blDoc, blCreate: Boolean;
begin
  blWd := False;
  blDoc := False;
  v_Docs := unassigned;
  v_wd := unassigned;
  v_Range := unassigned;
  v_Text := unassigned;
  v_Name := strFName;

  try
    try
      {连接word}
      if (VarIsEmpty(v_ParaWd)) then
      begin
        v_Wd := Off_GetOleObject('Word.Application', blCreate);
        {打开文档}
        v_Docs := v_Wd.Documents.Open(FileName:=v_Name, ReadOnly:=True,AddToRecentFiles:=False);
        blWd := True;
        blDoc := True;
        //FWordApplication := v_Wd;
      end
      else
      begin
        v_Wd := v_ParaWd;
        if (not VarIsEmpty(v_ParaDocs)) then
          v_Docs := v_ParaDocs
        else
        begin
          if (not blOpenFile) and (v_Wd.Documents.Count > 0) then
            v_Docs := v_Wd.ActiveDocument
          else
          begin
             v_Docs := v_Wd.Documents.Open(FileName:=v_Name, ReadOnly:=True, AddToRecentFiles:=False);
             blDoc := True;
          end;
        end;
      end;

      {提取Title}
      v_Text := v_Docs.BuiltInDocumentProperties[wdPropertyPages];
      strTitle := v_Text;
      if (blDoc) and (not VarIsEmpty(v_Docs)) then
         v_Docs.Close(wdDoNotSaveChanges, wdOriginalDocumentFormat, EmptyParam);
      Result := True;
    except
      Result := False;
    end;
  finally
    if (blWd) and (blCreate) and (not VarIsEmpty(v_Wd)) then
      v_Wd.quit;
    v_Wd := unassigned;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if (not OpenDialog1.Execute) then Exit;
  Edit1.Text := OpenDialog1.FileName;
end;

procedure TForm1.OPen1Click(Sender: TObject);
begin
  OleContainer1.CreateObjectFromFile(Edit1.Text, False);
end;

procedure TForm1.GetPages1Click(Sender: TObject);
var v_obj, v_wd: OleVariant;
    s: string;
begin
  v_obj := OleContainer1.OleObject;
  v_wd := v_obj.Application;
  Off_GetWordDocPages(v_wd, unassigned, Edit1.Text, s, False);
  Application.MessageBox(PChar('共有' + s + '页'), '提 示', MB_OK + MB_ICONINFORMATION);
end;

procedure TForm1.Close1Click(Sender: TObject);
begin
  OleContainer1.Close;
end;

end.
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部