⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ahword97.pas

📁 一个好的word的控件
💻 PAS
📖 第 1 页 / 共 4 页
字号:
	FComApp.Run(MacroName);
end;

procedure TWordApp.ScreenRefresh;
begin
	FComApp.ScreenRefresh;
end;

function TWordApp.GetScreenUpdating : Boolean;
begin
  GetScreenUpdating := FComApp.ScreenUpdating;
end;

procedure TWordApp.SetScreenUpdating (Value : Boolean);
begin
	FComApp.ScreenUpdating := Value;
end;

function TWordApp.GetWindowState : TOleEnum;
begin
  GetWindowState := FComApp.WindowState;
end;

procedure TWordApp.SetWindowState (Value : TOleEnum);
begin
	FComApp.WindowState := Value;
end;

{ TWordDoc }

constructor TWordDoc.CreateNewDoc(WordApp : TWordApp; Template: String);
var
  DocTemplate,
  NewTemplate : OleVariant;
begin
  Create;
  FMode := wdmCreating;
  FItemIndex := -1;
  FRanges := TList.Create;
  DocTemplate := Template;
  NewTemplate := False;
  FWordApp := WordApp;
  FComDoc := FWordApp.FComApp.Documents.Add (DocTemplate, NewTemplate);
  FItemIndex := FWordApp.NoOfDocuments;
  FWordApp.FDocuments.Add (self);
  // The following gives Word the opportunity to send events (eg open/change doc)
  // since it requests the document's Fullname.
  // It is important that the document has been added to the WordApp list so that
  // the event handler sees the document and doesn't try to create it again
  UpdateFullname;
  if Assigned (WordApp.OnOpenDocument) then WordApp.OnOpenDocument (WordApp, Self);
  FMode := wdmExisting;
end;

constructor TWordDoc.CreateOpenDoc(WordApp : TWordApp; FileName: String);
var
  ovFileName : OleVariant;
begin
  Create;
  FMode := wdmCreating;
  FItemIndex := -1;
  FRanges := TList.Create;
  ovFileName := FileName;
  FWordApp := WordApp;
  FComDoc := FWordApp.FComApp.Documents.Open (ovFileName, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                                              EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
  FItemIndex := FWordApp.NoOfDocuments;
  FWordApp.FDocuments.Add (self);
  // The following gives Word the opportunity to send events (eg open/change doc)
  // since it requests the document's Fullname.
  // It is important that the document has been added to the WordApp list so that
  // the event handler sees the document and doesn't try to create it again
  UpdateFullname;
  if Assigned (WordApp.OnOpenDocument) then WordApp.OnOpenDocument (WordApp, Self);
  FMode := wdmExisting;
end;

constructor TWordDoc.CreateFromComDoc(WordApp : TWordApp; ComDoc : _Document);
begin
  Create;
  FMode := wdmCreating;
  FItemIndex := -1;
  FWordApp := WordApp;
  FComDoc := ComDoc;
  FItemIndex := FWordApp.NoOfDocuments;
  FRanges := TList.Create;
  FWordApp.FDocuments.Add (self);
  // The following gives Word the opportunity to send events (eg open/change doc)
  // since it requests the document's Fullname.
  // It is important that the document has been added to the WordApp list so that
  // the event handler sees the document and doesn't try to create it again
  UpdateFullname;
  if Assigned (WordApp.OnOpenDocument) then WordApp.OnOpenDocument (WordApp, Self);
  FMode := wdmExisting;
end;

constructor TWordDoc.CreateFromActiveDoc(WordApp : TWordApp);
begin
  Create;
  FMode := wdmCreating;
  FItemIndex := -1;
  FWordApp := WordApp;
  FRanges := TList.Create;
  if WordApp.FComApp.Documents.Count = 0 then
    FComDoc := nil // indicates that no valid document
  else
  begin
    FComDoc := FWordApp.FComApp.ActiveDocument;
    FItemIndex := FWordApp.NoOfDocuments;
    FWordApp.FDocuments.Add (self);
    // The following gives Word the opportunity to send events (eg open/change doc)
    // since it requests the document's Fullname.
    // It is important that the document has been added to the WordApp list so that
    // the event handler sees the document and doesn't try to create it again
    UpdateFullname;
    if Assigned (WordApp.OnOpenDocument) then WordApp.OnOpenDocument (WordApp, Self);
  end;
  FMode := wdmExisting;
end;

function TWordDoc.DocStillInWord : Boolean;
// New function to check if document is still loaded in Word
// If user were to close document himself, further calls to FComDoc would
// generate an error. Therefor, you should check for the presence of the
// document before using the object, if user intervention could have occurred
// since the document was first created. This is most likely to be useful if
// you have a button on your program that closes Word or the document.
// NB if the user closes the document while your code is actively filling in
// the document, exceptions will still occur. Unless you check the document before
// every use of TWordDoc, you cannot get round this - you should warn users to leave
// well alone while your program does its magic.
var
  i : OleVariant;
  bPresent : boolean;
  docs : Documents;
begin
  bPresent := False;
  i := 1;
  docs := FWordApp.FComApp.Documents;
  while (not bPresent) and (i <= docs.Count) do
  begin
    if docs.Item (i) = FComDoc then bPresent := True;
    inc (i);
  end;
  Result := bPresent;
end;

procedure TWordDoc.FreeRangesAndRemoveDoc;
var
  i : Integer;
begin
  if Assigned (FRanges) and (FRanges.Count > 0) then
    for i := FRanges.Count - 1 downto 0 do // faster to free if we go backward
      if Assigned (FRanges [i]) then
        TWordRange (FRanges [i]).Free;
  FRanges.Free;
  if FItemIndex <> -1 then FWordApp.RemoveDoc (FItemIndex);
end;

destructor TWordDoc.Destroy;
begin
  FMode := wdmDestroying;
  FreeRangesAndRemoveDoc;
  inherited;
end;

destructor TWordDoc.CloseDoc(oeSaveChanges: TOleEnum);
var
  ovSaveChanges,
  OriginalFormat,
  RouteDocument  : OleVariant;
begin
  FMode := wdmDestroying;
  ovSaveChanges := oeSaveChanges;
  OriginalFormat := EmptyParam;
  RouteDocument := EmptyParam;
  FComDoc.Close (ovSaveChanges, OriginalFormat, RouteDocument);
  Application.ProcessMessages;
  FreeRangesAndRemoveDoc;
  inherited Destroy;
end;

function TWordDoc.GetActive : Boolean;
begin
  if FWordApp.FComApp.ActiveDocument = nil then
    GetActive := False
  else
    GetActive := (FWordApp.FComApp.ActiveDocument = FComDoc);
end;

procedure TWordDoc.SetActive(Value: Boolean);
begin
  FComDoc.Activate;
end;

procedure TWordDoc.Print;
begin
  FComDoc.PrintOut (EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                    EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                    EmptyParam, EmptyParam)
end;

procedure TWordDoc.SaveAs(Filename: String);
var
  DocName : OleVariant;
begin
  DocName := FileName;
  FComDoc.SaveAs (DocName, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                  EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
  UpdateFullname;
end;

procedure TWordDoc.UpdateFields;
begin
	FComDoc.Fields.Update;
end;

procedure TWordDoc.UpdateFullname;
begin
	FFullname := FComDoc.FullName;
end;

function TWordDoc.AddRangeFromBookMark(BookmarkName : String) : TWordRange;
var
  wr : TWordRange;
begin
  wr := TWordRange.CreateFromBookMark (Self, BookMarkName);
  AddRangeFromBookMark := wr;
end;

function TWordDoc.AddRangeFromSelection : TWordRange;
var
  wr : TWordRange;
begin
  wr := TWordRange.CreateFromSelection (Self);
  AddRangeFromSelection := wr;
end;

function TWordDoc.AddRangeFromDoc(iStart : Integer = 1; iEnd : Integer = 1) : TWordRange;
var
  wr : TWordRange;
begin
  wr := TWordRange.CreateFromDoc (Self, iStart, iEnd);
  AddRangeFromDoc := wr;
end;

function TWordDoc.AddRangeFromRange(ComRange : Range) : TWordRange;
var
  wr : TWordRange;
begin
  wr := TWordRange.CreateFromRange (Self, ComRange);
  AddRangeFromRange := wr;
end;

function TWordDoc.GetRange(Index : Integer) : TWordRange;
begin
  Assert ((Index >= 0) and (Index < FRanges.Count),
          'Index out of range for GetRange (' + IntToStr (Index) + ')');
  GetRange := TWordRange (FRanges [Index]);
end;

function TWordDoc.GetNoOfRanges : Integer;
begin
  GetNoOfRanges := FRanges.Count;
end;

procedure TWordDoc.DeleteRange(Index : Integer);
// remove and free range object
begin
  Assert ((Index >= 0) and (Index < FRanges.Count),
          'Index out of range for DeleteRange (' + IntToStr (Index) + ')');
  TWordRange (FRanges [Index]).Free;
end;

procedure TWordDoc.RemoveRange(Index : Integer);
// remove range object from list (but do not free it)
// should rarely be used as onus then on developer to free this range object
var
  i : Integer;
  wr : TWordRange;
begin
  Assert ((Index >= 0) and (Index < FRanges.Count),
          'Index out of range for RemoveRange (' + IntToStr (Index) + ')');
  FRanges.Delete (Index);
  i := Index;
  while i < FRanges.Count do
  begin
    wr := TWordRange (FRanges [i]);
    if Assigned (wr) then wr.FItemIndex := wr.FItemIndex - 1;
    inc (i);
  end;
end;

function TWordDoc.GoTo_(oeWhat, oeWhich: TOleEnum; oeCount: Integer = 1; oeName: String = '') : range;
var
  ovWhat, ovWhich, ovCount, ovName : OleVariant;
begin
  ovWhat := oeWhat;
  ovWhich := oeWhich;
  ovCount := ovCount;
  if ovName = '' then
    ovName := EmptyParam
  else
    ovName := ovName;
  GoTo_ := FComDoc.Range (EmptyParam, EmptyParam).GoTo_ (ovWhat, ovWhich, ovCount, ovName);
end;

function TWordDoc.GoToNext(oeWhat: TOleEnum) : {$IFDEF VER130}Word97.Range{$ELSE}Word_TLB.Range{$ENDIF};
var
  ovWhat : OleVariant;
begin
  ovWhat := oeWhat;
  GotoNext := FComDoc.Range (EmptyParam, EmptyParam).GoToNext (ovWhat);
end;

function TWordDoc.GoToPrevious(oeWhat: TOleEnum) : {$IFDEF VER130}Word97.Range{$ELSE}Word_TLB.Range{$ENDIF};
var
  ovWhat : OleVariant;
begin
  ovWhat := oeWhat;
  GotoPrevious := FComDoc.Range (EmptyParam, EmptyParam).GoToPrevious (ovWhat);
end;

procedure TWordDoc.ReplaceBookmark (BookmarkName, ReplaceText : String; ReassignBookmark : Boolean = True);
var
  ovBookMarkName : OleVariant;
  TempRange : {$IFDEF VER130}Word97.Range{$ELSE}Word_TLB.Range{$ENDIF};
  ovRange : OleVariant;
begin
  ovBookMarkName := BookMarkName;
  try
    TempRange := FComDoc.Bookmarks.Item (ovBookMarkName).Range;
    TempRange.Text := ReplaceText;
    ovRange := TempRange;
    if ReassignBookMark then FComDoc.Bookmarks.Add (BookmarkName, ovRange);
  except
    raise Exception.CreateFmt ('Bookmark "%s" not found in document "%s"',
                               [BookMarkName, FComDoc.Name]);
  end;
end;

function TWordDoc.NoOfPages (IncludeFootnotesAndEndnotes : Boolean = False) : Integer;
var
  ovIncudeHF : OleVariant;
begin
  ovIncudeHF := IncludeFootnotesAndEndnotes;
  NoOfPages := FComDoc.ComputeStatistics (wdStatisticPages, ovIncudeHF)
end;

function TWordDoc.NoOfWords (IncludeFootnotesAndEndnotes : Boolean = False) : Integer;
var
  ovIncudeHF : OleVariant;
begin
  ovIncudeHF := IncludeFootnotesAndEndnotes;
  NoOfWords := FComDoc.ComputeStatistics (wdStatisticWords, ovIncudeHF)
end;

function TWordDoc.GetNoOfBookMarks : Integer;
begin
  GetNoOfBookMarks := FComDoc.Bookmarks.Count;
end;

function TWordDoc.GetBookmarkByIndex (Index: Integer) : {$IFDEF VER130}Word97.Bookmark{$ELSE}Word_TLB.Bookmark{$ENDIF};
var
  ovBookMarkName : OleVariant;
begin
  Result := nil;
  try
    ovBookMarkName := Index;
    Result := FComDoc.Bookmarks.Item(ovBookMarkName);
  except
    raise Exception.CreateFmt ('Bookmark No %d not found in document "%s"',
                               [Index, FComDoc.Name]);
  end;
end;

function TWordDoc.GetBookmarkByName (BookmarkName: String) : {$IFDEF VER130}Word97.Bookmark{$ELSE}Word_TLB.Bookmark{$ENDIF};
var
  ovBookMarkName : OleVariant;
begin
  Result := nil;
  try
    ovBookMarkName := BookmarkName;
    Result := FComDoc.Bookmarks.Item(ovBookMarkName);
  except
    raise Exception.CreateFmt ('Bookmark "%s" not found in document "%s"',
                               [BookmarkName, FComDoc.Name]);
  end;
end;

function TWordDoc.GetAutoTextEntries : OleVariant;
begin
  GetAutoTextEntries := FComDoc.Get_AttachedTemplate.AutoTextEntries;
end;

procedure TWordDoc.SetBuiltInProperty(Index : String; Const Value: Variant);
//added by BDP 05/08/1999
var
  DocumentProperties : OLEVariant;
  DocumentProperty   : OLEVariant;
begin
  try
    DocumentProperties := FComDoc.BuiltInDocumentProperties;
    DocumentProperty := DocumentProperties.Item [Index];
    DocumentProperty.Value := Value;
  except
    raise Exception.CreateFmt ('Error setting document property: "%s"', [Index]);
  end;
end;

function TWordDoc.GetBuiltInProperty(Index : String) : Variant;
//added by BDP 05/08/1999
var
  DocumentProperties : OLEVariant;
  DocumentProperty   : OLEVariant;
begin
  try
    DocumentProperties := FComDoc.BuiltInDocumentProperties;
    DocumentProperty := DocumentProperties.Item [Index];
    Result := DocumentProperty.Value;
  except
    raise Exception.CreateFmt ('Error getting document property: "%s"', [Index]);
  end;
end;

procedure TWordDoc.SetCustomProperty(Index : String; Const Value : Variant);
//added by BDP 05/08/1999
var
  CustomProperties : OLEVariant;
  CustomProperty   : OLEVariant;
begin
  try
    CustomProperties := FComDoc.CustomDocumentProperties;
    CustomProperty := CustomProperties.Item [Index];
    CustomProperty.Value := Value;
  except
    raise Exception.CreateFmt ('Error setting custom property: "%s"', [Index]);
  end;
end;

function TWordDoc.GetCustomProperty(Index : String) : Variant;
//added by BDP 05/08/1999
var
  CustomProperties : OLEVariant;
  CustomProperty   : OLEVariant;
begin
  try
    CustomProperties := FComDoc.CustomDocumentProperties;
    CustomProperty := CustomProperties.Item [Index];
    Result := CustomProperty.Value;
  except
    raise Exception.CreateFmt ('Error getting custom property: "%s"', [Index]);
  end;
end;

{ TWordRange }

procedure TWordRange.Collapse(oeDirection: TOleEnum = wdCollapseStart);
var
  ovDirection : OleVariant;
begin
  ovDirection := oeDirection;
  FComRange.Collapse (ovDirection);
end;

constructor TWordRange.CreateFromBookMark(WordDoc: TWordDoc; BookmarkName: String);
var
  ovBookMarkName : OleVariant;
begin
  Create;
  FItemIndex := -1;
  ovBookMarkName := BookMarkName;
  FWordDoc := WordDoc;
  try
    FComRange := FWordDoc.FComDoc.Bookmarks.Item (ovBookMarkName).Range;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -