Windows Forms Word Document SaveAs Email Draft

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
LVL 2
Robinsonx6Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Are you using Outlook?

Bob
0
Robinsonx6Author Commented:
yes is the answer to outlook

0
Bob LearnedCommented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Robinsonx6Author Commented:
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
Bob LearnedCommented:
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
Robinsonx6Author Commented:
are you able to have a think about it when you get a chance please :-)

Thanks Chris
0
Robinsonx6Author Commented:
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
Bob LearnedCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
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
Robinsonx6Author Commented:
ok chears Bob i will post a question in there
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.