Scraping off a Word document?

G

gregarican

Here's my theoretical situation. I have a Word mail merge document.
There's a merge field that is of interest on each page of the merged
document. Let's call it the Employee field. Currently these pages are
sorted by this field. What I need to do is iterate through the pages
and save off each group of Employees as a separate Word document. So
if the first five pages of the merged document have Employee: Joe
Schmoe present I'd save these five pages off and call the document
"Joe Schmoe.Doc" and so on.

Any suggestions on where to start on this endeavor? I know I can
invoke Word OLE method calls and whatnot to start things off...
 
R

RayLopez99

Here's my theoretical situation. I have a Word mail merge document.
There's a merge field that is of interest on each page of the merged

I actually did something similar way back when. I used (from memory)
Visual Basic. I believe I actually worked from within Word--I think
Word has a powerful macro language that is built on Visual Basic. I
recall in a few hours of coding I accomplished what you want to do.

So, unless you have a compelling need to use C#, I would use VB from
inside of Word. In fact, as somebody here can tell you better than I
(actually I'm curious as well) you can call another app from inside of
C#, so just call up the Word macro from C# after generating this Word
macro.

That is, my question to anybody reading this: how do you call up an
app from inside of C#? Let's say you want to run an instance of Adobe
Acrobat or Microsoft Word from a C# generated form... it's in the Help
files I'm sure but a few keywords and pointers would be helpful for
me.

RL
 
M

Marty Honea

Ray,

http://blogs.msdn.com/csharpfaq/archive/2004/06/01/146375.aspx

This link tells you how.

System.Diagnostics.Process.Start(@"C:\listfiles.bat");


But this link is more elegant

http://www.csharp-station.com/HowTo/ProcessStart.aspx

Process notePad = new Process();

notePad.StartInfo.FileName = "notepad.exe";
notePad.StartInfo.Arguments = "ProcessStart.cs";

notePad.Start();



Marty
Here's my theoretical situation. I have a Word mail merge document.
There's a merge field that is of interest on each page of the merged

I actually did something similar way back when. I used (from memory)
Visual Basic. I believe I actually worked from within Word--I think
Word has a powerful macro language that is built on Visual Basic. I
recall in a few hours of coding I accomplished what you want to do.

So, unless you have a compelling need to use C#, I would use VB from
inside of Word. In fact, as somebody here can tell you better than I
(actually I'm curious as well) you can call another app from inside of
C#, so just call up the Word macro from C# after generating this Word
macro.

That is, my question to anybody reading this: how do you call up an
app from inside of C#? Let's say you want to run an instance of Adobe
Acrobat or Microsoft Word from a C# generated form... it's in the Help
files I'm sure but a few keywords and pointers would be helpful for
me.

RL
 
G

gregarican

Ray,

   http://blogs.msdn.com/csharpfaq/archive/2004/06/01/146375.aspx

This link tells you how.

System.Diagnostics.Process.Start(@"C:\listfiles.bat");

But this link is more elegant

http://www.csharp-station.com/HowTo/ProcessStart.aspx

            Process notePad = new Process();

            notePad.StartInfo.FileName   = "notepad.exe";
            notePad.StartInfo.Arguments = "ProcessStart.cs";

            notePad.Start();




I actually did something similar way back when.  I used (from memory)
Visual Basic.  I believe I actually worked from within Word--I think
Word has a powerful macro language that is built on Visual Basic.  I
recall in a few hours of coding I accomplished what you want to do.

So, unless you have a compelling need to use C#, I would use VB from
inside of Word.  In fact, as somebody here can tell you better than I
(actually I'm curious as well) you can call another app from inside of
C#, so just call up the Word macro from C# after generating this Word
macro.

That is, my question to anybody reading this:  how do you call up an
app from inside of C#?  Let's say you want to run an instance of Adobe
Acrobat or Microsoft Word from a C# generated form... it's in the Help
files I'm sure but a few keywords and pointers would be helpful for
me.

RL

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;
}
 
G

gregarican

This link tells you how.

But this link is more elegant

            Process notePad = new Process();
            notePad.StartInfo.FileName   = "notepad.exe";
            notePad.StartInfo.Arguments = "ProcessStart.cs";
            notePad.Start();
Marty"RayLopez99" <[email protected]> wrote in message
I actually did something similar way back when.  I used (from memory)
Visual Basic.  I believe I actually worked from within Word--I think
Word has a powerful macro language that is built on Visual Basic.  I
recall in a few hours of coding I accomplished what you want to do.
So, unless you have a compelling need to use C#, I would use VB from
inside of Word.  In fact, as somebody here can tell you better than I
(actually I'm curious as well) you can call another app from inside of
C#, so just call up the Word macro from C# after generating this Word
macro.
That is, my question to anybody reading this:  how do you call up an
app from inside of C#?  Let's say you want to run an instance of Adobe
Acrobat or Microsoft Word from a C# generated form... it's in the Help
files I'm sure but a few keywords and pointers would be helpful for
me.

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 -

- Show 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 :)~
 
M

Marty Honea

Look at Selection.Goto and Selection.Text


Ray,

This link tells you how.

But this link is more elegant

Process notePad = new Process();
notePad.StartInfo.FileName = "notepad.exe";
notePad.StartInfo.Arguments = "ProcessStart.cs";

Marty"RayLopez99" <[email protected]> wrote in message
I actually did something similar way back when. I used (from memory)
Visual Basic. I believe I actually worked from within Word--I think
Word has a powerful macro language that is built on Visual Basic. I
recall in a few hours of coding I accomplished what you want to do.
So, unless you have a compelling need to use C#, I would use VB from
inside of Word. In fact, as somebody here can tell you better than I
(actually I'm curious as well) you can call another app from inside of
C#, so just call up the Word macro from C# after generating this Word
macro.
That is, my question to anybody reading this: how do you call up an
app from inside of C#? Let's say you want to run an instance of Adobe
Acrobat or Microsoft Word from a C# generated form... it's in the Help
files I'm sure but a few keywords and pointers would be helpful for
me.

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 -

- Show 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 :)~
 
G

gregarican

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 -
- Show 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 am looking for a value in the fourth textbox on each Word page. The
code below fits the bill. Just wanted to post this in case any other
poor souls are miring through the quagmire of Office COM :)

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 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;
}
 
G

gregarican

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 -
- Show 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;

}

}
}
 

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