-
Apr 3rd, 2022, 01:09 AM
#1
Thread Starter
Hyperactive Member
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?
-
Apr 3rd, 2022, 02:08 AM
#2
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
-
Apr 3rd, 2022, 02:27 AM
#3
Thread Starter
Hyperactive Member
Re: Error while adapting VBA code
Originally Posted by westconn1
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.
-
Apr 3rd, 2022, 12:50 PM
#4
Lively Member
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.
-
Apr 3rd, 2022, 10:17 PM
#5
Thread Starter
Hyperactive Member
Re: Error while adapting VBA code
Originally Posted by Dan_W
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
-
Apr 4th, 2022, 01:48 AM
#6
Lively Member
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
-
Apr 4th, 2022, 04:02 AM
#7
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
-
Apr 4th, 2022, 10:18 AM
#8
Thread Starter
Hyperactive Member
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.
-
Apr 5th, 2022, 05:04 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|