howto link a textrange to a scrallbar within Word (VSTO 2005)

Hi,
I would like to display text using a scrollbar within a Word document.
Let's ask the other way round, I know how to call the VScrollBar constructor

[Microsoft.Office.Tools.Word.Controls.VScrollBar vScrollBar = this.Controls.AddVScrollBar(range, 15, 50, "vScrollBar");]

(and it shows nicely in the Word document when launching the application), but how can I link some text or textrange to the VScrollBar control

Thanks, Claudia


Answer this question

howto link a textrange to a scrallbar within Word (VSTO 2005)

  • Thelostcircuit

    Hi louri,
    Thanks for your reply! I just happen to see it. Your code works great... for my project I chose the RichTextBox control which is basically a textbox with a scrollbar.

    I was wondering if it is possible to attach a table to a scrollbar I have tried it, but it did not work :-(

    private void ThisDocument_Startup(object sender, System.EventArgs e)
    {
    Word.Range range = this.Range(ref missing, ref missing);
    Microsoft.Office.Tools.Word.Controls.VScrollBar vScrollBar = this.Controls.AddVScrollBar(range, 15, 200, "vScrollBar");


    Word.Table text = range.Tables.Add(range, 1, 1, ref missing, ref missing);
    text.Rows[1].Cells[1].Range.Text = @"
    Word.Range range = this.Range(ref missing, ref missing);
    Microsoft.Office.Tools.Word.Controls.VScrollBar vScrollBar = this.Controls.AddVScrollBar(range, 15, 50, vScrollBar);

    vScrollBar.Maximum = 20;
    vScrollBar.LargeChange = 1;

    vScrollBar.Scroll += delegate
    {
    text.Select();
    };
    }

    thanks, Claudia

  • Shawnk

    Hi Claudia,

    here's some code to get you started:

    private void ThisDocument_Startup(object sender, System.EventArgs e)
    {
    Word.Range range = this.Range(ref missing, ref missing);
    Microsoft.Office.Tools.Word.Controls.VScrollBar vScrollBar = this.Controls.AddVScrollBar(range, 15, 50, "vScrollBar");

    Microsoft.Office.Tools.Word.Controls.TextBox text = this.Controls.AddTextBox(range, 15, 50, "textBox");
    text.Multiline = true;

    text.Text = @"
    Word.Range range = this.Range(ref missing, ref missing);
    Microsoft.Office.Tools.Word.Controls.VScrollBar vScrollBar = this.Controls.AddVScrollBar(range, 15, 50, vScrollBar);

    Microsoft.Office.Tools.Word.Controls.TextBox text = this.Controls.AddTextBox(range, 15, 50, textBox);
    text.Multiline = true;
    ";

    vScrollBar.Maximum = text.Lines.Length;
    vScrollBar.LargeChange = 1;

    vScrollBar.Scroll += delegate
    {
    int idx = text.GetFirstCharIndexFromLine(vScrollBar.Value);
    text.Select(idx, 0);
    text.ScrollToCaret();
    };
    }

    ----

    Iouri

    This posting is provided AS-IS and confers no rights.


  • howto link a textrange to a scrallbar within Word (VSTO 2005)