none
Value does not fall within the expected range. Microsoft.Office.Interop.Word RRS feed

  • Question

  • Hi, I have already asked this question on MSDN here, but didn't find any solution! 

    starting new thread! 

    I am having following exception in my project having Microsoft Interops, Com Ad in for Office, When assigning

    fldTemp.Result.Font = currRange.Font;
    //where currRange is instance of Word.Range 
    //and   fldTemp is instance of Word.Field ;

    System.ArgumentException: Value does not fall within the expected range.

       at Microsoft.Office.Interop.Word.Range.set_Font(Font prop)
       at xxx.yyy.ThisIsMyFun(args)

    same piece of code running on some machines!

    what's wrong? 

    Kind regards

    Friday, March 14, 2014 5:27 AM

Answers

  • I was doing this before

    object omissing = System.Reflection.Missing.Value; 

    fldTemp.Result.InsertFile(strFilepath, ref omissing, ref omissing, ref omissing, ref omissing);

    Result.Text was null on some PCs (didn't know why!)

    but this worked for me, changed little implementation 

    object omissing = System.Reflection.Missing.Value;

    object bLink = true; 

    fldTemp.Result.InsertFile(strFilepath, ref omissing, ref omissing, ref bLink, ref omissing);

    Now Result.Text is what in strFilepath!
    Cheers!

    Regards

    Zaffar Mahmood

    • Marked as answer by Zaffar Mahmood Wednesday, April 16, 2014 8:10 AM
    Wednesday, April 16, 2014 8:10 AM

All replies

  • Hello Zaffar,

    The following line of code consists of multiple property calls:

    fldTemp.Result.Font 

    That is why I'd recommend breaking them into separate lines of code.

    Where do you run the code? Do you develop a COM add-in or standalone application?

    What version (including the build number) of Word do you have installed on the problematic PC

    Friday, March 14, 2014 7:12 PM
  •  breaking them into separate lines! while this code is also running fine on other machines!

    its COM add-in for Office 2013/2010/2007 (running fine on other machines perfectly for any version of office).

    problematic PC: Window 8.1,  MS Word 2013 (15.0.4420.1017) 64 bit. 

    problematic PC: Window 7,  MS Word 2013 (15.0.4551.1509) 64 bit. 

    repeating again same thing is running fine on different PCs with different office versions! that's weird for me too!

    Monday, March 17, 2014 8:33 AM
  • Hello Zaffar,

    Did you try to use the same document? Do you have a document and the definitive steps that can help in reproducing the issue?

    What interop assemblies do you use in the add-in?

    Finally, please make sure that you have all the required prerequisites installed on the end user machines such as VSTO runtime, .Net framework, PIAs.

    Monday, March 17, 2014 6:13 PM
  • i tried to use same document too!
    Even no definitive steps! seems like that some machines don't allow that code to execute! 

    Interop.Microsoft.Office.Core.dll 2.4.0.0

    Interop.Microsoft.Office.Interop.Word.dll 8.4.0.0

    do I need to install VSTO runtime? asking because I am not developing in VSTO project!

    .Net frameworks are also installed else 4.5!

    Tuesday, March 18, 2014 7:05 AM
  • There is no need to install VSTO runtime if you don't develop a VSTO based add-in.

    Is the document opened in Protected View?

    Tuesday, March 18, 2014 7:25 AM
  • Are you sure there is exactly one font in the range defined by currRange in each case?

    Also, applying a font to a field result isn't all that reliable; it's generally better to apply it via a Charformat switch or to leave it to the underlying paragraph Style.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, March 18, 2014 10:10 AM
  • @Eugene Astafiev

    no document is not in Protected View!

    Tuesday, March 18, 2014 1:41 PM
  • @Eugene Astafiev, i just found one thing!

    while I was debugging my code, I noticed

      

    string filepath="blaablaa.rtf";

    fldTemp.Result.InsertFile(filepath, ref omissing, ref omissing, ref omissing, ref omissing);

    after above line execution

    fldTemp.Result.Text

    remains null! n fldTemp.Result.Font throws exception!!!

    WHY??

    Regards

    Thursday, April 3, 2014 8:11 AM
  • From the snippet you've posted, it looks like you're trying to insert a file into a field's result!!! AFAIK, that's not possible.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, April 3, 2014 10:02 AM
  • @Paul Edstein, I guess you did not read this thread from beginning!

    I said my code is running fine on some machines! the 2nd thing Result.InsertFile(... is interops method, check here

    before my last post I was thinking below code of piece is creating problem!

    fldTemp.Result.Font = currRange.Font;

    but now I found that above statement is throwing Exception because Result.Text is null, and file is not inserted correctly! 

    BTW thanks for your time :)

    Thursday, April 3, 2014 10:40 AM
  • @Paul Edstein, I guess you did not read this thread from beginning!

    I don't know what makes you think that... and you have given us precious little to work with.

    You don't even say what kind of field you're working with. What you previously said was that you were having a problem applying font attributes to a field result, which I quite rightly said is unreliable.

    Nowhere have you previously stated that the code you now say is throwing an exception during debugging has worked on any machine, let alone on some machines.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, April 3, 2014 10:48 AM
  • @Paul Edstein i think there is huge difference in unreliable and not possible!
    I pay thanks for your time Mr MS MVP - Word! and I did find solution.

    Kind Regards 

    Friday, April 11, 2014 11:21 AM
  • <<I did find solution.>>

    What solution did you find? Knowing what you've found could help others and then we could close this discussion...


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, April 12, 2014 3:14 PM
    Moderator
  • I was doing this before

    object omissing = System.Reflection.Missing.Value; 

    fldTemp.Result.InsertFile(strFilepath, ref omissing, ref omissing, ref omissing, ref omissing);

    Result.Text was null on some PCs (didn't know why!)

    but this worked for me, changed little implementation 

    object omissing = System.Reflection.Missing.Value;

    object bLink = true; 

    fldTemp.Result.InsertFile(strFilepath, ref omissing, ref omissing, ref bLink, ref omissing);

    Now Result.Text is what in strFilepath!
    Cheers!

    Regards

    Zaffar Mahmood

    • Marked as answer by Zaffar Mahmood Wednesday, April 16, 2014 8:10 AM
    Wednesday, April 16, 2014 8:10 AM
  • Now Result.Text is what in strFilepath!

    What that code is doing is wiping out the field; it is not updating the field. That confirms what I said before about updating not being possible by this method. The .Result.Text does not contain what is in strFilepath, because even .Result no longer exists. Furthermore, .Text (which your code doesn't even mention) cannot be manipulated this way.

    If that's the same field you were trying to apply the font colour to, it's no wonder that fails too.

    In any event, as I said before, changing a field's colour by changing the result colour is unreliable; anything that subsequently updates the field will wipe out the colouring. Try it for yourself and you will see this is so.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Wednesday, April 23, 2014 11:00 PM
    Wednesday, April 16, 2014 9:03 AM