Replace a variable within a Word file

3

I am trying to do in C # (Windows Form application) to print a Word .doc only by changing some parsions of type @Nome to a string .

In short, I have a contract and I need to print it. By reporting specific fields, I already have a Word file template and just need to fill it with the data.

I'm doing this:

public void PreencherPorReplace(string CaminhoDocMatriz)
{
    //Objeto a ser usado nos parâmetros opcionais
    object missing=System.Reflection.Missing.Value;

    Word.Application oApp=new Word.Application() ;

    object template = CaminhoDocMatriz;

    Word.Document oDoc = oApp.Documents.Add(ref template , ref missing,
                                            ref missing, ref missing);

    //Troca o conteúdo de alguns tags
    Word.Range oRng= oDoc.Range(ref missing ,ref missing );

    object FindText = "@Nome";
    object ReplaceWith="Teste";
    object MatchWholeWord = true;
    object Forward = false;

    oRng.Find.Execute( ref FindText, ref missing, ref MatchWholeWord,
                       ref missing, ref missing, ref missing, ref Forward,
                       ref missing, ref missing, ref ReplaceWith, ref missing,
                       ref missing, ref missing, ref missing, ref missing);


    oApp.Visible = true;
}

But it only replaces @Nome .

    
asked by anonymous 12.08.2016 / 19:18

1 answer

0

The syntax of the method Find.Execute is:

bool Execute(
    ref Object FindText,
    ref Object MatchCase,
    ref Object MatchWholeWord,
    ref Object MatchWildcards,
    ref Object MatchSoundsLike,
    ref Object MatchAllWordForms,
    ref Object Forward,
    ref Object Wrap,
    ref Object Format,
    ref Object ReplaceWith,
    ref Object Replace,
    ref Object MatchKashida,
    ref Object MatchDiacritics,
    ref Object MatchAlefHamza,
    ref Object MatchControl
)

In the replace parameter, specify the wdReplaceAll / a>:

object replace = WdReplace.wdReplaceAll;

//...

oRng.Find.Execute( ref FindText , ref missing,ref MatchWholeWord , 
                   ref missing,ref missing,ref missing,ref Forward,
                   ref missing,ref missing,ref ReplaceWith ,ref replace,
                   ref missing,ref missing,ref missing,ref missing);
    
12.08.2016 / 20:22