Swapping Variables for Selected Word Documents

1

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);
    
asked by anonymous 30.10.2017 / 19:29

1 answer

1

With the short excerpt you placed, and assuming the checkedlistbox item is the name of the file you want to change, iterate between checked items:

        foreach (string arquivo in checkedListBox1.CheckedItems)
        {

            FileInfo file = new FileInfo("C:\Teste\"+ arquivo);

            if (file.Exists)
            {

            //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 = file.FullName;
            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);



          //Processa o arquivo alterado, Salvar, imprimir, etc..

            }
            else
            {
                  MessageBox.Show("Arquivo " + arquivo + " não existe");

            }

        }
    
30.10.2017 / 20:31