none
VS2010: Opening templates from within a solution RRS feed

  • Question

  • Hi all,

    I have the below button code which works awesome to open a Outlook template from a path on my C:.

    I would like to import these .oft files within the solution and have the path open them from there for deployment. Does anyone know what the path for that would be?

    I have this....

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
            Process.Start("C:\Users\tom\AppData\Roaming\Microsoft\Templates\TicketRequests\Ticket Please.oft")
        End Sub

    Is it easier to do what I am asking or is there another way to deploy oft files?

    Any help would be appreciated,

    Thanks,

    Tom

    Wednesday, March 27, 2013 6:25 PM

Answers

  • The file path of where the code was installed could be different than the path where the code is running from, depending on volume shadow copy of the Windows runtime version. I usually use code something like this for VB.NET:
     
    DIM oUri As UriBuilder = New UriBuilder(Assembly.GetExecutingAssembly().CodeBase)

    DIM sPath As String = Uri.UnescapeDataString(oUri.Path + oUri.Fragment)

    Dim sFolderName As String = Path.GetDirectoryName(sPath)

    Using Process.Start is really not at all the best way to open an OFT file in an Outlook COM addin. You should use the trusted Outlook.Application object passed to you in the VSTO Startup() handler code in ThisAddIn. The best way to open an OFT would be this, assuming the OFT is for a MailItem:
     

    Public Class ThisAddIn

        Friend _olApp As Outlook.Application

    Private Sub ThisAddIn_Startup() Handles Me.Startup

        _olApp = Me.Application

        ' whatever else

    End Sub

    Then in your button handler code to open the template, assuming it's a mail item code something like this:
     

        DIM oUri As UriBuilder = New UriBuilder(Assembly.GetExecutingAssembly().CodeBase)

        DIM sPath As String = Uri.UnescapeDataString(oUri.Path + oUri.Fragment)

        Dim sFolderName As String = Path.GetDirectoryName(sPath)

        Dim oMail As Outlook.MailItem = Globals.ThisAddIn._olApp.CreateItemFromTemplate(sFolderName & "Ticket Please.oft")
     
    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Phlegnh" <=?utf-8?B?UGhsZWduaA==?=> wrote in message news:ecc4980f-26be-4f15-b8eb-cb1f1d0b9ace...

    Hi all,

    I have the below button code which works awesome to open a Outlook template from a path on my C:.

    I would like to import these .oft files within the solution and have the path open them from there for deployment. Does anyone know what the path for that would be?

    I have this....

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
            Process.Start("C:\Users\tom\AppData\Roaming\Microsoft\Templates\TicketRequests\Ticket Please.oft")
        End Sub

    Is it easier to do what I am asking or is there another way to deploy oft files?

    Any help would be appreciated,

    Thanks,

    Tom


    Ken Slovak MVP - Outlook
    Wednesday, March 27, 2013 8:10 PM
  • In looking at that code I'd change the last line to this:
     
     
        Dim oMail As Outlook.MailItem = Globals.ThisAddIn._olApp.CreateItemFromTemplate(Path.Combine(sFolderName, "Ticket Please.oft"))
     
    The initial part of the code is getting the file path to the location where your code is executing. It compensates for any code moving by Windows due to any possible Volume Shadow Copy going on, and by using the UriBuilder it eliminates any "File://" start to the Uri.
     
    When in doubt about what some code is doing the best practice would be to run in debug mode, step the code and examine the results and any locals at each step of the way. That way you learn what's going on and aren't just copying code that makes no sense to you.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Phlegnh" <=?utf-8?B?UGhsZWduaA==?=> wrote in message news:83751804-dc7a-47c6-bd1e-3629ecff3701...

    Thanks Ken,

    I will try that. So where is that actually looking for the template oft's? I could not figure out the pathing and I might be missing something.

    Also, these are mail items.

    Thanks again!

    Tom


    Ken Slovak MVP - Outlook
    Thursday, March 28, 2013 2:49 PM

All replies

  • The file path of where the code was installed could be different than the path where the code is running from, depending on volume shadow copy of the Windows runtime version. I usually use code something like this for VB.NET:
     
    DIM oUri As UriBuilder = New UriBuilder(Assembly.GetExecutingAssembly().CodeBase)

    DIM sPath As String = Uri.UnescapeDataString(oUri.Path + oUri.Fragment)

    Dim sFolderName As String = Path.GetDirectoryName(sPath)

    Using Process.Start is really not at all the best way to open an OFT file in an Outlook COM addin. You should use the trusted Outlook.Application object passed to you in the VSTO Startup() handler code in ThisAddIn. The best way to open an OFT would be this, assuming the OFT is for a MailItem:
     

    Public Class ThisAddIn

        Friend _olApp As Outlook.Application

    Private Sub ThisAddIn_Startup() Handles Me.Startup

        _olApp = Me.Application

        ' whatever else

    End Sub

    Then in your button handler code to open the template, assuming it's a mail item code something like this:
     

        DIM oUri As UriBuilder = New UriBuilder(Assembly.GetExecutingAssembly().CodeBase)

        DIM sPath As String = Uri.UnescapeDataString(oUri.Path + oUri.Fragment)

        Dim sFolderName As String = Path.GetDirectoryName(sPath)

        Dim oMail As Outlook.MailItem = Globals.ThisAddIn._olApp.CreateItemFromTemplate(sFolderName & "Ticket Please.oft")
     
    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Phlegnh" <=?utf-8?B?UGhsZWduaA==?=> wrote in message news:ecc4980f-26be-4f15-b8eb-cb1f1d0b9ace...

    Hi all,

    I have the below button code which works awesome to open a Outlook template from a path on my C:.

    I would like to import these .oft files within the solution and have the path open them from there for deployment. Does anyone know what the path for that would be?

    I have this....

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
            Process.Start("C:\Users\tom\AppData\Roaming\Microsoft\Templates\TicketRequests\Ticket Please.oft")
        End Sub

    Is it easier to do what I am asking or is there another way to deploy oft files?

    Any help would be appreciated,

    Thanks,

    Tom


    Ken Slovak MVP - Outlook
    Wednesday, March 27, 2013 8:10 PM
  • Thanks Ken,

    I will try that. So where is that actually looking for the template oft's? I could not figure out the pathing and I might be missing something.

    Also, these are mail items.

    Thanks again!

    Tom

    Thursday, March 28, 2013 12:24 PM
  • In looking at that code I'd change the last line to this:
     
     
        Dim oMail As Outlook.MailItem = Globals.ThisAddIn._olApp.CreateItemFromTemplate(Path.Combine(sFolderName, "Ticket Please.oft"))
     
    The initial part of the code is getting the file path to the location where your code is executing. It compensates for any code moving by Windows due to any possible Volume Shadow Copy going on, and by using the UriBuilder it eliminates any "File://" start to the Uri.
     
    When in doubt about what some code is doing the best practice would be to run in debug mode, step the code and examine the results and any locals at each step of the way. That way you learn what's going on and aren't just copying code that makes no sense to you.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Phlegnh" <=?utf-8?B?UGhsZWduaA==?=> wrote in message news:83751804-dc7a-47c6-bd1e-3629ecff3701...

    Thanks Ken,

    I will try that. So where is that actually looking for the template oft's? I could not figure out the pathing and I might be missing something.

    Also, these are mail items.

    Thanks again!

    Tom


    Ken Slovak MVP - Outlook
    Thursday, March 28, 2013 2:49 PM