Hello I am using C# to call Microsoft Word and then I am building a table in Microsoft Word from an ArrayList. I create the table successfully in Microsoft Word from the ArrayList. But I need to format the table, so I run a macro that memorizes my keystrokes as I use the toolbar and select "table Properties" and set the properties for the column sizes and other formatting options. However, since the document already exists and has images, labels and text in it, the macro doesn't run and the message "The SelectColumn method or property is not available because some or all of the object does not refer to a table". So I try to copy the code from the keystrokes and past them in my code beneath the code that creates the table which means I am basically trying to programmatically position and format the table but the code doesn't run. My question is:
How can I get the code or macro that positions and formats a table in Microsft Word after C# creates the table in Microsoft Word?
Below is the code which shows after I create the table I try to programmatically position it and format it using the macro keystrokes that I ran:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; using plist; using System.Reflection; using Word = Microsoft.Office.Interop.Word; using Office = Microsoft.Office.Core;
namespace plist { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string adr = " "; string edr = " "; ArrayList funcData = new ArrayList(); ArrayList arrayData = new ArrayList(); ArrayList tblData = new ArrayList(); funcData = pgm2.AEdr(arrayData);
for (int i = 0; i < funcData.Count; i++) { adr = funcData[i].ToString(); tblData.Add(adr); i++; edr = funcData[i].ToString(); tblData.Add(edr); }
int numRows; int numCols; numRows = (funcData.Count / 2); numCols = 2;
object oMissing = System.Reflection.Missing.Value;
Word.ApplicationClass oWord = new Word.ApplicationClass(); oWord.Visible = true; Word.Documents oDocs = oWord.Documents;
object oFile = "c:\\ManUnDoc.doc";
Word._Document oDoc = oDocs.Open(ref oFile, 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);
int rNum = 0 ; int cNum = 0 ; int a = 0; Object start = 250; Object end = 250;
Word.Range docRange = oDoc.Range(ref start, ref end); //Word.Range docRange = oDoc.Content; Word.Table docTable = docRange.Tables.Add(docRange, numRows, numCols, ref oMissing, ref oMissing);
for (int i = 0; i < (tblData.Count / 2); i++) { rNum++; docTable.Cell(rNum, cNum).Range.Text = tblData[a].ToString(); a = a + 2; }
int rNum2 = 0; int cNum2 = 2; int a2 = 1;
for (int i = 0; i < (tblData.Count / 2); i++) { rNum2++; docTable.Cell(rNum2, cNum2).Range.Text = tblData[a2].ToString(); a2 = a2 + 2; } Selection.MoveDown Unit:=wdLine, Count:=5; Selection.TypeText Text:=" "; Selection.MoveRight Unit:=wdCharacter, Count:=4; Selection.TypeText Text:=" "; Selection.Collapse Direction:=wdCollapseStart; Selection.Move Unit:=wdColumn, Count:=-1; Selection.SelectColumn; Selection.Columns.PreferredWidthType = wdPreferredWidthPoints; Selection.Columns.PreferredWidth = InchesToPoints(1.63); Selection.Move Unit:=wdColumn, Count:=1; Selection.SelectColumn; Selection.Columns.PreferredWidthType = wdPreferredWidthPoints; Selection.Columns.PreferredWidth = InchesToPoints(4.38); Selection.Collapse Direction:=wdCollapseStart; Selection.Move Unit:=wdColumn, Count:=-1; Selection.SelectColumn; Selection.Tables(1).Rows.LeftIndent = InchesToPoints(1.6); Selection.Tables(1).PreferredWidthType = wdPreferredWidthPoints; Selection.Tables(1).PreferredWidth = InchesToPoints(6); Selection.MoveUp Unit:=wdLine, Count:=1; Selection.Delete Unit:=wdCharacter, Count:=1; Selection.TypeBackspace; Selection.TypeBackspace; Selection.TypeBackspace; Selection.TypeBackspace; Selection.TypeBackspace; Selection.TypeBackspace; Selection.TypeBackspace; Selection.TypeBackspace; Selection.TypeBackspace; Selection.Delete Unit:=wdCharacter, Count:=1 //RunMacro(oWord, new Object[] { "PlaceCursorHere" }); //RunMacro(oWord, new Object[] { "PlaceTableHere" });
} private void RunMacro(object oApp, object[] oRunArgs) { oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs); }
// // //oDoc.Close(ref oMissing, ref oMissing, ref oMissing); //System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc); //oDoc = null; //System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocs); //oDocs = null; //oWord.Quit(ref oMissing, ref oMissing, ref oMissing); //System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord); //oWord = null; private void label1_Click(object sender, EventArgs e) { } } }
|
|