By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,132 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,132 IT Pros & Developers. It's quick & easy.

How to replace an image in Word doc using c#

P: n/a
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.wdAllowOn lyReading , 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.ReleaseComO bject (objWordDoc);
System.Runtime.InteropServices.Marshal.ReleaseComO bject (objWord);

GC.Collect();

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

mail.To = txtTo.Text.ToString().Trim();

mail.From = "yo*@yourcompany.com";

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

Nov 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.