Look at Selection.Goto and Selection.Text
So far I have the merged document parsed out so that each page is
saved as an individual document. I am trying to find the employee
field contents based on regular expressions. The text immediately
following "Appraised By:" is what I am looking for to use as part of
each saved document's filename. The problem is that my regex is
consistently coming up short finding anything.
Here's a code snippet below. I know I'm not referencing the Word
document contents correctly and that's why the reference is falling
short. Each pass through the loop is showing the same character count.
And I know each page has a different character count when they're
being saved. Maybe the trouble is that my text is inside of a textbox
in Word???
Code snippet
-------------------------------------------------------------------------------------------------
int letters = wordapp.ActiveDocument.Sections.Count;
int counter = 1;
object homeKeyUnit =
Microsoft.Office.Interop.Word.WdUnits.wdStory;
wordapp.Selection.HomeKey(ref homeKeyUnit, ref objMiss);
while (counter < letters)
{
wordapp.ActiveDocument.Sections.First.Range.Cut();
wordapp.Documents.Add(ref objMiss, ref objMiss, ref
objMiss, ref objMiss);
wordapp.Selection.Paste();
wordapp.ActiveDocument.Sections
[2].PageSetup.SectionStart =
Microsoft.Office.Interop.Word.WdSectionStart.wdSectionContinuous;
object startIndex = 0;
object eindIndex = (object)
wordapp.ActiveDocument.Characters.Count;
Microsoft.Office.Interop.Word.Range range =
wordapp.ActiveDocument.Range(ref startIndex, ref eindIndex);
System.Console.WriteLine("Start Index :
"+startIndex.ToString());
System.Console.WriteLine("End Index :
"+eindIndex.ToString());
System.Console.WriteLine("Range Text : "+range.Text);
string pattern = @"Appraised By:\S+";
MatchCollection matches = Regex.Matches(range.Text,
pattern, RegexOptions.IgnoreCase);
System.Console.WriteLine("Matches :
"+matches.Count.ToString());
System.Console.WriteLine("Hit <Enter> to
continue...");
System.Console.ReadKey();
if (matches.Count == 0)
{
salespersonCode = "CA-"+counter.ToString();
}
else
{
salespersonCode = matches[0].Result
("$'");
}
object docName = SAVE_FILENAME+"-"+salespersonCode
+".doc";
wordapp.ActiveDocument.SaveAs(ref docName, ref
objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref
objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref
objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
wordapp.ActiveDocument.Close(ref objSaveChanges, ref
objOriginalFormat, ref objFalse);
counter += 1;
}- Hide quoted text -
I think this is close to the root of my problem. I checked the
ActiveDocument.Shapes.Count property come up with a count of 4. These
are my four textboxes I have formatted text in for each Word document
page. So now I have to figure out how the heck to pull the text out of
them. Getting closer

~- Hide quoted text -
- Show quoted text -
I was able to finally complete this task. Below I've posted the code
if anyone runs into trouble trying to accomplish something like this.
This short program takes a Word mail merge template document, pulls in
the data source, mail merges into separate files for each page, and
pulls a certain merge field to form part of the unique file name.
Figured I'd post this to help anyone else out there out!
Code snippet
--------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace AppraisalMailMerge
{
class Program
{
static void Main(string[] args)
{
DateTime dt = DateTime.Now;
bool DIRECT_PRINT = false;
string TEMPLATE_FILENAME = "i:\\apps\\ruby\
\arms_appraisals.doc";
string FILE_NAME = "i:\\apps\\ruby\\arms_appraisals.xls";
object SAVE_FILENAME = "i:\\receptionist\\appraisals\
\"+dt.ToString("yyyyMMdd",
System.Globalization.CultureInfo.InvariantCulture);
string salespersonCode = "";
//Open Microsoft Word
Microsoft.Office.Interop.Word.ApplicationClass wordapp =
new Microsoft.Office.Interop.Word.ApplicationClass();
wordapp.Visible = false;
object filename = TEMPLATE_FILENAME;
object objTrue = true;
object objFalse = false;
object objMiss = Type.Missing;
object objSaveChanges =
Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
object objOriginalFormat =
Microsoft.Office.Interop.Word.WdOriginalFormat.wdOriginalDocumentFormat;
object objSqlStmt = "SELECT * FROM `Sheet1$`";
Microsoft.Office.Interop.Word.Document myMergeDocument;
//Open the Template file
myMergeDocument = wordapp.Documents.Open(ref filename, ref
objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref
objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref
objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
myMergeDocument.Select();
//Open the data source
object format =
Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatAuto;
myMergeDocument.MailMerge.OpenDataSource(FILE_NAME, ref
format, ref objFalse, ref objMiss, ref objTrue, ref objFalse, ref
objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref
objMiss, ref objMiss, ref objSqlStmt, ref objMiss, ref objMiss);
//Perform the Mail Merge!!!
try
{
if (DIRECT_PRINT)
myMergeDocument.MailMerge.Destination =
Microsoft.Office.Interop.Word.WdMailMergeDestination.wdSendToPrinter;
else
myMergeDocument.MailMerge.Destination =
Microsoft.Office.Interop.Word.WdMailMergeDestination.wdSendToNewDocument;
myMergeDocument.MailMerge.SuppressBlankLines = true;
myMergeDocument.MailMerge.DataSource.FirstRecord =
(int)
Microsoft.Office.Interop.Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord;
myMergeDocument.MailMerge.DataSource.LastRecord = (int)
Microsoft.Office.Interop.Word.WdMailMergeDefaultRecord.wdDefaultLastRecord;
myMergeDocument.MailMerge.Execute(ref objFalse);
}
catch (Exception e)
{
return;
}
//Close the template document.
myMergeDocument.Close(ref objSaveChanges, ref
objOriginalFormat, ref objFalse);
//Print the derived document.
int letters = wordapp.ActiveDocument.Sections.Count;
int counter = 1;
object homeKeyUnit =
Microsoft.Office.Interop.Word.WdUnits.wdStory;
wordapp.Selection.HomeKey(ref homeKeyUnit, ref objMiss);
while (counter < letters)
{
wordapp.ActiveDocument.Sections.First.Range.Cut();
wordapp.Documents.Add(ref objMiss, ref objMiss, ref
objMiss, ref objMiss);
wordapp.Selection.Paste();
wordapp.ActiveDocument.Sections
[2].PageSetup.SectionStart =
Microsoft.Office.Interop.Word.WdSectionStart.wdSectionContinuous;
object index = 3;
Microsoft.Office.Interop.Word.Shape shape =
wordapp.ActiveDocument.Shapes.get_Item(ref index);
Microsoft.Office.Interop.Word.Range range =
shape.TextFrame.TextRange;
string pattern = @"Appraised By:";
MatchCollection matches = Regex.Matches(range.Text,
pattern, RegexOptions.IgnoreCase);
if (matches.Count == 0)
{
salespersonCode = "CA-"+counter.ToString();
}
else
{
salespersonCode = matches[0].Result("$'").Trim()
+"-"+counter.ToString();
}
object docName = SAVE_FILENAME+"-"+salespersonCode
+".doc";
wordapp.ActiveDocument.SaveAs(ref docName, ref
objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref
objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref
objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
wordapp.ActiveDocument.Close(ref objSaveChanges, ref
objOriginalFormat, ref objFalse);
counter += 1;
}
wordapp = null;
return;
}
}
}