Exchange variables from selected word documents

Asked

Viewed 245 times

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

  • 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.

  • 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 ?

  • this, it is only switching from the file passed in the Object template

1 answer

1


with the short snippet you have placed, and whereas the checkedlistbox item is the name of the file you want to change, make an iteration between the 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");

            }

        }
  • I already do this iteration checking the marked documents, but where do I pass the directory of all my documents so that it considers only the marked ones ? something like that:

  • Directoryinfo directory = new Directoryinfo(@"C: Test"); //Executes Getfile function(Lists the desired files according to the parameter) Fileinfo[] Files = directory.Getfiles(".docx");

  • so it’s important to put your code so we can see what you’re doing. I’ve changed the answer

  • Thanks @Rovann linhalis

  • 1

    had not seen your reply completely, it worked thank you very much @Rovann Linhalis

Browser other questions tagged

You are not signed in. Login or sign up in order to post.