How to replace an image in Word doc using c#

G

Guest

Hi All,



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

I am converting an html file to word doc and sending this as attachment
using System.Web.Mail; the recipient could not see the image in attachment. I
am using Inlinepicture.Addpicture but still not working



Below is the code



String Writer SW = new String Writer();

String InvoiceUrl
="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.IndexOf
("</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();



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

//object red =readonly;



Word.ApplicationClass objWord = new
Word.ApplicationClass();

Word. Document
objWordDoc=objWord.Documents.Open(ref fileNameObj, ref novalue, ref novalue,
ref novalue, ref novalue,ref novalue,ref novalue,ref novalue,ref novalue,ref
novalue,ref novalue,ref isVisible,ref 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 in
objWordDoc.Fields)

{

if (OField.Type
==Word.WdFieldType.wdFieldIncludePicture)

{

OField.Select();


object bb1 =
OField.InlineShape.Range;

//Word.Range bb
= OField.Result;

string spath =
OField.LinkFormat.SourceFullName;


object
filename123 = OField.LinkFormat.SourceFullName;

//object bb1 =
bb.Application.Selection.Range;

OField.Delete();



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









}

}












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

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









objWord.Quit(ref novalue, ref novalue,
ref novalue);



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


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

GC.Collect();





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


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

}





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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top