none
ReferenceAssemblyFromVbaProject - Not usable for Word2013 and 2016 VSTO-Template? RRS feed

  • Question

  • Hello, 

    i am using Visual Studio Professional 2015 with Update 3 and want to start a new Office/Sharepoint - Office-Add-In-Project.

    Starting with the following tutorial (but adapted it for Word 2016): https://msdn.microsoft.com/de-de/library/bb608613.aspx 

    Everything works fine, i can use c#-Code in a Word-VBA-Project. But if i want to use the same procedure with a Word template (dotm on Word 2016) i cannot find the property ReferenceAssemblyFromVbaProject in the class ThisDocument.cs.

    Summary:

    If i use the Project option Visual c# - Office/Sharepoint - Office-Add-Ins - Word 2013 and 2016 VSTO-Document i can find the property ReferenceAssemblyFromVbaProject right after creating the project in the class ThisDocument.cs.

    If i use the project option Visual c# - Office/Sharepoint - Office-Add-Ins - Word 2013 and 2016 VSTO-Document i cannot find the property ReferenceAssemblyFromVbaProject right after creating the project in the class ThisDocument.cs.

    Now my Question is:

    Is it not possible to use the property ReferenceAssemblyFromVbaProject in a Word Template Project?

    Thanks for your help.





    • Edited by Stefan Aures Thursday, September 1, 2016 8:32 AM
    Thursday, September 1, 2016 8:15 AM

Answers

  • Well... then i will use the comvisible-Method.

    For me this was the solution:

    1. Loading the Word-Template-Project with dotm.

    2. Properties - Application - Assemblyinformations -> Make it com-visible | Build -> Register Cominterop (if this is not working, use Postbuild-Event: "%Windir%\Microsoft.NET\Framework\v4.0.30319\regasm" /codebase /tlb "$(MSBuildProjectDirectory)\$(OutputPath)$(AssemblyName).dll")

    3. Use the following in your class (Example): 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    // A Hello World! program in C#.
    namespace HelloWorld
    {
        [System.Runtime.InteropServices.ComVisible(true)]
        [ClassInterface(ClassInterfaceType.AutoDual)]
        public class Hello
        {
            public string MainMethod(string input)
            {
                return "Hallo" + input;
                
            }
            public void MessageMethod()
            {
                MessageBox.Show("Hallo Welt");
            }
        }
    }

    4. Build it

    5. F5 -> Your Word-Template is opening

    6. Open VBA-Project

    7. Set a new reference for your Studio-Project.

    8. Create a new module with following Example-Code:

    Sub Test()
        Dim Testclass As New Hello
        MsgBox Testclass.MainMethod("Welt")
        Testclass.MessageMethod
    End Sub

    You should now get 2 Message Boxes.

    • Marked as answer by Stefan Aures Thursday, September 1, 2016 12:49 PM
    Thursday, September 1, 2016 11:50 AM

All replies

  • Hi,

    The ReferenceAssemblyFromVbaProject property is used for exposing the add-in code in C#/VB.NET project to VBA.

    We are unable to call code in word template project of document-level customization from VBA

    Please visit Combining VBA and Document-Level Customizations, you could find the note below in the node Calling Code in Document-Level Customization from VBA.

    This feature cannot be used in Word template projects. It can be used only in Word document, Excel workbook, or Excel template projects.





    Thursday, September 1, 2016 9:40 AM
    Moderator
  • Well... then i will use the comvisible-Method.

    For me this was the solution:

    1. Loading the Word-Template-Project with dotm.

    2. Properties - Application - Assemblyinformations -> Make it com-visible | Build -> Register Cominterop (if this is not working, use Postbuild-Event: "%Windir%\Microsoft.NET\Framework\v4.0.30319\regasm" /codebase /tlb "$(MSBuildProjectDirectory)\$(OutputPath)$(AssemblyName).dll")

    3. Use the following in your class (Example): 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    // A Hello World! program in C#.
    namespace HelloWorld
    {
        [System.Runtime.InteropServices.ComVisible(true)]
        [ClassInterface(ClassInterfaceType.AutoDual)]
        public class Hello
        {
            public string MainMethod(string input)
            {
                return "Hallo" + input;
                
            }
            public void MessageMethod()
            {
                MessageBox.Show("Hallo Welt");
            }
        }
    }

    4. Build it

    5. F5 -> Your Word-Template is opening

    6. Open VBA-Project

    7. Set a new reference for your Studio-Project.

    8. Create a new module with following Example-Code:

    Sub Test()
        Dim Testclass As New Hello
        MsgBox Testclass.MainMethod("Welt")
        Testclass.MessageMethod
    End Sub

    You should now get 2 Message Boxes.

    • Marked as answer by Stefan Aures Thursday, September 1, 2016 12:49 PM
    Thursday, September 1, 2016 11:50 AM