472,110 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

How to replace an image in Word doc using c#

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
0 5524

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by - ions | last post: by
3 posts views Thread by Andy | last post: by
1 post views Thread by James Vitale | last post: by
7 posts views Thread by lgbjr | last post: by
5 posts views Thread by ivowel | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.