views:

376

answers:

2

Hi there. I want replace some string within Word Document using ActiveX and JavaScript, now I have something like this:

var text = Selection.Text;
Selection.Text = text.replace('somesting','somevalue');

But after that I loose all formaing. If I record macros in WinWord I get this:

With Selection.Find
    .Text = "somestring"
    .Replacement.Text = "somevalue"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.Execute Replace:=wdReplaceAll

So, how can I rewrite the last string with JS?

+1  A: 
Selection.Find.Execute('somestring',
                       false,
                       false,
                       false,
                       false,
                       false,
                       wdFindContinue,
                       false,
                       false,
                       'somevalue'
                       wdReplaceAll,
                       false,
                       false,
                       false,
                       false);

Also you should have some word constants, like:

var wdReplaceAll = 2;
var wdFindContinue = 1;
A: 

Works well if the text is less than 256 characters. Refer to Article ID: 212543 http://support.microsoft.com/kb/212543 for workaround.

Thanks, Manju

Manju MS