|
|
Using VSTO with C# I have created a table in Word. I also created a template with a bookmark for the document that gets opened to use in it in order to position the table at a certain point in the document. However when I run the application there is an error message that says "requested member of collection does not exist". I don't know why the error appears because the template that I created resides in the Microsoft template folder and it was added to the Word document by selecting "Tools, Templates and Add-Ins" from the toolbar before the application is run but when the C# program opens the document to build the table in and use the bookmark in that template associated that was added to the document the error message still says "requested member of collection does not exist". I know it exists because when I created the Template I can still see it is in there and when I open the document and look select "Tools, Templates and Add-Ins" I still see the template in the template directory that it is attached to. So my question is what am I doing wrong? below is the code:
using System; using System.Data; using System.Drawing; using System.Windows.Forms; using Microsoft.VisualStudio.Tools.Applications.Runtime; using Word = Microsoft.Office.Interop.Word; using Office = Microsoft.Office.Core; using System.Collections;
namespace wdManhattanCal { public partial class ThisDocument { private void ThisDocument_Startup(object sender, System.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:\\ManhattanUnEst.doc"; object tempManUn = "C:\\Documents and Settings\\dname\\Application Data\\Microsoft\\Templates\\bmManhattanUnest";
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;
Word.Range docRange = oDoc.Bookmarks.get_Item(ref tempManUn).Range; Word.Table docTable = docRange.Tables.Add(docRange, numRows, numCols, ref oMissing, ref oMissing);
for (int i = 0; i < (tblData.Count / 2); i++) //for (int i = 0; i < 3; 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; }
}
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) { }
} }
|
|
While I do not know much about C#, looking at your code, it appears that rather than creating a document from the template by using (in vba, it would be) Documents.Add "templatename", that you are opening a document and then attaching a template do it.
That would not cause a bookmark that is present in the template to be present in the document.
-- Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
"JB" <JB[ at ]discussions.microsoft.com> wrote in message news:3CD940DD-609A-4CD7-90AE-2E73D272AE09[ at ]microsoft.com...
[Quoted Text] > Using VSTO with C# I have created a table in Word. I also created a > template with a bookmark for the document that gets opened to use in it in > order to position the table at a certain point in the document. However > when > I run the application there is an error message that says "requested > member > of collection does not exist". I don't know why the error appears because > the template that I created resides in the Microsoft template folder and > it > was added to the Word document by selecting "Tools, Templates and Add-Ins" > from the toolbar before the application is run but when the C# program > opens > the document to build the table in and use the bookmark in that template > associated that was added to the document the error message still says > "requested member of collection does not exist". I know it exists because > when I created the Template I can still see it is in there and when I open > the document and look select "Tools, Templates and Add-Ins" I still see > the > template in the template directory that it is attached to. So my question > is > what am I doing wrong? below is the code: > > > using System; > using System.Data; > using System.Drawing; > using System.Windows.Forms; > using Microsoft.VisualStudio.Tools.Applications.Runtime; > using Word = Microsoft.Office.Interop.Word; > using Office = Microsoft.Office.Core; > using System.Collections; > > namespace wdManhattanCal > { > public partial class ThisDocument > { > private void ThisDocument_Startup(object sender, System.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:\\ManhattanUnEst.doc"; > object tempManUn = "C:\\Documents and > Settings\\dname\\Application > Data\\Microsoft\\Templates\\bmManhattanUnest"; > > 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; > > Word.Range docRange = oDoc.Bookmarks.get_Item(ref > tempManUn).Range; > Word.Table docTable = docRange.Tables.Add(docRange, numRows, > numCols, ref oMissing, ref oMissing); > > for (int i = 0; i < (tblData.Count / 2); i++) > //for (int i = 0; i < 3; 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; > } > > } > > 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) > { > } > > > > } > } > >
|
|
Thank you Doug for letting me know to reply back to the newsgroup. The way I replied back was that I clicked the "Yes" button at the bottom of your response. -- JB
"Doug Robbins - Word MVP" wrote:
[Quoted Text] > While I do not know much about C#, looking at your code, it appears that > rather than creating a document from the template by using (in vba, it would > be) Documents.Add "templatename", that you are opening a document and then > attaching a template do it. > > That would not cause a bookmark that is present in the template to be > present in the document. > > -- > Hope this helps. > > Please reply to the newsgroup unless you wish to avail yourself of my > services on a paid consulting basis. > > Doug Robbins - Word MVP > > "JB" <JB[ at ]discussions.microsoft.com> wrote in message > news:3CD940DD-609A-4CD7-90AE-2E73D272AE09[ at ]microsoft.com... > > Using VSTO with C# I have created a table in Word. I also created a > > template with a bookmark for the document that gets opened to use in it in > > order to position the table at a certain point in the document. However > > when > > I run the application there is an error message that says "requested > > member > > of collection does not exist". I don't know why the error appears because > > the template that I created resides in the Microsoft template folder and > > it > > was added to the Word document by selecting "Tools, Templates and Add-Ins" > > from the toolbar before the application is run but when the C# program > > opens > > the document to build the table in and use the bookmark in that template > > associated that was added to the document the error message still says > > "requested member of collection does not exist". I know it exists because > > when I created the Template I can still see it is in there and when I open > > the document and look select "Tools, Templates and Add-Ins" I still see > > the > > template in the template directory that it is attached to. So my question > > is > > what am I doing wrong? below is the code: > > > > > > using System; > > using System.Data; > > using System.Drawing; > > using System.Windows.Forms; > > using Microsoft.VisualStudio.Tools.Applications.Runtime; > > using Word = Microsoft.Office.Interop.Word; > > using Office = Microsoft.Office.Core; > > using System.Collections; > > > > namespace wdManhattanCal > > { > > public partial class ThisDocument > > { > > private void ThisDocument_Startup(object sender, System.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:\\ManhattanUnEst.doc"; > > object tempManUn = "C:\\Documents and > > Settings\\dname\\Application > > Data\\Microsoft\\Templates\\bmManhattanUnest"; > > > > 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; > > > > Word.Range docRange = oDoc.Bookmarks.get_Item(ref > > tempManUn).Range; > > Word.Table docTable = docRange.Tables.Add(docRange, numRows, > > numCols, ref oMissing, ref oMissing); > > > > for (int i = 0; i < (tblData.Count / 2); i++) > > //for (int i = 0; i < 3; 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; > > } > > > > } > > > > 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) > > { > > } > > > > > > > > } > > } > > > > > > >
|
|
|