Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: find and replce in word documents  (Read 10332 times)
Stefan S.
Posts: 90


« on: April 08, 2015, 12:00:51 pm »

Hello List,

I try to use wincom to find/replcae
I am able to find the string, but I am not able to replace it.
Pressing "strg + h"  in word I can See my replace.String.

Here a word-Macro, which is working (see below):

My problem is the last part of the macro:
"    Selection.Find.Execute  Replace:=wdReplaceAll "

in the function "suchen_ersetzten" there is
call ui.Interface.frontCall("WINCOM","CallMethod", [gl_wwapp,"Selection.Find.Execute", lo_suchen],[gl_wwdoc])

I have to find out how to pass "Replace:=wdReplaceAll" with WinCom commands.

Attached a little program.
Perhaps anyone has an idea for me.

Thanks in advance.

Stefan Serwe


Sub Makro2()
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "<#ANREDE#>"
        .Replacement.Text = "Peter Unlustig"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute  Replace:=wdReplaceAll
    End Sub

* test_word.4gl (4.2 KB - downloaded 977 times.)
Lionel F.
Four Js
Posts: 83


« Reply #1 on: April 09, 2015, 05:50:43 pm »

Hi Stefan,

I'm afraid we're perhaps facing a WinCom limitation.

First, as an information, note that wdReplaceAll refers to a constant....and GDC is only supporting constants on their numeric forms . See also the documentation, especially the "Supported syntax" part:
https://4js.com/online_documentation/fjs-gdc-manual-html/?path=fjs-gdc-manual#c_gdc_WinCOM_API.html

You'll find in %GDCDIR%\etc\wincom.cst ,  a list of constants with their corresponding numeric values. This list is not exhaustive, you may find a lot of other constants with their numeric values by searching on the web (The list is now quite old as based on Microsoft Office XP).

If we're taking your example, the numeric value referring to wdReplaceAll is 2 (wdReplaceAll=2).  So constants must always be replaced by their numeric equivalent in the 4GL code.

But in your case this is not the real limitation...perhaps I'm wrong but I'm afraid the problem is that. :
Selection.Find.Execute  Replace:=wdReplaceAll is a "non parenthesis" notation, which is mentioned as an unsupported notation in the documentation (at the same URL as above). So we'd need to find an equivalent with a parenthesis notation. And unfortunately, from what I tested, Selection.Find.Execute (Replace:=2) doesn't seem to work, at least in Word 2010.

A small workaround that works for replacing an occurence would be possibly the following :
#First find the text to replace:
CALL ui.Interface.frontCall("WINCOM","SetProperty", [wdapp,"Selection.Find.Text", "some text"],[wddoc])

#Execute the search. Text of the search will be selected:
CALL ui.Interface.frontCall("WINCOM","CallMethod", [wdapp,"Selection.Find.Execute"],[wddoc])

#Type the text that will replace the other one:
CALL ui.Interface.frontCall("WINCOM","CallMethod", [wdapp,'Selection.TypeText("a new text")'],[wddoc])

Now if somebody has a better suggestion, it is really welcome :)

Regards,
Lionel

Stefan S.
Posts: 90


« Reply #2 on: April 10, 2015, 08:36:07 am »

Hello Lionel,

thanks for your workaround.
There was still a littel problem iththe "search direction".
To search in the whole document I have to set the "Wrap" Option to true in the function "suchen_ersetzen"
now It works.
Only if the Serach-String is not in the Document the replace-text is placed at the current Text-Position.

Thanks and have a nice weekend.

Stefan Serwe



Code
  1. function suchen_ersetzen(lo_suchen, lo_ersetzen)
  2. define lo_suchen       string,
  3.       lo_ersetzen     string,
  4.       lo_z            smallint
  5.  
  6.   let lo_suchen   = lo_suchen   clipped
  7.   let lo_ersetzen = lo_ersetzen clipped
  8.  
  9.  
  10.   call ui.Interface.frontCall("WINCOM","SetProperty",
  11.                           [gl_wwapp,"Selection.Find.Text",lo_suchen],[gl_wwdoc])
  12.   call CheckError(gl_wwdoc, __LINE__)
  13.  
  14.   call ui.Interface.frontCall("WINCOM","SetProperty",
  15.                           [gl_wwapp,"Selection.Find.Forward",true],[gl_wwdoc])
  16.   call CheckError(gl_wwdoc, __LINE__)
  17.  
  18. [b]   call ui.Interface.frontCall("WINCOM","SetProperty",
  19.                           [gl_wwapp,"Selection.Find.Wrap",1],[gl_wwdoc])
  20.   call CheckError(gl_wwdoc, __LINE__)
  21. [/b]
  22.   call ui.Interface.frontCall("WINCOM","CallMethod",
  23.                           [gl_wwapp,"Selection.Find.Execute"],[gl_wwdoc])
  24.   call CheckError(gl_wwdoc, __LINE__)
  25.  
  26.   call ui.Interface.frontCall("WINCOM","CallMethod",
  27.                           [gl_wwapp,"Selection.TypeText", lo_ersetzen],[gl_wwdoc])
  28.   call CheckError(gl_wwdoc, __LINE__)
  29.  
  30.  
  31. end function
  32.  
  33.  
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines