Announcement

Collapse
No announcement yet.

wordapplication + text finden und ersetzen

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • wordapplication + text finden und ersetzen

    Hallo!

    <pre><b>Source:</b>

    var
    name, bo, bo2, text, repllevariant;

    begin
    bo := true;
    bo2 := false;
    text:='TEST';
    repl := 'OK';

    datei := 'c:\test.doc';

    wordapplication1.Documents.Open(name, emptyparam, emptyparam, emptyparam,
    emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam,
    emptyparam, bo);

    showmessage('textbereich markieren');

    wa1.Selection.Find.Execute();
    </pre>

    <b>Problem:</b>

    Ich weiß nicht genau, wie die Parameter in der Funktion (wa1.Selection.Find.Execute(............))
    angegeben werden müssen, damit jedes Wort('Test'), das sich in dem markierten Bereich befindet, durch das jeweilige Wort(hier: 'OK') ersetzt wird.
    Oder gibt es eine elegeantere Lösung, zu diesem Problem?

    Vielen Dank im voraus!

    mfg,
    Bora

  • #2
    Hallo,

    welche Aufgabe soll erfüllt werden? Wenn es nur darum geht, in eine vordefinerte Word-Vorlage an bestimmten Stellen variablen Text einzufügen, dann ist der Weg über Textmarken viel effizienter

    Comment


    • #3
      Hallo Herr Kosch,

      im Endeffekt möchte ich den Text-Suchen-Ersetzen Dialog aus Office umsetzen, allerdings soll eben nur der markierte Textbereich verändert werden. Es sollen x-beliebige Word Dateien verändert werden können. Die Dateien sind also nicht vordefiniert!

      Vielen Dank im voraus!

      Mfg,
      Bor

      Comment


      • #4
        Sehr geehrter Herr Yalkinoglu,<br>anbei ein Beispiel zur Verwendung der von Ihnen genannten Methode:<br><br>

        <H1 class=RH1><A NAME="wohowsearchandreplace"></A>Finding and replacing text or formatting</H1>

        <P class=ALNSR>
        </P>

        <P class=T>Finding and replacing is exposed by the <B>Find</B> and <B>Replacement</B> objects. The <B>Find</B> object is available from the <B>Selection</B> and <B>Range</B> object. The find action differs slightly depending upon whether you access the <B>Find</B> object from the <B>Selection</B> or <B>Range</B> object.</P>

        <P class=RL><B>Finding text and selecting it</B></P>

        <P class=T>If the <B>Find</B> object is accessed from the <B>Selection</B> object, the selection is changed when the find criteria is found. The following example selects the next occurrence of the word "Hello." If the end of the document is reached before the word "Hello" is found, the search is stopped.</P>

        <PRE class=CT><CODE>With Selection.Find
        &nbsp;&nbsp;&nbsp; .Forward = True
        &nbsp;&nbsp;&nbsp; .Wrap = wdFindStop
        &nbsp;&nbsp;&nbsp; .Text = "Hello"
        &nbsp;&nbsp;&nbsp; .Execute
        End With</CODE></PRE>

        <P class=T>The <B>Find</B> object includes properties that relate to the options in the <B>Find and Replace</B> dialog box (choose <B>Find</B> from the <B>Edit</B> menu). You can set the individual properties of the <B>Find</B> object or use arguments with the <B>Execute</B> method as shown in the following example.</P>

        <PRE class=CT><CODE>Selection.Find.Execute FindText:="Hello", _
        &nbsp;&nbsp;&nbsp; Forward:=True, Wrap:=wdFindStop</CODE></PRE>

        <P class=RL><B>Finding text without changing the selection</B></P>

        <P class=T>If the <B>Find</B> object is accessed from a <B>Range</B> object, the selection is not changed but the <B>Range</B> is redefined when the find criteria is found. The following example locates the first occurrence of the word "blue" in the active document. If the find operation is successful, the range is redefined and bold formatting is applied to the word "blue." </P>

        <PRE class=CT><CODE>With ActiveDocument.Content.Find
        &nbsp;&nbsp;&nbsp; .Text = "blue"
        &nbsp;&nbsp;&nbsp; .Forward = True
        &nbsp;&nbsp;&nbsp; .Execute
        &nbsp;&nbsp;&nbsp; If .Found = True Then .Parent.Bold = True
        End With</CODE></PRE>

        <P class=T>The following example performs the same result as the previous example using arguments of the <B>Execute</B> method.</P>

        <PRE class=CT><CODE>Set myRange = ActiveDocument.Content
        myRange.Find.Execute FindText:="blue", Forward:=True
        If myRange.Find.Found = True Then myRange.Bold = True</CODE></PRE>

        <P class=RL><B>Using the Replacement object</B></P>

        <P class=T>The <B>Replacement</B> object represents the replace criteria for a find and replace operation. The properties and methods of the <B>Replacement</B> object correspond to the options in the <B>Find and Replace</B> dialog box (<B>Edit</B> menu).</P>

        <P class=T>The <B>Replacement</B> object is available from the <B>Find</B> object. The following example replaces all occurrences of the word "hi" with "hello." The selection changes when the find criteria is found because the <B>Find</B> object is accessed from the <B>Selection</B> object.</P>

        <PRE class=CT><CODE>With Selection.Find
        &nbsp;&nbsp;&nbsp; .ClearFormatting
        &nbsp;&nbsp;&nbsp; .Text = "hi"
        &nbsp;&nbsp;&nbsp; .Replacement.ClearFormatting
        &nbsp;&nbsp;&nbsp; .Replacement.Text = "hello"
        &nbsp;&nbsp;&nbsp; .Execute Replace:=wdReplaceAll, Forward:=True, _
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Wrap:=wdFindContinue
        End With</CODE></PRE>

        <P class=T>The following example removes bold formatting in the active document. The <B>Bold</B> property is <B>True</B> for the <B>Find</B> object and <B>False</B> for the <B>Replacement</B> object. In order to find and replace formatting, set the find and replace text to empty strings ("") and set the <B><I>Format</I></B> argument of the <B>Execute</B> method to <B>True</B>.

        Comment


        • #5
          Hallo,

          leider konnte mir Herr Stumpf`s Beitrag in sofern nicht weiterhelfen, da im Beitrag nur gesagt wird, wie der gesamte Text, unabhängig davon ob er Text markiert wurde oder nicht, einer Word Datei durchsucht werden kann. Das ausschlaggebende Property ist meines Erachtens:
          wordapplication.selection.find.execute.<b>Replace: = wdreplaceall<b/>;

          Da ich nicht weiß, welcher Wert(also NICHT wdreplaceall :-)) hier für das Property "Replace" angegeben werden muss, damit die Prozedur execute nur den markierten Textbereich durchsucht und nicht den gesamten Text!!!!

          Mfg,
          Bor

          Comment


          • #6
            Da mir in meinem letzen Beitrag ein Fehler unterlaufen ist.... :-( hier <b>nochmal</b>:

            Hallo,

            leider konnte mir Herr Stumpf`s Beitrag in sofern nicht weiterhelfen, da im Beitrag nur gesagt wird, wie der gesamte Text, unabhängig davon ob er Text markiert wurde oder nicht, einer Word Datei durchsucht werden kann. Das ausschlaggebende Property ist meines Erachtens: wordapplication.selection.find.execute.Replace:= wdreplaceall</b>;

            Daher muss ich wissen, welcher Wert(also NICHT wdreplaceall :-)) hier für das Property "Replace" angegeben werden muss, damit die Prozedur execute nur den markierten Textbereich durchsucht und nicht den gesamten Text!!!!

            Mfg, Bor

            Comment


            • #7
              Hallo Bora,

              das nachfolgende ist der notwendige Word-VBA-Code. Dieser braucht dann nur noch in Delphi umgesetzt zu werden.
              <pre>
              With Selection.Find
              .ClearFormatting
              .Text = "hi"
              .Replacement.ClearFormatting
              .Replacement.Text = "hello"
              .Execute Replace:=wdReplaceAll, Forward:=True, _
              Wrap:= <font color="#FF0080"><b>wdFindStop</b></font>
              End With
              </pre>

              Tschüß

              Torste

              Comment


              • #8
                Hallo Torsten,

                Danke, jetzt klappt`s!

                Mfg,
                Bor

                Comment

                Working...
                X