//copio el docuemnto original a uno temporal y lo abro
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
object saveChanges = Word.WdSaveOptions.wdSaveChanges;
System.IO.File.Copy("C:\documentos\prueba.docx", "c:\documentos\temp.doc", true);
Word
.Application wordApp
= new Word
.Application();Word.Document aDoc = null;
object filename = "c:\documentos\temp.doc";
//edito el documento
if (System.IO.File.Exists((string)filename))
{
object readOnly = false;
object isVisible = false;
aDoc = wordApp.Documents.Open(ref filename, ref readOnly, ref isVisible);
aDoc.Activate();
this.FindAndReplace(wordApp, "<fecha>", dateTimePicker1.Text);
this.FindAndReplace(wordApp, "<nombre>", TB_Nombre_Cotizacion_Nueva.Text.Trim());
this.FindAndReplace(wordApp, "<tipo_evento>", CB_tipo_evento_Cotizacion_Nueva.Text.Trim());
this.FindAndReplace(wordApp, "<precio>", TB_Precio_Cotizacion_Nueva.Text.Trim());
aDoc.Save();
//cierro el documento
((Word._Document)aDoc).Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(aDoc);
}
else
{
MessageBox.Show("El archivo no existe.", "No Existe", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//copio el temporal a uno final que sera el que se envia
System.IO.File.Copy("C:\documentos\temp.doc", "c:\documentos\cotizacion " + TB_Nombre_Cotizacion_Nueva.Text + ".doc", true);
//envio el documento
Enviar(TB_Enviar_Cotizacion_Nueva.Text);
//esta es metodo para enviar
public static bool Enviar(string a)
{
string PathFile = @"c:\documentos\"+TB_Nombre_Cotizacion_Nueva.Text +".doc";
System.Net.Mail.MailMessage msg
= new System.Net.Mail.MailMessage();
msg.To.Add(a);
msg
.From = new MailAddress
("luisbazanmontana@gmail.com",
"",
System.Text.Encoding.UTF8);msg.SubjectEncoding = Encoding.UTF8;
msg.Subject = "Cotizacion";
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = "Mensaje del correo";
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false;
Attachment Data
= new Attachment
(PathFile
);
msg.Attachments.Add(Data);
SmtpClient client
= new SmtpClient
();
client.UseDefaultCredentials = false;
client
.Credentials = new System.Net.NetworkCredential("micorreo@gmail.com",
"mipass");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try
{
client.Send(msg);
MessageBox.Show("Se ha enviado exitosamente ");
return true;
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show("No se mando"+ex, "No Existe", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
}