MS word and Delphi

Programm
Programm used Ask the Experts™
on
I am trying to delete line from MS word document from delphi program.

I can delete or remove bookmarks from the document but the line is still there.
How can I delete the line?


Here is my code.
Can I add some params to delete current line where my bookmark is?

      OleObj.application.Selection.Find.ClearFormatting;
      OleObj.application.Selection.Find.Text := strFinna;
      OleObj.application.Selection.Find.MatchCase := False;
      OleObj.application.Selection.Find.MatchWholeWord := False;
      OleObj.application.Selection.Find.MatchWildcards := False;
      OleObj.application.Selection.Find.MatchSoundsLike := False;
      OleObj.application.Selection.Find.MatchAllWordForms := False;
      OleObj.application.Selection.Find.Forward := True;
      OleObj.application.Selection.Find.Wrap := wdFindContinue;
      OleObj.application.Selection.Find.Format := True;
      OleObj.application.Selection.Find.Replacement.ClearFormatting;
      OleObj.application.Selection.Find.Replacement.Text := strResponse;
      OleObj.application.Selection.Find.Execute(Replace := wdReplaceAll);
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ferruccio AccalaiSenior developer, analyst and customer assistance

Commented:
In your code I see a Text replacement routine, not a bookmark deletion!
What do you really need? Where does this code come from?
Senior developer, analyst and customer assistance
Commented:
Ok I think now I've understood what do you need. You want (i guess) to use this kind of code to delete a line containig the found word.
If so then here's the code
replace YourDocument whit the filename and strFinna with the text to find
uses ActiveX, ComObj;
const
  wdFindContinue = 1;
  wdLine=5;
  wdStory = 6;
  wdExtend = 1;
  wdCharacter = 1;
var
  OleObj: Variant;
begin
  // create OLE object for MS Word application:
  OleObj := CreateOLEObject('Word.Application');
  // load a document from your file
  OleObj.Documents.Open(YourDocument);
  OleObj.Selection.Find.ClearFormatting;
  OleObj.Selection.Find.Text := strFinna;
  // WordApp.Selection.Find.Replacement.Text := yourNewStringForReplace;
  OleObj.Selection.Find.Forward := True;
  OleObj.Selection.Find.MatchAllWordForms := False;
  OleObj.Selection.Find.MatchCase := False;
  OleObj.Selection.Find.MatchWildcards := False;
  OleObj.Selection.Find.MatchSoundsLike := False;
  OleObj.Selection.Find.MatchWholeWord := False;
  OleObj.Selection.Find.MatchFuzzy := False;
  OleObj.Selection.Find.Wrap := wdFindContinue;
  OleObj.Selection.Find.Format := False;
  OleObj.Selection.HomeKey(unit := wdStory);
  while OleObj.Selection.Find.Execute do
  begin
    OleObj.Selection.EndKey(Unit := wdLine, Extend := wdExtend);
    OleObj.Selection.MoveRight(Unit := wdCharacter, Count := 1);
    OleObj.Selection.MoveUp(Unit := wdLine, Count := 1, Extend := wdExtend);
    OleObj.Selection.Delete(Unit := wdCharacter, Count := 1);
  end;
  OleObj.ActiveDocument.Save;
  OleObj.Quit;
  OleObj := Unassigned;
end;

Open in new window

Author

Commented:
Thanks. I can use this solution to our old problem! :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial