none
vb.net Find object late binding code RRS feed

  • Question

  • Running vb.net code for MS office applications.  The Code was working fine until recently.

    Recently on the Find object, on the execute, the stack error message displays:

    Stack Trace:
       at Microsoft.Office.Interop.Word.Find.Execute(Object& FindText, Object& MatchCase, Object& MatchWholeWord, Object& MatchWildcards, Object& MatchSoundsLike, Object& MatchAllWordForms, Object& Forward, Object& Wrap, Object& Format, Object& ReplaceWith, Object& Replace, Object& MatchKashida, Object& MatchDiacritics, Object& MatchAlefHamza, Object& MatchControl)

    Ive discovered the following ms-bug on interop generates An Exception Error Using the Word Find Object

    http://support.microsoft.com/default.aspx?scid=kb;en-us;313104

    2 recommendations provided here:

    1. rewrite the early binding code to late binding

    heres the early binding code:

    Dim range As MSWord.Range = Nothing
    dim documentOutputController As Corrs.Business.Controllers.DocumentOutputController = New Corrs.Business.Controllers.DocumentOutputController
    If documentOutputController.GetProtection(wrdDoc).Protection = MSWord.WdProtectionType.wdNoProtection Then
    
    range = wrdDoc.Document.Content
    range.Find.Execute(FindText:="VARIABLE LIST", Forward:=True)
    If range.Find.Found = True Then
    Return True
    Else
    Return False
    End If
    End If
    

     

    Ive written as late binding code however I'm still getting an exception error displayed.

    Question:

    can someone pls provide sample late binding code on the Find object

     

    2. the second recommendation was using  the command line, use Regtlib.exe with the full path to the type library to reregister the library for COM.

    I have VS2008 therefore Regtlib.exe wasnt available,and regtlibv12.exe was.

    Heres my registration, however this didnt work either

    cd "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\"
    regtlibv12 "C:\Program Files\Microsoft Office\Office12\MSWORD.OLB"

     

    any ideas?

    thanks in advace..

    dd





    • Edited by Mike Feng Wednesday, April 20, 2011 7:23 AM Make it more readable
    • Moved by Mike Feng Wednesday, April 20, 2011 7:50 AM VSTO (From:Visual Basic Language)
    • Moved by Cindy Meister MVPModerator Wednesday, April 20, 2011 8:08 AM not VSTO-specific (From:Visual Studio Tools for Office)
    Tuesday, April 19, 2011 5:36 AM

Answers

  • Hi dd

    First, I'm going to move this to the Word for Developers forum as you do not appear to be using the VSTO technology.

    Then, you don't actually post the error message you're getting. Is it the message in the KB article?

    FWIW you're not using late-binding in the code snippet you show. That's still early binding. For late binding, you'd need to change this:
        Dim range As Object = Nothing

    and then you CANNOT use named parameters in the Find.Execute method. You have to list them, comma-delimited, in the exact order the method requires them, with "empty commas" for parameters you aren't passing. As you don't mention the version(s) of Word involved I won't attempt to write this out for you.

    See also this KB article, which has a VB-language example: http://support.microsoft.com/kb/292744/EN-US


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Tuesday, April 26, 2011 4:23 AM
    Wednesday, April 20, 2011 8:08 AM
    Moderator

All replies

  • Hi Rohit28,

     

    I have moved this thread to

    VSTO forum where you may get much better support because more this aspect experts live there.

    Thanks for your understanding.

     

    Best regards,
    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 20, 2011 7:49 AM
  • Hi dd

    First, I'm going to move this to the Word for Developers forum as you do not appear to be using the VSTO technology.

    Then, you don't actually post the error message you're getting. Is it the message in the KB article?

    FWIW you're not using late-binding in the code snippet you show. That's still early binding. For late binding, you'd need to change this:
        Dim range As Object = Nothing

    and then you CANNOT use named parameters in the Find.Execute method. You have to list them, comma-delimited, in the exact order the method requires them, with "empty commas" for parameters you aren't passing. As you don't mention the version(s) of Word involved I won't attempt to write this out for you.

    See also this KB article, which has a VB-language example: http://support.microsoft.com/kb/292744/EN-US


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Tuesday, April 26, 2011 4:23 AM
    Wednesday, April 20, 2011 8:08 AM
    Moderator
  • Thanks for moving to the appropriate forum

    Cindy - Yes Ive posted the error message in my orginial post.

    The actual error message im getting is:

    Stack Trace:
     at Microsoft.Office.Interop.Word.Find.Execute(Object& FindText, Object& MatchCase, Object& MatchWholeWord, Object& MatchWildcards, Object& MatchSoundsLike, Object& MatchAllWordForms, Object& Forward, Object& Wrap, Object& Format, Object& ReplaceWith, Object& Replace, Object& MatchKashida, Object& MatchDiacritics, Object& MatchAlefHamza, Object& MatchControl)

    The error occurs on the line

    range.Find.Execute(FindText:="VARIABLE LIST", Forward:=True)

    Ive re-written the code several times and managed to break it down to the error occuring on the "Execute"

    The code I posted was early binding code

    The version of Word  im using is Word -Office 2007 SP3

    thank you


    Monday, May 2, 2011 12:25 AM