TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Shawn
NA
2
0
Adding an image to word document.
Jan 12 2009 2:26 PM
Ok i hope someone has an anwers as i've been out of luck so far. I am creating a word document on the fly and can get all the text to work but i can't get an image inserted. It's a letter that is generated that is sent to a large number of people and i'd like to add a jpg of someones signature so they won't have to sign all 2-3 thousand letters. Anyone have any ideas? Thanks and please see code below.
private void btnAlternateCreate_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection concurrent = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source= " +
this.txtSourcePath.Text + ";Extended Properties=Excel 8.0");
System.Data.OleDb.OleDbCommand commembers = new System.Data.OleDb.OleDbCommand("SELECT * FROM [Members$]", concurrent);
System.Data.OleDb.OleDbCommand comdues = new System.Data.OleDb.OleDbCommand("SELECT * FROM [2006Dues$]", concurrent);
System.Data.OleDb.OleDbDataReader readermembers;
System.Data.OleDb.OleDbDataReader readerdues;
System.Data.DataTable dtmembers = new DataTable("members");
System.Data.DataTable dtdues = new DataTable("dues");
System.Object myfalse = false;
System.Object mymissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordapp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document worddoc;
System.Object mypagebreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
System.Object mycollapsetoend = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
System.Object myzero = 0;
string _namelast = string.Empty;
string _namefirst = string.Empty;
string _address1 = string.Empty;
string _address2 = string.Empty;
string _city = string.Empty;
string _province = string.Empty;
string _postalcode = string.Empty;
string _duespaid = string.Empty;
string _outputstring = string.Empty;
string _pagetitle = "Company Title";
string _pageaddress = "Address here";
string _body1 = "It is hereby certified that";
string _body2 = "was a member of this in";
string _body3 = "and out of the annual dues (not including initiation " +
"fees) paid by her(him) for that calendar year the sum of";
string _body4 = "i. " +
"No " +
"a " +
"n.\n\n";
string _summaryshop = string.Empty;
string _summaryclock = string.Empty;
string _summaryledger = string.Empty;
//test code
//Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional bc = new Neodynamic.WinControls.BarcodeProfessional.BarcodeProfessional();
//Microsoft.Office.Interop.Word.Range range = worddoc.Bookmarks.get_Item(ref oBarcodeBookmark).Range;
//Image _Shawntest = Image.FromFile(@"C:\Unite Dues Copy\LoisSignFinal.bmp");
//range = wordDoc.Bookmarks.Item(oBarcodeBookmark).Range;
//Image = Image.FromFile(@"C:\Unite Dues Copy\LoisSignFinal.jpg");
// _colShapes = worddoc.Shapes;
//_objShape = _colShapes. inlineshapes.addpicture(@"C:\LoisSignFinal.jpg");
//System.Object objShape = Microsoft.Office.Interop.Word.InlineShapes.AddPicture(@"C:\Scripts\Chart.gif");
string _summarysignatureline = string.Empty;
string _summarysignaturedescription = string.Empty;
bool _haserrors = false;
try
{
// load the data grids with data.
System.Diagnostics.Debug.WriteLine("Tax Report tool run at: " + System.DateTime.Now.ToShortDateString());
System.Diagnostics.Debug.WriteLine("Specified Local: " + this.txtLocalNumber.Text);
concurrent.Open();
System.Diagnostics.Debug.WriteLine("loading member data...");
readermembers = commembers.ExecuteReader();
dtmembers.Load(readermembers);
System.Diagnostics.Debug.WriteLine("loading member data done.");
System.Diagnostics.Debug.WriteLine("loading dues data...");
readerdues = comdues.ExecuteReader();
dtdues.Load(readerdues);
System.Diagnostics.Debug.WriteLine("loading dues data done.");
concurrent.Close();
System.Diagnostics.Debug.WriteLine("creating dataviews...");
System.Data.DataView dvcurrentmember = new DataView(dtmembers, "Local = " + this.txtLocalNumber.Text,
"shop, clock", DataViewRowState.CurrentRows);
System.Data.DataView dvcurrentdues = new DataView(dtdues, "Local = " + this.txtLocalNumber.Text,
"shop, clock", DataViewRowState.CurrentRows);
System.Diagnostics.Debug.WriteLine("creating dataviews done.");
System.Diagnostics.Debug.WriteLine("Member datatable contains " + dvcurrentmember.Count.ToString() +
" rows for local " + this.txtLocalNumber.Text);
System.Diagnostics.Debug.WriteLine("Dues datatable contains " + dvcurrentdues.Count.ToString() +
" rows for local " + this.txtLocalNumber.Text);
// create a word document.
worddoc = wordapp.Documents.Add(ref mymissing, ref mymissing,
ref mymissing, ref mymissing);
// set up the page format.
worddoc.PageSetup.LeftMargin = 36F;
worddoc.PageSetup.RightMargin = 36F;
worddoc.PageSetup.TopMargin = 36F;
worddoc.PageSetup.BottomMargin = 36F;
Microsoft.Office.Interop.Word.Range rngstart;
rngstart = worddoc.Range(ref mymissing, ref mymissing);
rngstart.Collapse(ref mycollapsetoend);
System.Object headername = "uhmheader";
Microsoft.Office.Interop.Word.Style styleheader = worddoc.Styles.Add((string)headername, ref mymissing);
styleheader.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
styleheader.Font.Size = 12;
styleheader.Font.Bold = 1;
System.Object mystyleheader = styleheader;
System.Object bodyname = "uhmbody";
Microsoft.Office.Interop.Word.Style stylebody = worddoc.Styles.Add((string)bodyname, ref mymissing);
stylebody.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;
stylebody.Font.Size = 10;
stylebody.Font.Bold = 0;
System.Object mystylebody = stylebody;
System.Object bodyemphasis = "uhmbodyemphasis";
Microsoft.Office.Interop.Word.Style stylebodyemphasis = worddoc.Styles.Add((string)bodyemphasis, ref mymissing);
stylebodyemphasis.Font.Size = 12;
stylebodyemphasis.Font.Bold = 1;
System.Object mystyleemphasis = stylebodyemphasis;
// output the data to a text file.
System.IO.StreamWriter mywriter = new System.IO.StreamWriter(@"C:\data.txt", false);
int matchingcount = 0;
System.Double duestotal = 0;
int reportsonpage = 0;
for (int indexmember = 0; indexmember < dvcurrentmember.Count; indexmember++)
{
for (int indexdues = 0; indexdues < dvcurrentdues.Count; indexdues++)
{
if (dvcurrentmember[indexmember]["Local"].ToString() ==
dvcurrentdues[indexdues]["Local"].ToString() &&
dvcurrentmember[indexmember]["Ledger"].ToString() ==
dvcurrentdues[indexdues]["Ledger"].ToString())
{
matchingcount++;
// create and ouput the letter header
Microsoft.Office.Interop.Word.Range rngheader = worddoc.Range(ref mymissing, ref mymissing);
rngheader.Select();
rngheader.Collapse(ref mycollapsetoend);
rngheader.InsertAfter(_pagetitle + "\n" + _pageaddress + "\n\n\n");
rngheader.set_Style(ref mystyleheader);
//rngheader.Collapse(ref mycollapsetoend);
duestotal += System.Convert.ToDouble(dvcurrentdues[indexdues]["ytd2006"]);
// create the name and address string
_namelast = System.Convert.ToString(dvcurrentmember[indexmember]["last name"]);
_namefirst = System.Convert.ToString(dvcurrentmember[indexmember]["first name"]);
//if (System.Convert.ToString(dvcurrentmember[indexmember]["address 1"]) != "null")
//{ _address1 = System.Convert.ToString(dvcurrentmember[indexmember]["address 1"]); }
//else
//{ _haserrors = true; }
//if (System.Convert.ToString(dvcurrentmember[indexmember]["address 2"]) != "null")
//{ _address2 = System.Convert.ToString(dvcurrentmember[indexmember]["address 2"] + "\n"); }
//if (System.Convert.ToString(dvcurrentmember[indexmember]["city"]) != "null")
//{ _city = System.Convert.ToString(dvcurrentmember[indexmember]["city"]); }
//else
//{ _haserrors = true; }
//if (System.Convert.ToString(dvcurrentmember[indexmember]["province"]) != "null")
//{ _province = System.Convert.ToString(dvcurrentmember[indexmember]["province"]); }
//else
//{ _haserrors = true; }
//if (System.Convert.ToString(dvcurrentmember[indexmember]["postal code"]) != "null")
//{ _postalcode = System.Convert.ToString(dvcurrentmember[indexmember]["postal code"]); }
//else
//{ _haserrors = true; }
//_outputstring = _namelast + ", " + _namefirst + "\n" + _address1 + "\n" + _address2 +
// _city + ", " + _province + "\n" + _postalcode + "\n\n\n";
//// add the name address to the document.
//rngcurrent.InsertAfter(_outputstring);
//rngcurrent.Collapse(ref mycollapsetoend);
// create and add the body1 string
Microsoft.Office.Interop.Word.Range rngbody1 = worddoc.Range(ref mymissing, ref mymissing);
rngbody1.Select();
rngbody1.Collapse(ref mycollapsetoend);
rngbody1.InsertAfter(_body1);
rngbody1.Select();
rngbody1.set_Style(ref mystylebody);
//rngbody1.Collapse(ref mycollapsetoend);
// create and add the name
Microsoft.Office.Interop.Word.Range rngname = worddoc.Range(ref mymissing, ref mymissing);
rngname.Select();
rngname.Collapse(ref mycollapsetoend);
rngname.InsertAfter(" " + _namefirst + " " + _namelast + " ");
rngname.Select();
rngname.set_Style(ref mystyleemphasis);
//rngname.Collapse(ref mycollapsetoend);
// create and add the body 2 string
Microsoft.Office.Interop.Word.Range rngbody2 = worddoc.Range(ref mymissing, ref mymissing);
rngbody2.Select();
rngbody2.Collapse(ref mycollapsetoend);
rngbody2.InsertAfter(_body2);
rngbody2.Select();
rngbody2.set_Style(ref mystylebody);
//rngbody2.Collapse(ref mycollapsetoend);
// create and add the year string
Microsoft.Office.Interop.Word.Range rngyear = worddoc.Range(ref mymissing, ref mymissing);
rngyear.Select();
rngyear.Collapse(ref mycollapsetoend);
rngyear.InsertAfter(" 2006 ");
rngyear.Select();
rngyear.set_Style(ref mystyleemphasis);
//rngyear.Collapse(ref mycollapsetoend);
// create and add the body 3 string
Microsoft.Office.Interop.Word.Range rngbody3 = worddoc.Range(ref mymissing, ref mymissing);
rngbody3.Select();
rngbody3.Collapse(ref mycollapsetoend);
rngbody3.InsertAfter(_body3);
rngbody3.Select();
rngbody3.set_Style(ref mystylebody);
//rngbody3.Collapse(ref mycollapsetoend);
// create and add the amount string
// create the value string
if (System.Convert.ToString(dvcurrentdues[indexdues]["ytd2006"].ToString()) != "null")
{
_duespaid = System.Convert.ToString(dvcurrentdues[indexdues]["ytd2006"].ToString());
// format the dues paid string to two decimal places.
if (_duespaid.IndexOf(".") == -1)
{
// there are no decimal values.
_duespaid += ".00";
}
if (_duespaid.IndexOf(".") == _duespaid.Length -2)
{
_duespaid += "0";
}
}
Microsoft.Office.Interop.Word.Range rngamount = worddoc.Range(ref mymissing, ref mymissing);
rngamount.Select();
rngamount.Collapse(ref mycollapsetoend);
rngamount.InsertAfter(" $" + _duespaid + " ");
rngamount.Select();
rngamount.set_Style(ref mystyleemphasis);
//rngamount.Collapse(ref mycollapsetoend);
// create and add the body 4 string
Microsoft.Office.Interop.Word.Range rngbody4 = worddoc.Range(ref mymissing, ref mymissing);
rngbody4.Select();
rngbody4.Collapse(ref mycollapsetoend);
rngbody4.InsertAfter(_body4);
rngbody4.Select();
rngbody4.set_Style(ref mystylebody);
//rngbody4.Collapse(ref mycollapsetoend);
// create the summary string
_summaryshop = "Shop: ";
if (System.Convert.ToString(dvcurrentmember[indexmember]["shop"].ToString()) != "null")
{_summaryshop += System.Convert.ToString(dvcurrentmember[indexmember]["shop"].ToString());}
_summaryshop += "\t";
_summaryclock = "Clock: ";
if (System.Convert.ToString(dvcurrentmember[indexmember]["clock"].ToString()) != "null")
{_summaryclock += System.Convert.ToString(dvcurrentmember[indexmember]["clock"].ToString());}
_summaryclock += "\t";
_summaryledger = "Ledger: ";
if (System.Convert.ToString(dvcurrentmember[indexmember]["ledger"].ToString()) != "null")
{ _summaryledger += System.Convert.ToString(dvcurrentmember[indexmember]["ledger"].ToString()); }
_summaryledger += "\n\n\n";
//_Shawntest = "Shawn's picture test location\n";
//this.Application.Selection.InlineShapes.AddPicture(@"C:\Unite Dues Copy\LoisSignFinal.jpg");
_summarysignatureline = "------------------------------------------------------\n";
_summarysignaturedescription = "On behalf of the Union.\n";
_outputstring = _summaryshop + _summaryclock + _summaryledger +
_summarysignatureline + _summarysignaturedescription;
// add the summary
Microsoft.Office.Interop.Word.Range rngsummary = worddoc.Range(ref mymissing, ref mymissing);
rngsummary.Select();
rngsummary.Collapse(ref mycollapsetoend);
rngsummary.InsertAfter(_outputstring);
rngsummary.Select();
rngsummary.set_Style(ref mystylebody);
//rngsummary.Collapse(ref mycollapsetoend);
reportsonpage += 1;
// insert spacing or a page break on every third report.
Microsoft.Office.Interop.Word.Range rngbreak = worddoc.Range(ref mymissing, ref mymissing);
rngbreak.Select();
rngbreak.Collapse(ref mycollapsetoend);
if (reportsonpage == 3)
{
rngbreak.InsertBreak(ref mypagebreak);
reportsonpage = 0;
}
else
{
rngbreak.InsertAfter("\n\n\n\n\n\n\n\n\n");
}
rngbreak.set_Style(ref mystylebody);
rngbreak.Collapse(ref mycollapsetoend);
rngstart = null;
rngheader = null;
rngbody1 = null;
rngbody2 = null;
rngbody3 = null;
rngname = null;
rngyear = null;
rngamount = null;
rngsummary = null;
rngbreak = null;
if (_haserrors == true)
{
mywriter.WriteLine("ERROR-------------------------------------------------------");
mywriter.WriteLine("application index: " + matchingcount.ToString());
mywriter.WriteLine("local: " + System.Convert.ToInt32(dvcurrentmember[indexmember]["local"].ToString()));
mywriter.WriteLine("ledger: " + System.Convert.ToInt32(dvcurrentmember[indexmember]["ledger"].ToString()));
mywriter.WriteLine("nameFirst: " + System.Convert.ToString(dvcurrentmember[indexmember]["first name"].ToString()));
mywriter.WriteLine("nameLast: " + System.Convert.ToString(dvcurrentmember[indexmember]["last name"].ToString()));
mywriter.WriteLine("address1: " + System.Convert.ToString(dvcurrentmember[indexmember]["address 1"].ToString()));
mywriter.WriteLine("address2: " + System.Convert.ToString(dvcurrentmember[indexmember]["address 2"].ToString()));
mywriter.WriteLine("city: " + System.Convert.ToString(dvcurrentmember[indexmember]["city"].ToString()));
mywriter.WriteLine("province: " + System.Convert.ToString(dvcurrentmember[indexmember]["province"].ToString()));
mywriter.WriteLine("postal code: " + System.Convert.ToString(dvcurrentmember[indexmember]["postal code"].ToString()));
mywriter.WriteLine("shop: " + System.Convert.ToString(dvcurrentmember[indexmember]["shop"].ToString()));
mywriter.WriteLine("clock number: " + System.Convert.ToInt32(dvcurrentmember[indexmember]["clock"].ToString()));
mywriter.WriteLine("dues paid: " + System.Convert.ToString(dvcurrentdues[indexdues]["ytd2006"].ToString()));
mywriter.WriteLine("------------------------------------------------------------");
mywriter.WriteLine();
}
// reset all temporary variables.
_namefirst = string.Empty;
_namelast = string.Empty;
_address1 = string.Empty;
_address2 = string.Empty;
_city = string.Empty;
_province = string.Empty;
_postalcode = string.Empty;
_duespaid = string.Empty;
_outputstring = string.Empty;
_summaryclock = string.Empty;
_summaryledger = string.Empty;
_summaryshop = string.Empty;
_haserrors = false;
}
}
}
mywriter.Close();
System.Diagnostics.Debug.WriteLine("matching count is: " + matchingcount.ToString());
System.Diagnostics.Debug.WriteLine("total dues: " + duestotal.ToString());
// save the word docuement
System.Object savepath = @"C:\data.doc";
worddoc.SaveAs(ref savepath, ref mymissing, ref mymissing, ref mymissing,
ref mymissing, ref mymissing, ref mymissing, ref mymissing,
ref mymissing, ref mymissing, ref mymissing, ref mymissing,
ref mymissing, ref mymissing, ref mymissing, ref mymissing);
System.Diagnostics.Debug.WriteLine("done.");
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine("failed on btnAlternateCreate_Click");
System.Diagnostics.Debug.WriteLine("source: " + ex.Source.ToString());
System.Diagnostics.Debug.WriteLine("message: " + ex.Message.ToString());
throw ex;
}
finally
{
// release objects.
dtdues.Dispose();
dtmembers.Dispose();
comdues.Dispose();
commembers.Dispose();
concurrent.Dispose();
wordapp.Quit(ref myfalse, ref mymissing, ref mymissing);
}
}
Reply
Answers (
1
)
String in UTF-8 format was inserted to DB as ???????????
Missing the point re: Arrays