MS Word search & replace using automation

megawavezmegawavez Member Posts: 133
So, has anybody implemented or does anybody have a link to directions to how to do a search and replace in an MS Word document using automation? I found some stuff about mail merge, but it would be a little easier if it could be done using a search and replace.

Thanks,
Mega

Comments

  • kinekine Member Posts: 12,562
    You can use variables very easilly in word to be replaced through code from NAV by correct values.
    Docs := App.Documents;
    Doc := Docs.Add(TemplatePath);
    Fields := Doc.Fields;
    WHILE Fields.Count > 0 DO BEGIN
      Field := Fields.Item(1);
      Range := Field.Code;
      FieldName := DELCHR(Range.Text,'<>',' ');
      FieldName := DELSTR(FieldName,1,STRPOS(FieldName,' ')+1);
      FieldName := DELSTR(FieldName,STRPOS(FieldName,' '));
      Range := Field.Result;
    
      CASE FieldName OF
       ...
         Range.Text := 'blabla';
      end;
      Field.Unlink;
    end;
    
    In the case you can fill the text you want based on the FieldName (which you used in Word document). Range,Field, Fields, Doc, Docs are automations for Word...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • megawavezmegawavez Member Posts: 133
    Thanks for the response !

    I know little about word - is "Field" some sort of variable you can setup in msword. Excel has named ranges - would this be the equivalent?
  • kinekine Member Posts: 12,562
    Something like that...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • megawavezmegawavez Member Posts: 133
    Think I found it - thanks.
Sign In or Register to comment.