Group:  Microsoft Word ยป microsoft.public.word.vba.customization
Thread: Word documents open another

Word documents open another
JB 12/6/2008 1:04:01 AM
Hello

I have an C# program that calls Microsoft Word interop.
First I open a Word document using interop.
Next I automatically insert another Word document into
the first Word document that I opened using interop but the second
Word document needs to execute some code without opening itself. Since
it is already open it I doesnt need it to open itself instead
it should execute the code.

How do you make "this1.cs" execute the
the code in "this2.cs" and execute the code to
create the table on the Word
document within itself ie "this2.cs" and not the
Word document in this1.cs

Note: the last line in this1.cs' ThisDocument_Startup calls
this2.cs and inserts it into this1.cs

I have provided the code snippets below:

//**************** this1.cs
namespace CovProj
{
public partial class ThisDocument
{

private void ThisDocument_Startup(object sender, System.EventArgs e)
{
object oMissing = System.Reflection.Missing.Value;

Word.ApplicationClass oWord = new Word.ApplicationClass();
oWord.Visible = true;
Word.Documents oDocs = oWord.Documents;
object oFile = "c:\\CovDoc.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);

string strBoro = "x";
string strDiv1 = "x";
string strDiv2 = "x";
string strDiv3 = "x";
string strDiv4 = "x";
string strDiv5 = "x";
string strDiv6 = "x";
DateTime curryear = DateTime.Now;
string cyear = String.Format("{0:MMMM d, yyyy}", curryear);

object tempcovdate = "ShowDate";
Word.Range covdate = oDoc.Bookmarks.get_Item(ref
tempcovdate).Range;
covdate.Text = cyear.ToString();


object tempboro = "x";
Word.Range borodiv = oDoc.Bookmarks.get_Item(ref tempboro).Range;
borodiv.Text = strBoro.ToString();

object tempdiv1 = "div1";
Word.Range divdiv1 = oDoc.Bookmarks.get_Item(ref tempdiv1).Range;
divdiv1.Text = strDiv1.ToString();

object tempdiv2 = "div2";
Word.Range divdiv2 = oDoc.Bookmarks.get_Item(ref tempdiv2).Range;
divdiv2.Text = strDiv2.ToString();

object tempdiv3 = "div3";
Word.Range divdiv3 = oDoc.Bookmarks.get_Item(ref tempdiv3).Range;
divdiv3.Text = strDiv3.ToString();

object tempdiv4 = "div4";
Word.Range divdiv4 = oDoc.Bookmarks.get_Item(ref tempdiv4).Range;
divdiv4.Text = strDiv4.ToString();

object tempdiv5 = "div5";
Word.Range divdiv5 = oDoc.Bookmarks.get_Item(ref tempdiv5).Range;
divdiv5.Text = strDiv5.ToString();

object tempdiv6 = "div6";
Word.Range divdiv6 = oDoc.Bookmarks.get_Item(ref tempdiv6).Range;
divdiv6.Text = strDiv6.ToString();

object insDoc = "UnEstab";
String oFilePath = "C:\\UnEstDoc.doc";
oDoc.Bookmarks.get_Item(ref insDoc).Range.InsertFile(oFilePath,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);

}

private void ThisDocument_Shutdown(object sender, System.EventArgs e)
{
}

#region VSTO Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisDocument_Startup);
this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
}

#endregion
}
}

//********************* this2.cs

namespace UnEstProj
{
public partial class ThisDocument
{
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
object oMissing = System.Reflection.Missing.Value;

Word.ApplicationClass oWord = new Word.ApplicationClass();
oWord.Visible = true;
Word.Documents oDocs = oWord.Documents;
object unFile = "c:\\UnEstDoc.doc";

Word._Document oDoc = oDocs.Open(ref unFile, 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);

DateTime curryear = DateTime.Now;
int cyear = (curryear.Year - 1);
string uboro = "Manhattan";

object tempunyear = "abbrevyr";
Word.Range unyear = oDoc.Bookmarks.get_Item(ref tempunyear).Range;
unyear.Text = cyear.ToString();

object tempunboro = "tboro";
Word.Range unboro = oDoc.Bookmarks.get_Item(ref tempunboro).Range;
unboro.Text = uboro.ToString();



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;
int rNum = 0;
int cNum = 0;
int a = 0;
object tempPosTable = "untbl";
Word.Range posTable = oDoc.Bookmarks.get_Item(ref
tempPosTable).Range;
Word.Table FTable = posTable.Tables.Add(posTable, numRows,
numCols, ref oMissing, ref oMissing);

for (int i = 0; i < (tblData.Count / 2); i++)
{
rNum++;
FTable.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++;
FTable.Cell(rNum2, cNum2).Range.Text = tblData[a2].ToString();
a2 = a2 + 2;
}

FTable.Rows.LeftIndent = 113;
FTable.PreferredWidth = 432;
RunMacro(oWord, new Object[] { "firstind" });
FTable.Columns[1].PreferredWidth = oWord.InchesToPoints(2);
FTable.Columns[2].PreferredWidth = oWord.InchesToPoints(4);
RunMacro(oWord, new Object[] { "crtGrid" });
}

private void RunMacro(object oApp, object[] oRunArgs)
{
oApp.GetType().InvokeMember("Run",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.InvokeMethod,
null, oApp, oRunArgs);
}

private void ThisDocument_Shutdown(object sender, System.EventArgs e)
{
}

#region VSTO Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisDocument_Startup);
this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
}

#endregion
}
}

--
JB

Home | Search | Terms | Imprint
Newsgroups Reader