none
range MoveStart - bad parameter RRS feed

  • Question

  • just experimenting ...

    Insert a bunch of lines of text.  Then use Range MoveStart and MoveEnd to select 5 lines of text and apply the "No Spacing" style to the range.  Getting a "bad parameter" error on the range.MoveStart method.  I copied the code from http://msdn.microsoft.com/en-us/library/vstudio/1hsb2ey6.aspx  - why the error?

    also, is set_Style the method to use to set the style of the range?

    thanks,

          var missing = Type.Missing;
          var app = new Word.Application();
          var doc1 = app.Documents.Add(
            ref missing, ref missing, ref missing, ref missing);
    
          app.Visible = true;
    
          var r1 = doc1.Range(ref missing, ref missing);
    
          var rand = new Random();
          for (int i = 0; i < 1000; i++)
          {
            var randomNumber = rand.NextDouble().ToString();
            r1.InsertAfter(randomNumber + System.Environment.NewLine);
          }
    
          r1 = doc1.Range(ref missing, ref missing);
          r1.MoveStart(Word.WdUnits.wdLine, 5);
          r1.MoveEnd(Word.WdUnits.wdLine, 5) ;
          r1.set_Style("No Spacing") ;
    

    Saturday, November 17, 2012 8:46 PM

Answers

  • Hi Steve

    If you look carefully at the Intellisense or the Language Reference article for MoveStart you'll see that it has two optional parameters: Unit, Count. Since these are optional parameters they should be passed by Reference and as objects, so more like this:

       object oUnitLine = Word.WdUnits.wdLine;
       object oFive = 5;
       r1.MoveStart(ref oUnitLine, ref oFive);


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Steve Richter Sunday, November 18, 2012 4:19 PM
    Sunday, November 18, 2012 7:34 AM
    Moderator

All replies

  • Hi Steve

    If you look carefully at the Intellisense or the Language Reference article for MoveStart you'll see that it has two optional parameters: Unit, Count. Since these are optional parameters they should be passed by Reference and as objects, so more like this:

       object oUnitLine = Word.WdUnits.wdLine;
       object oFive = 5;
       r1.MoveStart(ref oUnitLine, ref oFive);


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Steve Richter Sunday, November 18, 2012 4:19 PM
    Sunday, November 18, 2012 7:34 AM
    Moderator
  • This does not fix the issue.  MSDN says you pass in a negative number to move it backwards, passing in a negative number throws the COM Exception "Bad parameter".  Regardless of if you pass it by ref or pass an inline number.

    (Inline parameters are behaving the exact same as "ref object".)

    Wednesday, October 2, 2013 7:26 PM
  • Please provide more information and repro steps for your particular issue so that we can be sure we're on the same wave-length...

    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 3, 2013 2:45 PM
    Moderator