I've seen quite a few examples of changing variables in a word document with find replace, but only with a document, and passed the path of it in an 'object template'. In my case, I bring several documents word from a directory (in a checklistbox) and would like to change the variable of which will be selected (more than one). How do I do this?
Switches the variables:
//Objeto a ser usado nos parâmetros opcionais
object missing = System.Reflection.Missing.Value;
//Abre a aplicação Word e faz uma cópia do documento mapeado
Microsoft.Office.Interop.Word.Application oApp = new
Word.Application();
object template = @"C:\Teste\ABASTECEDOR DE GLP.docx";
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 = "@var1";
object ReplaceWith = c.codigo;
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);
oRng = oDoc.Range(ref missing, ref missing);
FindText = "@var2";
ReplaceWith = c.nome;
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);
oRng = oDoc.Range(ref missing, ref missing);
FindText = "@var3";
ReplaceWith = c.setor;
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);
oRng = oDoc.Range(ref missing, ref missing);