How to replace an image in word using c#

B

balakrishna

Hi All,

Can anyone tell me how to replace an image in MS Word 2003 usin
c#.net.
I am converting an html file to word doc and sending this as attachmen
using System.Web.Mail; the recipient could not see the image i
attachment. I am using Inlinepicture.Addpicture but still not working

Below is the code

String Writer SW = new Strin
Writer();
String InvoiceUr
="MCS_uiInvoiceCDN.aspx?TEFormat=US.STD&FPFormat=&rId={EF9F205D-B746-437D-9236-2370E632FEBB}&sno={0CBA44DE-2528-421A-BBA3-98910D8BE837}&ui=W&sInvoiceId={1B1FE817-5D17-4302-94E6-3871F08C56A5}&iNum=1&sFormat=/Core/Report/Custom/MCS_uiInvoiceUSDefault.aspx";
Server. Execute(InvoiceUrl, SW);


String sInvoiceHTML;
sInvoiceHTML = SW.ToString();


int startups
sInvoiceHTML.IndexOf("<span class='hideOnPrint'>");
int indoors = sInvoiceHTML.IndexO
("</body>",iStartpos);
sInvoiceHTML
sInvoiceHTML.Remove(iStartpos ,(iEndPos-8)-iStartpos);



//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'","<img src='"
imgPath.ToUpper() + "'");
string fileName = Server.MapPath("
+ "SampleInvoiceMail1" + ".doc");

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

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

SW.Close();

objec
fileNameObj=(Object)fileName;
objec
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";
//object red =readonly;

Word.ApplicationClass objWord = ne
Word.ApplicationClass();
Word. Documen
objWordDoc=objWord.Documents.Open(ref fileNameObj, ref novalue, re
novalue, ref novalue, ref novalue,ref novalue,ref novalue,re
novalue,ref novalue,ref novalue,ref novalue,ref isVisible,re
novalue,ref novalue,ref novalue,ref novalue);
objWord.Visible = true;



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



foreach (Word. Field OField i
objWordDoc.Fields)
{
if (OField.Typ
==Word.WdFieldType.wdFieldIncludePicture)
{

OField.Select();
object bb
= OField.InlineShape.Range;

//Word.Range bb = OField.Result;
strin
spath = OField.LinkFormat.SourceFullName;

objec
filename123 = OField.LinkFormat.SourceFullName;
//objec
bb1 = bb.Application.Selection.Range;

OField.Delete();

objWordDoc.InlineShapes.AddPicture(spath,ref linkfile,ref save1,re
bb1);





}
}






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, re
novalue, ref novalue);


System.Runtime.InteropServices.Marshal.ReleaseComObject (objWordDoc);

System.Runtime.InteropServices.Marshal.ReleaseComObject (objWord);
GC.Collect();


MailAttachment oAttch = ne
MailAttachment(fileName,MailEncoding.UUEncode);

MailMessage mail = ne
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);
}


Any pointers in this regard will be highly appreciated.
Thanks,
Bala
 

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