|
|
Hello
How do you make a Word document call another Word document and execute the code in the second Word document"?
Note: the last line in this1.cs' ie ThisDocument_Startup calls this2.cs and inserts it into this1.cs then it needs to execute the code inside this2.cs that creates a table itself ie this2.cs, the Open statement in the second Word document is commented out because it is already open
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
|
|
Hi JB,
You posted your question in a VBA group but your code is VB.Net I think? However, I can tell you how this is done in VBA, perhaps you can translate this to your own environment.
Make sure the the code you want to call is in the ThisDocument class of the document and that it is a public sub.
For example: (Code in Doc1)
Sub OpenAndCallDoc2()
Dim oDoc As Word.Document Set oDoc = Documents.Open("C:\Doc2.doc") oDoc.Test set oDoc = Nothing
End Sub
(Code in ThisDocument module in Doc2) Public Sub Test() MsgBox "This is document two" End Sub
Hope this helps, kind regards, Astrid
"JB" wrote:
[Quoted Text] > Hello > > How do you make a Word document call another > Word document and execute the code in the second > Word document"? > > Note: the last line in this1.cs' ie ThisDocument_Startup calls > this2.cs and inserts it into this1.cs then it needs to execute the code > inside this2.cs that creates a table itself ie this2.cs, > the Open statement in the second Word document is commented out > because it is already open > > 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
|
|
|