none
Paste a datagridview cell contents to the selection point in Outlook mailitem RRS feed

  • Question

  • I'm trying to paste a string into an outlook mailitem at the insertion point of my cursor. The string is coming from a datagridview. I'm using C# from vs2015 and outlook O365. This is what I'm trying:

    private void button5_Click(object sender, EventArgs e)
            {
                {
                    // Paste To Outlook      
                    object oItem;       
                    Outlook.Application oApp = new Outlook.Application();
                    //Outlook.Application oInsp = Inspector.CurrentItem();
                    Outlook.Explorer oExp = oApp.ActiveExplorer();
                    Outlook.Selection oSel = oExp.Selection;
                    // the current row
                    int rwindex = dataGridView1.CurrentCell.RowIndex;
                    oWordApp.Selection.TypeText(dataGridView1.Rows[rwindex].Cells[0].Value.ToString());
                    oWordApp.Selection.TypeText(dataGridView1.Rows[rwindex].Cells[1].Value.ToString());
                    // the current column
                    int colindex = dataGridView1.CurrentCell.ColumnIndex;
                    for (int ARows = 0; ARows <= dataGridView1.RowCount; ARows++)
                    {
                        oWordApp.Selection.TypeText(dataGridView1.Rows[ARows].Cells[colindex].Value.ToString());
                    }
                }
            }

    Of course this is not right, but I can't find information to help me figure it out. I modified the above code from where I'm pasting into a Word document from the datagridview, that part does work.


    Thursday, November 17, 2016 9:13 PM

Answers

  • I got this to work.

     // Paste To Outlook 
                    int i;
                    object oItem;
                    string holdme;
                    Outlook.Application oApp = new Outlook.Application();
                    Outlook.Explorer oExp = oApp.ActiveExplorer();
                    Outlook.Selection oSel = oExp.Selection;
                    for (i = 1; i <= oSel.Count; i++)
                    {
                        oItem = oSel[i];
                        Outlook.MailItem oMail = (Outlook.MailItem)oItem;
                        Outlook.Inspector inspector = oMail.GetInspector;
    
                        // Obtain the Word.Document object from the Inspector object
                        Microsoft.Office.Interop.Word.Document document =
                            (Microsoft.Office.Interop.Word.Document)inspector.WordEditor;
    
                        holdme = dataGridView1.CurrentCell.Value.ToString();
                        document.Application.Selection.Text = holdme;
                    }

    • Marked as answer by jrdnoland Sunday, November 20, 2016 1:33 PM
    Sunday, November 20, 2016 1:33 PM

All replies

  • Hi jrdnoland,

    Thank you for posting here.

    Since your problem is more related to outlook, I will move it to Outlook for Developers Forum for suitable support.

    The Visual C# Forum discuss and ask question about the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar and code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 18, 2016 2:26 AM
  • I got this to work.

     // Paste To Outlook 
                    int i;
                    object oItem;
                    string holdme;
                    Outlook.Application oApp = new Outlook.Application();
                    Outlook.Explorer oExp = oApp.ActiveExplorer();
                    Outlook.Selection oSel = oExp.Selection;
                    for (i = 1; i <= oSel.Count; i++)
                    {
                        oItem = oSel[i];
                        Outlook.MailItem oMail = (Outlook.MailItem)oItem;
                        Outlook.Inspector inspector = oMail.GetInspector;
    
                        // Obtain the Word.Document object from the Inspector object
                        Microsoft.Office.Interop.Word.Document document =
                            (Microsoft.Office.Interop.Word.Document)inspector.WordEditor;
    
                        holdme = dataGridView1.CurrentCell.Value.ToString();
                        document.Application.Selection.Text = holdme;
                    }

    • Marked as answer by jrdnoland Sunday, November 20, 2016 1:33 PM
    Sunday, November 20, 2016 1:33 PM