How to embed an image in word using c#

B

balakrishna

Hi All,

Can anyone tell me how to embed an image in MS Word 2003 using c#.net.


I am converting an html file to word doc and sending this as a
attachment using System.Web.Mail; the recipient could not see the imag
in attachment. I am using Inlinepicture.Addpicture but still no
working. I have also tried Field.Unlink to save the image as inlin
image but still not working


*Code*


private void btnSendMail_Click(object sender, System.EventArgs e)
{

StringWriter SW = new StringWriter();
//Dim filename As String = Request.CurrentExecutionFilePath
string InvoiceUrl ="test1.aspx";
Server.Execute(InvoiceUrl, SW);

String sInvoiceHTML;
sInvoiceHTML = SW.ToString();

//Remove the Invocie history from the String generated
//assuming the Invoice history part is contatained in the litera
with css class = hideonprint
//Removing till /body

int iStartpos = sInvoiceHTML.IndexOf("<spa
class='hideOnPrint'>");
int iEndPos = sInvoiceHTML.IndexOf ("</body>",iStartpos);
sInvoiceHTML = sInvoiceHTML.Remove(iStartpo
,(iEndPos-8)-iStartpos);

/*String Path;
Path = Request.PhysicalPath;
int pos = Path.LastIndexOf("\\");
Path = Path.Remove(pos,Path.Length-pos);
Path = Path + "\\mslogo.gif";*/

//For MS logo
string imgPath = Server.MapPath(""
@"\Core\Report\Custom\mslogo.gif");
string pp = Server.MapPath(@"\Core\Report\Custom\");
sInvoiceHTML = sInvoiceHTML.Replace("<img src='mslogo.gif'","<im
src='" + imgPath.ToUpper() + "'");
string fileName = Server.MapPath("" + "SampleInvoiceMail2"
".doc");

if (File.Exists(fileName))
File.Delete(fileName);

StreamWriter sWriter = File.CreateText(fileName);
sWriter.WriteLine(sInvoiceHTML);
sWriter.Close();
SW.Close();

object fileNameObj=(Object)fileName;
object novalue=System.Reflection.Missing.Value;
object isVisible = false; //false;
object readonly1 = false; //test
object format=8;
object kk = true;
object Noreset = true;
object UserIRM = true;
object Password ="bala";


Word.ApplicationClass objWord = new Word.ApplicationClass();
Word.Document objWordDoc=objWord.Documents.Open(ref fileNameObj
ref novalue, ref novalue, ref novalue, ref novalue,ref novalue,re
novalue,ref novalue,ref novalue,ref novalue,ref novalue,re
isVisible,ref novalue,ref novalue,ref novalue,ref novalue);
objWord.Visible = false;

try
{
object linkfile = (object) false;
object save1 = (object)true;
object kkkkk = System.Reflection.Missing.Value;


foreach (Word.Field OField in objWordDoc.Fields)
{
if (OField.Type ==Word.WdFieldType.wdFieldIncludePicture)
{

OField.Select();
OField.Application.Selection.Fields.Unlink();
OField.Update();
objWordDoc.Save();


///OField.LinkFormat.SavePictureWithDocument = true;


}
}





objWordDoc.Protect(Word.WdProtectionType.wdAllowOnlyReading , re
Noreset ,ref Password ,ref novalue,ref novalue);

objWordDoc.SaveAs(ref fileNameObj, ref novalue, ref novalue, re
novalue,ref novalue, ref novalue, ref novalue, ref novalue, re
novalue,ref novalue, ref novalue,ref novalue,ref novalue,re
novalue,ref novalue,ref novalue);


objWord.Quit(ref novalue, ref novalue, ref novalue);
System.Runtime.InteropServices.Marshal.ReleaseComObjec
(objWordDoc);
System.Runtime.InteropServices.Marshal.ReleaseComObject (objWord);
GC.Collect();


MailAttachment oAttch = ne
MailAttachment(fileName,MailEncoding.Base64);
MailMessage mail = new MailMessage();
mail.To = txtTo.Text.ToString().Trim();
mail.From = "(e-mail address removed)";

mail.Subject = txtSubject.Text;
mail.BodyFormat = MailFormat.Html;

mail.Attachments.Add(oAttch);
SmtpMail.SmtpServer = "Localhost";
SmtpMail.Send(mail);
}
catch (Exception err)
{
Response.Write(err.Message);
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject
(objWordDoc);
System.Runtime.InteropServices.Marshal.ReleaseComObject (objWord);
GC.Collect();

}

}
 

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