1
I saw many examples of exchanging variables in word document with find replace, but only with a document, being passed the path of it in an 'Object template(allows only one informed document)'. In my case I bring several word documents from a directory (in a checklistbox) and would like to change variable of the ones that will be selected (more than one). How do I do?
Make the exchange of 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);
loop by swapping the variables one by one. Put the code you have please
– Rovann Linhalis
code where I can pass only 1 file. can you give me an example? @Rovannlinhalis the others Voce passed on the other question I could not understand.
– Lucão
This code you put in, is changing the variables of 1 file ? the model, with the variables, is the template, right? what data is in the checkedlistbox ?
– Rovann Linhalis
this, it is only switching from the file passed in the Object template
– Lucão