Results 1 to 3 of 3

Thread: VB 6.0 Error: Object Variable or with block variable not set

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    1

    VB 6.0 Error: Object Variable or with block variable not set

    I am writing a VB 6.0 application where in I want to copy data from one word document to another.I am able to open the source word document and count the number of tables present in the document, but I am unable to select any data present in that word document.

    Code:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Public wdApp As New Word.Application
    Public wddoc As Word.Document
    
    Public Sub OpenSrcWord()
         Dim strSCOId As String
    
        wdApp.Documents.Add frmStoryBoard.txtSrcWordDoc.Text, , , True
        Set wddoc = wdApp.ActiveDocument
    
       ActiveDocument.Tables(1).Cell(2, 1).Select
       strSCOId = Selection.Text
    End Sub
    I get the error when I reach the the statement that is in bold.
    Also I have used the same code in my another application which I had developed sometime back; where it works fine.
    I have selected the word object library reference in the Project.

    Any help in this regard would be appreciated !

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: VB 6.0 Error: Object Variable or with block variable not set

    To me it seems that Selection is not defined. If it is defined, where? Does the code get executed? You can add a few Debug.Print lines to follow the logic.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VB 6.0 Error: Object Variable or with block variable not set

    Welcome to VBForums

    That line is not the only one that has a problem - the line before it has essentially the same problem (both of them together have another), and the way you set wddoc also has an issue.


    As implied by Merri, the problem is that Selection (and ActiveDocument) are not valid in your VB program - they are sub-objects of Word, and can only be used without a prefix if you are using code inside the Word VBA editor (and even then it isn't recommended). Inside your VB code you must to prefix them with appropriate parent objects, eg:
    Code:
       wdApp.ActiveDocument.Tables(1).Cell(2, 1).Select
       strSCOId = wdApp.Selection.Text
    ...as you have a variable for the document, the first line would be better like this:
    Code:
       wddoc.Tables(1).Cell(2, 1).Select
    Not specifying parent objects always creates problems to some degree, but they aren't always obvious like the error you got - most often you will get an extra hidden copy of Word running, which eats up memory, stops your code from running a second time, and might show a "save changes?" message when you shut down the computer.


    As to the other two problems I referred to, they are based on what is Active or Selected. While code using those methods usually works, it does so more slowly, and when it fails it can cause massive problems (such as accidentally deleting all of the work the user has been doing for the last 5 hours).

    It is surprisingly easy to avoid... in the case of Selection you basically remove the ".Select" from the end of one line, and the "x.Selection" from the next. For the code above, this:
    Code:
       wddoc.Tables(1).Cell(2, 1).Select
       strSCOId = wdApp.Selection.Text
    ..becomes this:
    Code:
       strSCOId = wddoc.Tables(1).Cell(2, 1).Text
    As for what is Active, it depends on the situation... for the code above it was fairly obvious to replace wdApp.ActiveDocument with the variable wddoc , but others can be a bit tricky. In terms of the Set, you would merge the two lines:
    Code:
        wdApp.Documents.Add frmStoryBoard.txtSrcWordDoc.Text, , , True
        Set wddoc = wdApp.ActiveDocument
    ...so that instead of creating a document and then storing whichever one happens to be active, it stores the one that was created:
    Code:
        Set wddoc = wdApp.Documents.Add (frmStoryBoard.txtSrcWordDoc.Text, , , True)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width