1

I would like to ask how can I print a variety of documents from C #. I did this by modifying the document word by replacing the text for example:

<Value> -> value. 

I have various documents, ie. a plain text document and the document to the table. The table shows the installments to repay. Document to the table is a problem because I do not know how to retrieve data and how to display them.

The whole still must be possible to configure, add, edit, delete documents. I have somethink as You can see below.

public string CreateDoc(object fileName, string _query, string _numUmowy)
    {
        object oMissing = System.Reflection.Missing.Value;
        object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;

        string message = string.Empty;

        Application wordApp = new Application();
        Document aDoc = null;

        if (File.Exists((string)fileName))
        {
            DateTime today = DateTime.Now;

            object readOnly = false;
            object isVisible = false;

            wordApp.Visible = false;

            aDoc = wordApp.Documents.Open(ref fileName, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);

            aDoc.Activate();



            using (SQLiteConnection conn = new SQLiteConnection(GetConnStr()))
            {
                using (SQLiteCommand cmd = new SQLiteCommand(_query, conn))
                {
                    conn.Open();

                    cmd.Parameters.AddWithValue("@UmowaNum", _numUmowy);

                    using (SQLiteDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();

                            int rdrLength = rdr.FieldCount;
                            for (int i = 0; i < rdrLength; i++)
                            {
                                this.FindAndReplace(wordApp, "<" + rdr.GetName(i) + ">", rdr[i].ToString());
                            }


                            aDoc.PrintOut(oMissing, oMissing, oMissing, oMissing,
                                oMissing, oMissing, oMissing, oMissing, oMissing,
                                oMissing, oMissing, oMissing, oMissing, oMissing,
                                oMissing, oMissing, oMissing, oMissing);

                            message = "Drukowanie zakończone powodzeniem!";
                        }
                    }
                }
            }

            aDoc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
            wordApp.Quit();

            return message;

        }
        else
        {
            message = "Plik nie istnieje!";
            return message;
        }
    }

This code print Agreement, but I don't know how to print Installment. Agreement.doc

aa bbb fbdsf
<Value1>
aa bbb fbdsf
<Value2>
aa bbb fbdsf
<Value3>
aa bbb fbdsf
<Value4>
aa bbb fbdsf
<Value5>

Installment.doc

aa bbb fbdsf
<Value1>
aa bbb fbdsf
<Value2>
aa bbb fbdsf
TABLE (start)
1. <val1.1> <val1.2> <val1.3>
2. <val2.1> <val2.2> <val2.3>
3. <val3.1> <val3.2> <val3.3>
4. <val4.1> <val4.2> <val4.3>
5. <val5.1> <val5.2> <val5.3>
TABLE (end)

3 Answers3

0

Take a look at this: Printing using Word Interop with Print Dialog

If it wont help you, try to be more specific with your problem.

Edit: 2nd option (without interoop) code from (https://wurstkoffer.wordpress.com/2013/05/18/c-printing-to-word-programmatically-in-3-way/)

string filename = @"C:\test.docx";
string printer = "printer name";

// Send it to the selected printer
using (PrintDialog printDialog1 = new PrintDialog())
{
  printDialog1.PrinterSettings.PrinterName = printer;
  if (printDialog1.ShowDialog() == DialogResult.OK)
  {
    System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(filename);
    //info.Arguments = “\”” + printDialog1.PrinterSettings.PrinterName + “\””;
    info.Arguments = "\"" + printer + "\"";
    info.CreateNoWindow = true;
    info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    info.UseShellExecute = true;
    info.Verb = "PrintTo";
    System.Diagnostics.Process.Start(info);
  }
}
Community
  • 1
  • 1
eren
  • 708
  • 8
  • 20
  • Thank You for reply. I know how to print documents, but I don't know how to automatize it. I do not have to write different functions for each document. – Paweł Janicki Feb 26 '15 at 13:25
  • I'm not quite sure what u mean by that. I may be wrong with this but have u tried to make a function with filePath as parameter? Anyway it would be best to post what have u done so far ;) – eren Feb 26 '15 at 13:32
  • Well, i created exact copies of your agreement and installemnt documents and tried your function. It worked pretty well so i guess there is some issue in FindAndReplace Method. – eren Feb 26 '15 at 14:06
  • I don't know how can I explain You what I need. I need to know how to get data to Installment.doc and print it. – Paweł Janicki Feb 26 '15 at 14:32
  • Try to post the body of FindAndReplace than ;) i'll try to do my best and help you out with it. – eren Feb 26 '15 at 15:08
  • I'm trying to do my best. So let me recapitulate: The Function CreateDoc works fine for agreement file, that means it takes the file replace whatever you need to replace (), prints the file and returns the value: "Drukowanie zakończone powodzeniem!". And for the installment.doc it do the same but it fails to print out the file? Or it also fails to replace the value ? Edit: Try to add conn.Close() before end of using (SQLiteConnection conn = new SQLiteConnection(GetConnStr())) – eren Feb 26 '15 at 15:35
0

This is my FindAndReplace function which work perfectly

public void FindAndReplace(Application WordApp, object findText, object replaceWithText)
    {
        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object nmatchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;

        WordApp.Selection.Find.Execute(ref findText, 
            ref matchCase, ref matchWholeWord, 
            ref matchWildCards, ref matchSoundsLike,
            ref nmatchAllWordForms, ref forward, 
            ref wrap, ref format, ref replaceWithText, 
            ref replace, ref matchKashida, 
            ref matchDiacritics, ref matchAlefHamza, ref matchControl);
    }
0

Well, i tried everything. I used exact copy of all your code, i used 'northwindEF.db' as database.

I used CustomerID as '_numUmowy'

cmd.Parameters.AddWithValue("@CustomerID", _numUmowy);

I put exact column names from table Customers into all '<' '>' within those word documents. And everything went fine. All the values for specific users was nicely copied into word documents and those documents were afterwards printed without any problems.

this.CreateDoc("path to agreement doc", "SELECT * FROM Customers", "ANATR")
this.CreateDoc("path to installment doc", "SELECT * FROM Customers", "ALFKI")

I'm really sorry i couldn't help you out with this. I hope someone else will.

eren
  • 708
  • 8
  • 20