?
Solved

Windows Forms Word Document SaveAs Email Draft

Posted on 2006-04-10
10
Medium Priority
?
470 Views
Last Modified: 2012-05-05
We are using VS.net 2003 developing an intrnal office system, my application is successfully creating Word Documents and printing them, what i need to do is save the word document as an email draft not as an attachment but as the body of the email;

Very Urgent

Thanks in advance
0
Comment
Question by:Robinsonx6
  • 5
  • 5
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16416920
Are you using Outlook?

Bob
0
 
LVL 2

Author Comment

by:Robinsonx6
ID: 16416974
yes is the answer to outlook

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16417145
Try this:

//Add a reference to Micro$oft Outlook type library from the COM tab.
using System;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;

public enum MailFormat
{
  Unknown = OlBodyFormat.olFormatUnspecified,
  Html = OlBodyFormat.olFormatHTML,
  RichText = OlBodyFormat.olFormatRichText,
  Plain = OlBodyFormat.olFormatPlain
}

public class OutlookMailItem
{

  private Application _app = null;
  private NameSpace _namespace = null;
  private MAPIFolder _inbox = null;
  private MailItem _item = null;

  public OutlookMailItem()
  {
    _app = new Application();

    _namespace = _app.GetNamespace("MAPI");

    _inbox = _namespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

  }

  ~OutlookMailItem()
  {
    _app.Quit();

    Marshal.ReleaseComObject(_app);
    Marshal.ReleaseComObject(_namespace);
    Marshal.ReleaseComObject(_inbox);
    Marshal.ReleaseComObject(_item);
  }

  public void SelectMailItem(int index)
  {
    _item = (MailItem)_inbox.Items[index];
  }

  public string Body
  {
    get { return _item.Body; }
    set { _item.Body = value; }
  }

  public string ToAddress
  {
    get { return _item.To; }
    set { _item.To = value; }
  }

  public string Subject
  {
    get { return _item.Subject; }
    set { _item.Subject = value; }
  }

  public MailFormat BodyFormat
  {
    get { return (MailFormat)_item.BodyFormat; }
    set { _item.BodyFormat = (OlBodyFormat)value; }
  }

  public void Send()
  {
    _item.Send();
  }

  public void SaveDraft()
  {
    _item.Save();
  }

}

Example:
  OutlookMailItem item = new OutlookMailItem();
  item.Body = "This is a test of the radio broadcast system";
  item.BodyFormat = MailFormat.Html;
  item.SaveDraft();

Bob
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 2

Author Comment

by:Robinsonx6
ID: 16417355
currently i have created an office document using c# and have used the following to print the document


Microsoft.Office.Interop.Word.Document aDoc;

                  aDoc.PrintOut(ref vk_missing, ref vk_missing,
                        ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
                        ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
                        ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
                        ref vk_missing, ref vk_missing, ref vk_missing,      ref vk_missing);

how do i get the content of the worddocument including images contained into the item.Body = ????????
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16417399
That is a question that I don't have an ready-made answer for.  The Word model isn't very straight-forward when it comes to an ordered processing of items, that I know of.

Bob
0
 
LVL 2

Author Comment

by:Robinsonx6
ID: 16417453
are you able to have a think about it when you get a chance please :-)

Thanks Chris
0
 
LVL 2

Author Comment

by:Robinsonx6
ID: 16417874
if i try setting the item.ToAddress = "xxxxxx@xxxx.com"
it falls over on the

public string ToAddress
  {
    get { return _item.To; }
    set { _item.To = value; }  // fall over hear
  }

it is saying that object doesnt exist its not the value as it has a value so surely it must be the _item.To which doesnt exist???

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16417961
I didn't think that you were going to use those, so I added some checks.  You need to call SelectMailItem[index] before doing anything else.

//Add a reference to Micro$oft Outlook type library from the COM tab.
using System;
using System.IO;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;
using System.Reflection;

public enum MailFormat
{
  Unknown = OlBodyFormat.olFormatUnspecified,
  Html = OlBodyFormat.olFormatHTML,
  RichText = OlBodyFormat.olFormatRichText,
  Plain = OlBodyFormat.olFormatPlain
}

public class OutlookMailItem
{

  private Application _app = null;
  private NameSpace _namespace = null;
  private MAPIFolder _inbox = null;
  private MailItem _item = null;

  public OutlookMailItem()
  {
    _app = new Application();

    _namespace = _app.GetNamespace("MAPI");

    _inbox = _namespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
  } //constructor

  ~OutlookMailItem()
  {
    _app.Quit();

    if (_app != null)
      Marshal.ReleaseComObject(_app);

    if (_namespace != null)
      Marshal.ReleaseComObject(_namespace);

    if (_inbox != null)
      Marshal.ReleaseComObject(_inbox);

    if (_item != null)
      Marshal.ReleaseComObject(_item);
  } //destructor

  public void SelectMailItem(int index)
  {
    _item = (MailItem)_inbox.Items[index];
  } //SelectMailItem

  public string Body
  {
    get
    {
      if (_item == null)
        throw new ArgumentNullException("Select mail item", "Call SelectMailItem[index] to select a mail item.");

      return _item.Body;
    }
    set
    {
      if (_item == null)
        throw new ArgumentNullException("Select mail item", "Call SelectMailItem[index] to select a mail item.");

      _item.Body = value;
    }
  } //Body

  public string ToAddress
  {
    get
    {
      if (_item == null)
        throw new ArgumentNullException("Select mail item", "Call SelectMailItem[index] to select a mail item.");

      return _item.To;
    }
    set
    {
      if (_item == null)
        throw new ArgumentNullException("Select mail item", "Call SelectMailItem[index] to select a mail item.");

      _item.To = value;
    }
  } //ToAddress

  public string Subject
  {
    get
    {
      if (_item == null)
        throw new ArgumentNullException("Select mail item", "Call SelectMailItem[index] to select a mail item.");

      return _item.Subject;
    }
    set
    {
      if (_item == null)
        throw new ArgumentNullException("Select mail item", "Call SelectMailItem[index] to select a mail item.");

      _item.Subject = value;
    }
  } //Subject

  public MailFormat BodyFormat
  {
    get
    {
      if (_item == null)
        throw new ArgumentNullException("Select mail item", "Call SelectMailItem[index] to select a mail item.");

      return (MailFormat)_item.BodyFormat;
    }
    set
    {
      if (_item == null)
        throw new ArgumentNullException("Select mail item", "Call SelectMailItem[index] to select a mail item.");

      _item.BodyFormat = (OlBodyFormat)value;
    }
  } //BodyFormat

  public void Send()
  {
    if (_item == null)
      throw new ArgumentNullException("Select mail item", "Call SelectMailItem[index] to select a mail item.");

    _item.Send();
  } //Send

  public void SaveDraft()
  {
    if (_item == null)
      throw new ArgumentNullException("Select mail item", "Call SelectMailItem[index] to select a mail item.");

    _item.Save();
  } //SaveDraft

  public void AddAttachments(string attachments)
  {

    if (_item == null)
      throw new ArgumentNullException("Select mail item", "Call SelectMailItem[index] to select a mail item.");

    string[] list = attachments.Split(";".ToCharArray());

    foreach (string file in list)
    {

      if (File.Exists(file))
      {
        _item.Attachments.Add(file, Missing.Value, Missing.Value, Missing.Value);
      }
      else
      {
        // Avoid any errors here, just quietly skip.
      } //if
 
    } //for

  } //AddAttachments

} //class

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16439350
The Word model doesn't give you straight forward access to the images and paragraphs, as far as I can tell.  

It may prove to be useful to ask that question in the Word topic area:
  http://www.experts-exchange.com/Applications/MS_Office/Word/

Bob
0
 
LVL 2

Author Comment

by:Robinsonx6
ID: 16442763
ok chears Bob i will post a question in there
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question