none
Accessing VBscript in outlook form via C# RRS feed

  • Question

  • Hope anyone can help!

    I have an old outlook form which contains a VBscript that does different tasks! before we had a VB.dll button that could envoke different parts of the VBscripts. since i dont have the code anymore i can only guess that it could look like this

    pseudokode: 

     

    IPM.Note.test(method name);

     

    I have now writin a new button in C# which works as intended, but how do i pass parameters to the VBscript via the C# button. I have been looking for solution but can only find VB solutions.

    private void nyTest(Office.CommandBarButton ctrl, ref bool cancel)
        {
          Outlook.MAPIFolder pubFolder = (Outlook.MAPIFolder)this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders);
    
          Outlook.MAPIFolder test = pubFolder.Folders["test1"];
          Outlook.MAPIFolder test2 = test.Folders["test3"];
    
          try
          {
    
            Outlook.MailItem miItem = (Outlook.MailItem)test2.Items.Add("IPM.Note.heman");
            miItem.Display(true);
          }
          catch (System.Exception ex)
          {
            System.Windows.Forms.MessageBox.Show("Error " + ex.Message.ToString());
          }
        }

     

    Can anyone help :-) thx in advance

    Thursday, May 12, 2011 10:15 AM

Answers

All replies

  • Hi,

    see if that may help:

    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/eb7744fb-d427-4b16-bfbc-489ee75c16e1

    However I'm not sure how to call a VBCode in a Form.

    I also would suggest to remove the code of the Form and put it in the AddIn.

    All you can do in the script, you can do in the AddIn.

    This also makes the deployment easier, cause when scriptblocking is enabled, scripts may prompt security dialogs and get removed when deploying updated forms as I know.

    Greets - Helmut

     


    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
    Thursday, May 12, 2011 12:23 PM
    Answerer
  • hey thx for link, the problem is that the VB code is big so to re-write would take to much time :-) ill try reading up on your link an maybe get back with an answer. If anyone else has input, i would appreciated it .
    Thursday, May 12, 2011 1:31 PM
  • I don't know of any ways to call Outlook form code from outside the form.
     
    Usually you end up doing the reverse, calling into addin code from form code and if needed getting back data from the addin. I just did that in an addin I'm working on when the customer wanted a complex user form launched from a button in the form design surface. I have the form code call into the addin code, which displays the form and gathers data from it and then returns the data to the form code.
     
    Or of course you can do what Helmut suggested and just use pure addin code with no form code at all :)
     
    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "allanmoller" <=?utf-8?B?YWxsYW5tb2xsZXI=?=> wrote in message news:e001d540-a5d0-4da4-bfdb-0bb4e6f66c5c...
    hey thx for link, the problem is that the VB code is big so to re-write would take to much time :-) ill try reading up on your link an maybe get back with an answer. If anyone else has input, i would appreciated it ..

    Ken Slovak MVP - Outlook
    Thursday, May 12, 2011 1:44 PM
  • hmmm i hope it is possible or else i have to write my buttons in VB and compile as a dll, which was the case before! It seems strange that i cant call a "VBscript sub"  with a C# button, when i can call the form itself, but what do i know i am new to vsto. 

    The form itself has 2 functions, one for customer phone call and one for customer email. When it is a email it uses a the VBscript to pull email data into form 

    sub cmdOpenOpMail_click()
    	pull data!
    end sub
    

     

    since iam not good in VB and dont know how to do it in that code, i hoped i could do it in C#.

     

    Thursday, May 12, 2011 2:04 PM
  • This has nothing to do with VSTO or C#, it's the same with any code external to the form, it cannot call into the form. It would be the same in a VB.NET addin or a VB6 addin or Outlook VBA code or whatever.
     
    Your form code can call your C# addin to perform the actions of pulling data, and if what it calls into is a function it can return data to the form code. Take a look at my (outdated) templates showing how to call into addin code from the outside. The form code can do that, calling into the addin. The templates are at http://www.slovaktech.com/outlook_2007_templates.htm. The VSTO templates show how to set up a VSTO addin to be called from the outside world, which can include from a form.
     
    A simpler approach is to code the entire thing in the addin and add the button for that into the ribbon or toolbars using your addin code. That's how I'd do it unless the button absolutely has to be in the form's design surface.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "allanmoller" <=?utf-8?B?YWxsYW5tb2xsZXI=?=> wrote in message news:e390b86f-c4be-4f73-abf4-387ae8b9e9d0...

    hmmm i hope it is possible or else i have to write my buttons in VB and compile as a dll, which was the case before! It seems strange that i cant call a "VBscript sub"  with a C# button, when i can call the form itself, but what do i know i am new to vsto. 

    The form itself has 2 functions, one for customer phone call and one for customer email. When it is a email it uses a the VBscript to pull email data into form 

    sub cmdOpenOpMail_click()
    	pull data!
    end sub
    

     

    since iam not good in VB and dont know how to do it in that code, i hoped i could do it in C#.

     


    Ken Slovak MVP - Outlook
    Thursday, May 12, 2011 2:18 PM
  • Hmm i was thinking is it possible to set a form field via C#, then i could easily make the VBscript do something if field is one thing and another thing if it is something else?

     

     

    Thursday, May 12, 2011 2:32 PM
  • If a form field is bound to an Outlook property, built-in or user property, you can easily change the field value by changing that property value. Code in the form for the PropertyChange() or UserPropertyChange() events can then handle that field change.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "allanmoller" <=?utf-8?B?YWxsYW5tb2xsZXI=?=> wrote in message news:b4177c89-bdd0-414a-8315-40dc6846f1ca...

    Hmm i was thinking is it possible to set a form field via C#, then i could easily make the VBscript do something if field is one thing and another thing if it is something else?

     

     


    Ken Slovak MVP - Outlook
    Thursday, May 12, 2011 2:35 PM
  • So if i had a Form field called "Type" how would i set that using C#?

    pseudo:

    IPM.Note.Test(type=E-mail);

    Thursday, May 12, 2011 2:46 PM
  • First you'd need to get a handle to the item as a mailitem object. How you do that depends on whether the item is opened (Inspector.CurrentItem), is selected in an Explorer or is retrieved using an EntryID.
     
    If your Type property is bound to a UserProperty named Type you would acess the property using the following, assuming oMail is your MailItem reference to your custom form:
       
        Outlook.UserProperties props = oMail.UserProperties;
        Outlook.UserProperty prop = props.Find("Type", true);
        prop.Value = "E-mail");
     
    That has no error handling of course to check that the property actually exists on the item, or to handle any other possible exceptions.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "allanmoller" <=?utf-8?B?YWxsYW5tb2xsZXI=?=> wrote in message news:69524149-9f35-47a1-8e0e-872bef97e9ff...

    So if i had a Form field called "Type" how would i set that using C#?

    pseudo:

    IPM.Note.Test(type=E-mail);


    Ken Slovak MVP - Outlook
    Thursday, May 12, 2011 2:53 PM
  • would this work?

     

     

    Property = miItem.UserProperties.Find ("type", missing);
    	if (Property != null)
    	{
    		Property.Value = "E-mail";
    	}

     

    Thursday, May 12, 2011 2:56 PM
  • Well, yes, but...
     
    You really shouldn't use compound dot operators as you're showing. You should use what I did and get individual object references instead of using an implicit UserProperty object. That way you can release that object as needed and you don't create internal variable objects you can't control.
     
    I'd also use the second argument and if the property wasn't there I'd add it.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "allanmoller" <=?utf-8?B?YWxsYW5tb2xsZXI=?=> wrote in message news:ca36233a-b06b-47ea-b7d3-25c3adfdc566...

    would this work?

     

     

    Property = miItem.UserProperties.Find ("type", missing);
    	if (Property != null)
    	{
    		Property.Value = "E-mail";
    	}

     


    Ken Slovak MVP - Outlook
    Thursday, May 12, 2011 3:09 PM
  • Thx this works, but via debugging the VBscript i found out that it also needs value se below:

    if itm.UserProperties.Find("originalMail").value<>""
    

    So i also need to set this value, but could u have any idea which value it needs (maybe mail id number) i managed to get this number, from old and old item:

    000000001A447390AA6611CD9BC800AA002FC45A0900055E8E2A816DB0449A8C66329A3C4A5401670E81C1A50000055E8E2A816DB0449A8C66329A3C4A5401670E8265E40000

    So can anyone maybe guess what that number represents, it looks like som mail id (but thats my guess) and if so how do i get this from an email???? 

    Thursday, May 12, 2011 7:53 PM
  • Thursday, May 12, 2011 8:00 PM
  • That would be my guess, but it's totally a guess.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "allanmoller" <=?utf-8?B?YWxsYW5tb2xsZXI=?=> wrote in message news:21a346c0-656e-42d5-a699-acbe9507bea9...

    could it be the EntryID ???

     

    http://www.codeproject.com/tips/54556/Get-Outlook-07-email-attachments-entry-ID-using-Cs.aspx


    Ken Slovak MVP - Outlook
    Thursday, May 12, 2011 8:07 PM
  • It was EntryId thx for help.
    Tuesday, May 17, 2011 10:52 AM