Resolved Adding text from a document to a textbox in a custom taskpane

d1sturbed

New member
Joined
Sep 9, 2021
Messages
3
Programming Experience
10+
Quick explanation of the setup:

I have a custom taskpane loaded on startup as follows:

C#:
            tpane _ctrl = new tpane();
            var myPane = this.CustomTaskPanes.Add(_ctrl, "My Control");
            myPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight;
            myPane.Visible = true;
            myPane.Width = 400;

tpane is a UserControl that I've added a textbox to. So now when Word starts up with this add in, the taskpane will show up with the UserControl tpane and a textbox named tResult.

Now what I want to do is from within VBA call a function that will take the highlighted text on the word document and add it to this textbox. This is the function I have (working) in VBA that's calling upon the function in VSTO to perform this:
Visual Basic:
Sub CallVSTOMethod_addthetext()
    Dim addIn As COMAddIn
    Dim automationObject As Object
    Set addIn = Application.COMAddIns("Test002")
    Set automationObject = addIn.Object
    Debug.Print automationObject.f_addthetext(Selection.Text)
End Sub

And now my only issue is, how do I reference the textbox "tresult"

C#:
        public void f_addthetext(string seltext)
        {
          
                (This is the unknown for me here)
                tResult.text = seltext;

        }
 
Last edited by a moderator:
Assuming that tResult was a public property of your tpane class, then you would access it via _ctrl.tResult. You'll just have to make sure that you make your _ctrl a class variable instead of a local variable.

As a quick aside, Hungarian notation is not recommended in C# code. Use it all you want in your VBA, but the .NET Framework naming convention recommends not using Hungarian.
 
Thanks for the feedback. Yeah the Hungarian way was something that was embedded into me during school and I still have a tendency to slip back to it.

So on the issue at hand, I've been able to get everything running without error, but when I access the textbox in the taskpane it can't send text to it through the VBA macro and as I'm stepping through the code it's also not picking up on any current value of the textbox. My guess is this has to do with your comment of making sure _ctrl is a class variable instead of a local variable. So this is where I'm stuck because I am new to C# and in a situation of learning on the fly here. I'm going to post the code that I have right now and if you're able to, explain how to get the _ctrl to be a class variable if it isn't already:

First, ThisAddIn.cs

C#:
namespace Test002
{

    public partial class ThisAddIn
    {


        protected override object RequestComAddInAutomationService()
        {
            return MyTest.Instance;
        }

        public task_pane myusercontrol1;
        public Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {

            myusercontrol1 = new task_pane();
            myCustomTaskPane = this.CustomTaskPanes.Add(myusercontrol1, "Case Information");
            myCustomTaskPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
            myCustomTaskPane.Width = 450;
            myCustomTaskPane.Visible = true;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }    
        #endregion
    }
}

Next: MyTest.cs This is what VBA will call into

C#:
namespace Test002
{
    public interface iMyTest
    {

    }


    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public class MyTest : iMyTest
    {

        private static MyTest myTest;
        public static MyTest Instance
        {
            get {
                if (myTest == null)
                {
                    myTest = new MyTest();
                }
                return myTest;
            }
        }
        public void send_text(string texttosend)
        {

            task_pane ctrl = new task_pane();
            ctrl.word_list.Text = texttosend;
           
        }

And then of course my VBA code which I put before:

C#:
Sub CallVSTOMethod_addthetext()
    Dim addIn As COMAddIn
    Dim automationObject As Object
    Set addIn = Application.COMAddIns("Test002")
    Set automationObject = addIn.Object
    Debug.Print automationObject.f_addthetext(Selection.Text)
End Sub

So again to reiterate, everything works, except when I pass the text to the actual textbox, the textbox remains empty. I'm assuming that has to do with the way I'm handling _ctrl as you mentioned before. Any further guidance on this would be greatly appreciated, or even point me to a web/book source that I can figure this out. Thanks in advance.
 
Last edited by a moderator:
Thank you for trying to put your code in code tags. Unfortunately this site's iconography doesn't follow current industry patterns. The icon that looks like code tags >_ in most other sites is actually for inline code tags. On this site, the icon that looks like HTML tags </> is the one that is actually for code tags. I've edited your post above to use the appropriate code tags.
 
Instance member variables are like your myusercontrol1 and myCustomTaskPane. Static member variables are like your myTest.
 
I want to thank you for your help with this. Did some reading up and got a little more educated on the terminology and feeling pretty good about it. Solution just in case others drop by with similar issues.

Changed:
ThisAddIn:
public task_pane myusercontrol1;
public Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;

To this:
ThisAddIn:
        public static task_pane myUsercontrol1;
        public static Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;

And then using the same VBA function I used it to call the send_text method:

MyText.cs:
        public void send_text(string texttosend)
        {

            ThisAddIn.myUsercontrol1.word_list.Text += Environment.NewLine + texttosend;
            
        }
 
Back
Top Bottom