Results 1 to 9 of 9

Thread: Error while adapting VBA code

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    364

    Error while adapting VBA code

    When adapting running VBA code, a "438" error occurs:

    Code:
    Dim WIn As Object
    Dim Wdoc As Object
    Private Sub Form_Load()
    Set WIn = CreateObject("Word.Application")
        WIn.Visible = True
    Set Wdoc = WIn.Documents.Open(App.Path & "\file.docx")
        Wdoc.Activate
    Wdoc.ActiveDocument.Tables(1).Cell(Row:=6, Column:=2).Range.Delete    'Error 438
    End Sub
    What am I doing wrong?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,621

    Re: Error while adapting VBA code

    Wdoc is a document object, i doubt that you should refer to the activedocument of a document
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    364

    Re: Error while adapting VBA code

    Quote Originally Posted by westconn1 View Post
    Wdoc is a document object, i doubt that you should refer to the activedocument of a document
    But in VBA this line of code works.

  4. #4
    Member
    Join Date
    May 2021
    Posts
    32

    Re: Error while adapting VBA code

    I tried it in VBA, and the line of code in question (as provided by you) did not work for me, but I didn't expect it to either.
    As westconn1 explained, Wdoc is itself a Document, and ActiveDocument is a member of Application (not of the Document object).
    As such, of the following options:

    Code:
       WIn.ActiveDocument.Tables(1).Cell(Row:=6, Column:=2).Range.Delete 
       WDoc.Tables(1).Cell(Row:=6, Column:=2).Range.Delete 
       ActiveDocument.Tables(1).Cell(Row:=6, Column:=2).Range.Delete
    The first and third options will likely work, but you should avoid using ActiveDocument if possible (and the third would've worked only within Word VBA). Here, in my view, the second option is preferable.
    Last edited by Dan_W; Apr 3rd, 2022 at 12:54 PM.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    364

    Re: Error while adapting VBA code

    Quote Originally Posted by Dan_W View Post
    The first and third options will likely work, but you should avoid using ActiveDocument if possible (and the third would've worked only within Word VBA). Here, in my view, the second option is preferable.
    Thanks. The second line works.
    Please tell me why the following code does not work:
    Code:
    Public objRange As Object
    ....................
                 Wdoc.Tables(1).Cell(4, 1).Select  
                    Set objRange = Wdoc.Selection.Range 'Error 438

  6. #6
    Member
    Join Date
    May 2021
    Posts
    32

    Re: Error while adapting VBA code

    Your most code snippet will result in the 438 error for the same reason - like ActiveDocument, Selection is a member of the Application (and not of the Document).
    Referring to your original code, where you've assigned the Word.Application to the WIn variable, the following should work:

    Code:
     Set objRange = WIn.Selection.Range

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,621

    Re: Error while adapting VBA code

    if you develop your code using early bindings it may be a lot easier, then change to late binding before deploying

    ie. add a reference to word and declare strongly typed variable while writing the code
    this will give you the advantages of intellisense in the IDE, when the code is ready to deploy, change all variable to object and remove the reference
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    364

    Re: Error while adapting VBA code

    Dan_W, Thanks!
    westconn1, At first I thought to do so, but I was afraid.
    The problem is that I'm writing code on Win 7, on which office 2007 is installed. And I need to work on Win 10, on which 2016 office is installed.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,621

    Re: Error while adapting VBA code

    The problem is that I'm writing code on Win 7, on which office 2007 is installed. And I need to work on Win 10, on which 2016 office is installed.
    that is exactly what i was talking about

    even if they work in vba, you should try to avoid working with active anything, or the selection object, even in vba, they can lead to errors
    and in vb6 they may work once, then the same code fail subsequent times it is run, so as far as possible use fully qualified ranges and objects
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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