Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
I have developed a desk top based application that create word file.
my code is below:
 Word.Application wrdApp;
        Word._Document wrdDoc;
        Word.Table wrdTable;
        Object oMissing = System.Reflection.Missing.Value;
        Object oFalse = false;
<pre> private void button2_Click(object sender, EventArgs e)
       {

           wrdApp = new Word.Application();
           wrdApp.Visible = false;
           wrdDoc = wrdApp.Documents.Add(ref oMissing, ref oMissing,
               ref oMissing, ref oMissing);
          
           Word.Selection wrdSelection;
           Word.MailMerge wrdMailMerge;
           wrdDoc.Select();
           wrdSelection = wrdApp.Selection;
           wrdMailMerge = wrdDoc.MailMerge;

        
           //merge
          // Word._Document oDataDoc;
           int iCount;

           Object oName = "D:\\ddd.doc";
           // Insert a new table with 9 rows and 4 columns.
           
         Word.Table  wrdTable = wrdDoc.Tables.Add(wrdSelection.Range, 9, 4,
               ref oMissing, ref oMissing);
         
           Object oHeader = "'   ','   ','  ','  ' ";
            // Set the column widths.
           wrdTable.Columns[1].SetWidth(300, Word.WdRulerStyle.wdAdjustSameWidth);
           wrdTable.Columns[2].SetWidth(500, Word.WdRulerStyle.wdAdjustSameWidth);
           wrdTable.Columns[3].SetWidth(400, Word.WdRulerStyle.wdAdjustSameWidth);
           wrdTable.Columns[4].SetWidth(100, Word.WdRulerStyle.wdAdjustSameWidth);
	
           wrdDoc.MailMerge.CreateDataSource(ref oName, ref oMissing,
           ref oMissing, ref oHeader, ref oMissing, ref oMissing,
           ref oMissing, ref oMissing, ref oMissing);

           // Open the file to insert data.
           wrdDoc = wrdApp.Documents.Open(ref oName, 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);

           for (iCount = 1; iCount <= 2; iCount++)
           {
               wrdDoc.Tables[1].Rows.Add(ref oMissing);
           }

           // Fill in the data.
           FillRow(wrdDoc, 2, "I.D. Number          : ", "" + txtReceipt.Text + "", "Date:", "" + dtDatePicker.Text + "");
           FillRow(wrdDoc, 3, "Name of the patient  :", "" + txtPatientName.Text + "", "Age :", "" + txtAge.Text + "");
           FillRow(wrdDoc, 4, "Referred by          :",""+txtRefDocter.Text+"","Sex:",""+txtSex.Text+"");
           
           
           // Save and close the file.
           
           wrdDoc.Save();
           wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
       }


and
private void FillRow(Word._Document oDoc, int Row, string Text1,string Text2,string Text3,string Text4)
        {
            // Insert the data into the specific cell.
            oDoc.Tables[1].Cell(Row, 1).Range.InsertAfter(Text1);
            oDoc.Tables[1].Cell(Row, 2).Range.InsertAfter(Text2);
            oDoc.Tables[1].Cell(Row, 3).Range.InsertAfter(Text3);
            oDoc.Tables[1].Cell(Row, 4).Range.InsertAfter(Text4);
           // oDoc.Tables[1].Cell(Row, 5).Range.InsertAfter(Text5);
        }


my word file:
shows

M____	                M____1	  M___    M___1
I.D. Number          : 	sfdsfdf	  Date:	  07/15/2010
Name of the patient  :	sfdfds	  Age :	  sfdfd
Referred by          :	sfdfdf	  Sex:	  sfsfsd 


Problem:
columns width in table cell isn't work and my word file header M_... like this I don't need this but if close header then a lot of header will appear and my existing document was erased but i strongly need it bec only this table will be inserted.
Please help me,I am in deep fix.
Any help thanks in advance.
Masud
Posted
Updated 29-Jun-17 4:07am
v5

1 solution

If just table Width, this is what i have did try and error. The final result is I got this.

C#
          wrdApp = new Word.Application();
          wrdApp.Visible = false;
          wrdDoc = wrdApp.Documents.Add(ref oMissing, ref oMissing,
               ref oMissing, ref oMissing);

          //Range
          Word.Range wrdtableRange = wrdDoc.Paragraphs.Add(ref oMissing).Range;
          wrdtableRange.Tables.Add(wrdtableRange, 9,1, ref oMissing, ref oMissing);  
            
          //Table - It will only get the last table found for this Word.Range
          Word.Table wrdTable = wrdtableRange.Tables[wrdtableRange.Tables.Count];
           
          wrdTable.AutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitFixed;
          wrdTable.AllowAutoFit = false;
      
//Below are just test values (you can adjust it if you like.)
//The first column width
wrdTable.Columns.Add(accpt_table.Columns[1]).SetWidth(wrdApp.Application.CentimetersToPoints(5f), Word.WdRulerStyle.wdAdjustNone);

//The second column width
wrdTable.Columns.Add(accpt_table.Columns[2]).SetWidth(wrdApp.Application.CentimetersToPoints(2.8f), Word.WdRulerStyle.wdAdjustNone);

//The third column width
wrdTable.Columns.Add(accpt_table.Columns[3]).SetWidth(wrdApp.Application.CentimetersToPoints(3.2f), Word.WdRulerStyle.wdAdjustNone);

//Then set fourth column width            
wrdTable.Columns[4].SetWidth(wrdApp.Application.CentimetersToPoints(4f), Word.WdRulerStyle.wdAdjustNone);

// If you want to see it without opening it just make it to true
wrdApp.Visible = true;
 
Share this answer
 
Comments
Kats2512 30-Jun-17 3:12am    
7 years later???
Adam Sin 1-Jul-17 11:47am    
Well, Its not my fault, 7 years ago I don't even know any programming. I saw this question and I found out the way to do it so I just put my solution there. It works in VS and latest Word but not sure about the older ones. Moreover, I just registered approximately two days or is it three days ago?
Member 12641543 8-Nov-20 21:42pm    
Good on you Adam! Thanks you for contributing. There are others who are searching for these topics and are benefiting from your solution even in 2020. Disregard unconstructive comments like the one posted by Kats2512.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900