Board index » delphi » Help: Word OLE - Ways to change Selection.Find.Style := MyStyle

Help: Word OLE - Ways to change Selection.Find.Style := MyStyle

Hi all,
my small application uses Word_TLB by standard methods.

I need find text formatted with user defined style "MyStyle". In Word8
Visual Basic it is made by

    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Oddelovac")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute

But if I chose in OLE change WordObject.Selection.Find.Style there is OLE
Error C0000005 dialog. There is skeleton of the problem:
type
 TWinWord = class
  Private
   App : _Application;
  public
   constructor Create;
   procedure EditStyle(Styl : WideString);
  end;
constructor TWinWord.Create;
begin
  App := CoApplication.Create;
end;
procedure TWinWord.EditStyle(StyleRequested : WideString);
var vStyle, vS:OLEVariant;
begin
    vS:=StyleRequested; {String of User Style Name}
    vStyle:=App.ActiveDocument.Styles.Item(vS) ;
    App.Selection.Find.Format:=True;
    App.Selection.Find.Style  :=vStyle; {There is OLE Error exception
C0000005}
end;

Main procedure:
var WordObj: TWinWord;
.
.
  WordObj := TWinWord.create;
. WordObj.Visible:=True;
.
{now, create new document or open old document. In the Normal.dot
 there is user defined character style "MyStyle" and opened document
 has some text written with this style.
 If you debug under Delphi IDE, you can do this steps direct in the Word.}
.
.
WordObj.EditClearFormatting;
WordObj.EditStyle('MyStyle');
{There I need to use Selection.Find.Execute for searching, but program ends
before in the EditStyle procedure ..}

What is bad in my skeleton {so, this can be my mind, too} ?
Thank you in advance for any ideas about solving this problem.
Vladimir

 

Re:Help: Word OLE - Ways to change Selection.Find.Style := MyStyle


Quote
Vladimir Janda wrote:
>my small application uses Word_TLB by standard methods.
>procedure TWinWord.EditStyle(StyleRequested : WideString);
>var vStyle, vS:OLEVariant;
>begin
>    vS:=StyleRequested; {String of User Style Name}
>    vStyle:=App.ActiveDocument.Styles.Item(vS) ;
>    App.Selection.Find.Format:=True;
>    App.Selection.Find.Style  :=vStyle; {exception C0000005}
>end;

Mattew Greet discovered that the dispinterface is unaffected:
if you cast the interface to SelectionDisp or FindDisp and
assign the property that way it works.
  http://www.dejanews.com/getdoc.xp?AN=345994672

Exactly why the vtable interface fails I'm not sure.

Re:Help: Word OLE - Ways to change Selection.Find.Style := MyStyle


PS. Sorry for misspelling your name Matthew.
This morning's coffee hasn't quite kicked in yet...

Other Threads