Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 09-29-2018, 09:50 AM
Lex34 Lex34 is offline
Junior Member
 
Join Date: Sep 2018
Posts: 1
Default Text adding does not work in Word - InsertAfter and InsertBefore

I try to add text in the word before and after the selected area. The document is opened, but the text is not added, what could be the problem?
Code:
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtnrs, ComObj, ComCtrls, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    RichEdit1: TRichEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var
  MSWord: OLEVariant;
  DocName, Doc1 : OleVariant;
  WordApplication1: OLEVariant;
  vstart, vend: OleVariant;
begin
 
  DocName  := 'C:\Doc3.docx';
  
  try
    MsWord := GetActiveOleObject('Word.Application');
  except
    try
      MsWord := CreateOleObject('Word.Application');
      WordApplication1 := CreateOLEObject('Word.Application');
      Doc1 := WordApplication1.Documents.Open(DocName);
 
 
      RichEdit1.Text:=Doc1.Range.Text;
      WordApplication1.Visible := True;
 
       vstart := 10;
       vend := 20;
       Doc1.Range(vstart, vend).Select;
       Doc1.Selection.InsertAfter('text1');
       Doc1.Selection.InsertBefore('text2');
 
    except
      Exception.Create('Error');
    end;
  end;
end;
end.
Reply With Quote
  #2  
Old 09-30-2018, 11:11 PM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 191
Default

Rather than basing the flow of your code to an exception, you can get a reference to MS Word via code that doesn't cause and exception:
Code:
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    function GetWordApplication: IDispatch; //Add this
  public
     { Public declarations }
  end;

var
  Form1: TForm1;


implementation


uses System.Win.ComObj, Winapi.ActiveX;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  MSWord, Doc1: OLEVariant;
  DocName: String;
  vstart, vend: Integer;
begin
  MsWord := GetWordApplication;
  DocName  := 'C:\Test\Hello.docx';
  Doc1 := MsWord.Documents.Open(DocName);
  MsWord.Visible := False;
  RichEdit1.Text := Doc1.Range.Text;
  vstart := 10;
  vend := 20;
  Doc1.Range(vstart, vend).Select;
  MsWord.Selection.InsertAfter(' textAfter ');
  MsWord.Selection.InsertBefore(' textBefore ');
  Doc1.Save;//this saves to the original file
  MsWord.quit;
end;

function TForm1.GetWordApplication: IDispatch;
var
  ClassID: TGUID;
  Unknown: IUnknown;
  HR: HResult;
begin
  ClassID := ProgIDToClassID('Word.Application');
  HR := GetActiveObject(ClassID, nil, Unknown);
  if (HR = MK_E_UNAVAILABLE) then
    Result := CreateComObject(ClassID) as IDispatch
  else
  begin
    OleCheck(HR);
    OleCheck(Unknown.QueryInterface(IDispatch, Result));
  end;
end;
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 08:26 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2018, vBulletin Solutions, Inc.