Bookmark VSTO error "requested member of collection does not exist

J

JB

hello

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.ToString();
tblData.Add(adr);
i++;
edr = funcData.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)
{
}
}
}
 
J

Jay Freedman

The problem is that attaching a template to a Word document that already exists
does _not_ transfer a bookmark from the template to the document -- that would
happen only if you created the document by basing it on the template.

See http://www.shaunakelly.com/word/attachtemplate/index.html. Although that
article is written from the point of view of the user interface, the principle
is the same for programmatic manipulation.

hello

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:

<snip>
 
D

Doug Robbins - Word MVP

Please do not post the same question separately to multiple newsgroups. I
have just taken the time to study your code in the other newsgroup to which
you posted this and came to the same conclusion as that reached by Jay
Freedman, only to find his response already here when I looked at this
newsgroup.

--
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 said:
hello

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.ToString();
tblData.Add(adr);
i++;
edr = funcData.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)
{
}
}
}
 
J

JB

Hello Jay

You are correct. By creating the document based on the template, the
bookmark then is available to the Word document for programatic use.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top