Link line of txt file with selected in my chelistbox

Asked

Viewed 49 times

0

my code brings inside a checklistbox the contents of a txt file. Each line of that file (it is also a line in my checklistbox) is the contents of a variable that I will exchange in a Word document. I need that when selecting in my checklistbox the (name of the collaborator) it brings to me from within the txt file only the line of the collaborator I selected for me to store in my variable that will give the replace in Word. Follow code where I read all the lines and assign my strings I need to manipulate their content to bring only 'selected''.

Preencherreplace c = new Preencherreplace();

                string filePath = @"C:\Teste\Lista_col.txt";
                string line;
                string[] array  = File.ReadAllLines(@"C:\Teste\Lista_col.txt");

                if (File.Exists(filePath))
                {
                    using (StreamReader reader = new StreamReader(filePath))

                    {
                        while ((line = reader.ReadLine()) != null)
                        {
                            string[] auxiliar = line.Split(';'); // faço um split separando onde possui barras e virgulas

                            c.codigo = auxiliar[0];
                            c.nome = auxiliar[1];
                            c.funcao = auxiliar[2];
                            c.setor = auxiliar[3];

1 answer

0

For this, I use a function:

public static void ImprimirArquivoWord(string arquivo, bool preview)
{
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(arquivo, ReadOnly: true, Visible: preview);

    if (preview)
    {
        wordApp.Visible = true;
        aDoc.Activate();
        aDoc.PrintPreview();
    }
    else
    {
        aDoc.PrintOut();
        ((Microsoft.Office.Interop.Word._Application)wordApp).Quit(true);
    }
}

Reference was used for: Microsoft Word 12.0 Object Library and Microsoft Office 12.0 Object Library

Putting in your context:

foreach (FileInfo Arqui in ck_arquivos.CheckedItems)
{
    ImprimirArquivoWord(Arqui.FullName,false);
}
  • Okay, buddy, in this same program I’m swapping out some variables from the Word(.docx) document and then printing. However due to my 'Object template' allow to be setting only one file it does not take another that I have selected in my checklistbox if not this, you would know a way to pass the other files of mine to be my choice which to use ? @Rovann Linhalis

  • @Lucão also performs the exchange of variables within the word. But for this I do in a separate function and save the file in a temporary folder for this. The stream is: Save the temporary file. I call the function that changes the variables, and saves the file. Calls the function that prints. In its context, just put it inside the loop.

  • could explain me better how you do to pass over a Word file to me to verify which ones selected through code?

  • i don’t pass over a file in function or wordApp. I loop calling one file at a time

  • friends need also in this same program the following, I already have the contents of a txt file read, now I need to bring the line of the txt file that I selected in my checklistbox to change in a variable someone can help me with this link?

  • edit the question, explaining in detail what you need to do, and I will change the answer by putting what you need.

  • but those files he passes one at a time, how does he identify the path where those files are ? @Rovann Linhalis

  • i save the files I need, and store the name in variable. then loop to print

  • got it, as for the link with txt I’ll change the question, you can help me ?

  • yes, I just need to understand what you need to do rs. Try to explain the program requirement and what you are trying to do

Show 5 more comments

Browser other questions tagged

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