H
H R Graglia
Hi,
I'm trying to write some C# code that automates Word to do the following:
- Open a document
- Attach my custom schema
- Search the document for some specific text
- When I find the text, create an XML Node that includes an attribute
- Save the document in XML format
First off, I'm a total rookie at this. But here's a bit of the code I wrote:
private void button1_Click(object sender, System.EventArgs e)
{
object sTokenPattern = "\\<%[A-Za-z.0-9]@%\\>";
object sReplace = "";
object wrap = Word.WdFindWrap.wdFindStop;
Word.WdSeekView[] locations =
{
Word.WdSeekView.wdSeekCurrentPageHeader,
Word.WdSeekView.wdSeekCurrentPageFooter,
Word.WdSeekView.wdSeekMainDocument
};
wordApp.ActiveWindow.View.Type = Word.WdViewType.wdPrintView;
for (int x=0; x < locations.Length; x++)
{
wordApp.ActiveWindow.ActivePane.View.SeekView = locations[x];
bool bFound = true;
while (bFound)
{
bFound = wordApp.Selection.Find.Execute(
ref sTokenPattern, // FindText
ref bFalse, // MatchCase
ref bFalse, // MatchWholeWord
ref bTrue, // MatchWildcards
ref bFalse, // MatchSoundsLike
ref bFalse, // MatchAllWordForms
ref bTrue, // Forward
ref wrap, // Wrap
ref bFalse, // Format
ref sReplace, // ReplaceWith
ref none, // Replace
ref bFalse, // MatchKashida
ref bFalse, // MatchDiacritics
ref bFalse, // MatchAlefHamza
ref bTrue); // MatchControl
if (bFound)
{
String sValue = wordApp.Selection.Text;
object ch = Word.WdUnits.wdCharacter;
object one = 1;
object range = wordApp.Selection.Range;
Word.XMLNode tokenNode =
wordApp.ActiveDocument.XMLNodes.Add("Token", (string)FCINS, ref range);
Word.XMLNode nameAttr =
tokenNode.Attributes.Add("tokenName", "", ref missing);
nameAttr.NodeValue = sValue;
wordApp.Selection.MoveRight(ref ch, ref one, ref
bFalse);
}
}
}
Now here's my problem. When the location is the main document, everything
works fine. But when the location is either the header or the footer, I end
up losing my attribute. That is, the code seems to execute just fine, but
after saving the document and inspecting the XML, only the nodes that were
created in the main document have their attributes. Here's some sample...
main doc -
<ns0:Token name="<%Common.DisplayName%>">
<w:r>
<w:t><%Common.DisplayName%></w:t>
</w:r>
</ns0:Token>
header -
<ns0:Token> <== NOTICE MY NAME ATTRIBUTE IS MISSING
<w:r>
<w:t><%CurrentUser.Name%></w:t>
</w:r>
</ns0:Token>
Am I way off base here? Any advice would be greatly appreciated.
-Rob
I'm trying to write some C# code that automates Word to do the following:
- Open a document
- Attach my custom schema
- Search the document for some specific text
- When I find the text, create an XML Node that includes an attribute
- Save the document in XML format
First off, I'm a total rookie at this. But here's a bit of the code I wrote:
private void button1_Click(object sender, System.EventArgs e)
{
object sTokenPattern = "\\<%[A-Za-z.0-9]@%\\>";
object sReplace = "";
object wrap = Word.WdFindWrap.wdFindStop;
Word.WdSeekView[] locations =
{
Word.WdSeekView.wdSeekCurrentPageHeader,
Word.WdSeekView.wdSeekCurrentPageFooter,
Word.WdSeekView.wdSeekMainDocument
};
wordApp.ActiveWindow.View.Type = Word.WdViewType.wdPrintView;
for (int x=0; x < locations.Length; x++)
{
wordApp.ActiveWindow.ActivePane.View.SeekView = locations[x];
bool bFound = true;
while (bFound)
{
bFound = wordApp.Selection.Find.Execute(
ref sTokenPattern, // FindText
ref bFalse, // MatchCase
ref bFalse, // MatchWholeWord
ref bTrue, // MatchWildcards
ref bFalse, // MatchSoundsLike
ref bFalse, // MatchAllWordForms
ref bTrue, // Forward
ref wrap, // Wrap
ref bFalse, // Format
ref sReplace, // ReplaceWith
ref none, // Replace
ref bFalse, // MatchKashida
ref bFalse, // MatchDiacritics
ref bFalse, // MatchAlefHamza
ref bTrue); // MatchControl
if (bFound)
{
String sValue = wordApp.Selection.Text;
object ch = Word.WdUnits.wdCharacter;
object one = 1;
object range = wordApp.Selection.Range;
Word.XMLNode tokenNode =
wordApp.ActiveDocument.XMLNodes.Add("Token", (string)FCINS, ref range);
Word.XMLNode nameAttr =
tokenNode.Attributes.Add("tokenName", "", ref missing);
nameAttr.NodeValue = sValue;
wordApp.Selection.MoveRight(ref ch, ref one, ref
bFalse);
}
}
}
Now here's my problem. When the location is the main document, everything
works fine. But when the location is either the header or the footer, I end
up losing my attribute. That is, the code seems to execute just fine, but
after saving the document and inspecting the XML, only the nodes that were
created in the main document have their attributes. Here's some sample...
main doc -
<ns0:Token name="<%Common.DisplayName%>">
<w:r>
<w:t><%Common.DisplayName%></w:t>
</w:r>
</ns0:Token>
header -
<ns0:Token> <== NOTICE MY NAME ATTRIBUTE IS MISSING
<w:r>
<w:t><%CurrentUser.Name%></w:t>
</w:r>
</ns0:Token>
Am I way off base here? Any advice would be greatly appreciated.
-Rob