Application defined or object defined error word

Rory has already told you what the main problem with your code is.

I would recommend a slightly different approach to what you are trying to achieve.

Please note that xlDown should be avoided as much as possible. Consider the scenario where there is data only in cell A1. In such a case xlDown will select the entire Col A. The alternative is to find last row in Col A which has data and then create your range.

With ws
    '~~> Find Last Row in Col A and then create the range
    '~~> oXL is the Excel Application
    If oXL.WorksheetFunction.CountA(.Cells) <> 0 Then
        lastrow = .Cells.Find(What:="*", _
                      After:=.Range("A1"), _
                      Lookat:=xlPart, _
                      LookIn:=xlFormulas, _
                      SearchOrder:=xlByRows, _
                      SearchDirection:=xlPrevious, _
                      MatchCase:=False).Row
    Else
        lastrow = 1
    End If

    Set Rng = ws.Range("A1:A" & lastrow)
End With

And declare this at the top

Const xlPart As Long = 2
Const xlFormulas As Long = -4123
Const xlByRows As Long = 1
Const xlPrevious As Long = 2

And if you are sure that there will always be data in Col A then you can try this as well

    lastrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

    Set Rng = ws.Range("A1:A" & lastrow)

And declare this at the top

Const xlUp As Long = -4162

title keywords f1_keywords ms.prod ms.assetid ms.date

Application-defined or object-defined error

vblr6.chm1011299

vblr6.chm1011299

office

4c1ea0e8-e6f6-a960-eb13-b4dfc2bf96fe

06/08/2017

This message is displayed when an error generated with the Raise method or Error statement doesn’t correspond to an error defined by Visual Basic for Applications. It is also returned by the Error function for arguments that don’t correspond to errors defined by Visual Basic for Applications. Thus it may be an error you defined, or one that is defined by an object, including host applications like Microsoft Excel, Visual Basic, and so on. For example, Visual Basic forms generate form-related errors that can’t be generated from code simply by specifying a number as an argument to the Raise method or Error statement. This message has the following causes and solutions:

  • Your application executed an Err.Raisen or Errorn statement, but the number n isn’t defined by Visual Basic for Applications. If this was what was intended, you must use Err.Raise and specify additional arguments so that an end user can understand the nature of the error. For example, you can include a description string, source, and help information. To regenerate an error that you trapped, this approach will work if you don’t execute Err.Clear before regenerating the error. If you execute Err.Clear first, you must fill in the additional arguments to the Raise method. Look at the context in which the error occurred, and make sure you are regenerating the same error.

  • It may be that in accessing objects from other applications, an error was propagated back to your program that can’t be mapped to a Visual Basic error.

Check the documentation for any objects you have accessed. The Err object’s Source property should contain the programmatic ID of the application or object that generated the error. To understand the context of an error returned by an object, you may want to use the On Error Resume Next construct in code that accesses objects, rather than the On Error GoToline syntax.

List trappable errors for the host application

In the past, programmers often used a loop to print out a list of all trappable error message strings. Typically this was done with code such as the following:

For index = 1 to 500
    Debug.Print Error$(index)
Next index

Such code still lists all the Visual Basic for Applications error messages, but displays «Application-defined or object-defined error» for host-defined errors, for example those in Visual Basic that relate to forms, controls, and so on. Many of these are trappable run-time errors. You can use the Help Search dialog box to find the list of trappable errors specific to your host application. Click Search, type Trappable in the first text box, and then click Show Topics. Select Trappable Errors in the lower list box and click Go To.

For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).

  • Remove From My Forums
  • Question

  • Hello all,

    I have an access application that allows for the user to Save the current form record and also generate an email. After clicking the command button they are asked to allow or deny the event.

    If the user clicks Deny they receive the error message «Run-time error ‘287’: Application-defined or object-defined error»

    The user can then either click End or Debug. If they click End there isn’t an issue. If they click Debug the VBA debugger is opened and the code is displayed. This could result in the users making changes to the application code and cause it to break
    further.

    How can I fix the VBA code so it works without this error appearing?

    The code is below and attached is the error message and the point the debugger picks up the error. Any assistance is appreciated.

    Private Sub Command75_Click()
    Dim stId As Integer
    Dim db As Database
    Dim rs As DAO.Recordset
    Dim appOutLook As Outlook.Application
    Dim MailOutLook As Outlook.MailItem
    Dim stText As String
    Dim stDefect As String
    Dim stGkOrder As Long
    '==Save the current record
    If Me.Dirty Then Me.Dirty = False
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
    '==Set db = CurrentDb()
    '==Set rs = db.OpenRecordset("dbo.NonConfData")
    stId = Me.Id
    stDefect = Me.Combo45.Column(1)
    stGkOrder = Me.GKOrderNum
    stText = "NonConformance IR #" & " " & "Defect Category:" & stDefect & " " & "Against Order#" & stGkOrder
    With MailOutLook
        .BodyFormat = olFormatHTML
        .To = "capateam@company.com"
        ''.cc = ""
        ''.bcc = ""
        .Subject = "NonConformance Generated IR #" & stId
        .HTMLBody = stText
        .Send
    End With
    '==rs.Close


    Keith

    • Edited by

      Friday, December 27, 2013 9:13 PM

Answers

  • You need error handlers to properly manage errors.  Users should never see the End/Debug dialog.

    Here’s the basics:

    Function YourFunction()
      On Error Goto Error_Proc
    
      ' all your usual code goes here'
    
    
    Exit_Proc:
      'this is your exit portion'
      'all exits will be directed here'
      'use this to clean up any open objects'
      Exit Function
    Error_Proc:
      'this is your error handler'
      'with the On Error statement at the top,'
      'any errors jump to the specified label'
      ''
      'check errors:'
      Select Case Err.Number
        Case 287:
          Resume Exit_Proc 'ignore the error'
        Case Else:
          MsgBox "Error encountered: " & Err.Description
          Resume Exit_Proc 'display a message then exit'
    End Function

    This should be standard for pretty much every procedure.  Tools like the invaluable mz-tools (www.mztools.com) let you customize your handler and add it to a procedure with the click of a button.

    Do a google search for VBA error handling for more info, but the above is fully sufficient.  There’s more complex global handlers that can log errors, notify developers, etc, but this is all you really need.

    Cheers


    Jack D. Leach (Access MVP)
    UtterAccess Wiki: (Articles, Functions, Classes, VB7 API Declarations and more)

    Dymeng: blog |
    Services

    • Edited by
      Jack D Leach
      Saturday, December 28, 2013 3:04 PM
      changed error number from 291 to 287
    • Marked as answer by
      Keith732
      Friday, January 3, 2014 2:11 PM

  • Your Access instance probably has the option «Breal on All Errors» selected.

    In the code window, use the Menu command Tools / Options… / General tab and chgane the error-trapping option to «Break on Unhandled Errors» and then try your code again.

    Note: the code you posted WILL display the error message when an error occurs UNLIKE Jack’s suggested code (which trap all errors and display the error message for all errors EXCEPT error 287).


    Van Dinh

    • Marked as answer by
      George Hua
      Friday, January 3, 2014 9:48 AM

  • Home
  • VBForums
  • Visual Basic
  • Visual Basic 6 and Earlier
  • Application defined or object defined error

  1. Dec 19th, 2006, 08:43 AM


    #1

    Vishalgoyal is offline

    Thread Starter


    New Member


    Application defined or object defined error

    Hi All,

    I am trying to open an word doc from VB6.
    When i am using LATE binding of word application i am getting error :
    «runtime error 57121 application defined or Object defined error» on wrdDoc.Saved = True line of following code

    ————————
    Dim mwrdApp As Object
    wrdDoc As Object
    Private Sub Form_Load()
    Set mwrdApp = CreateObject(«Word.Application»)
    mwrdApp.Visible = True
    Set wrdDoc = mwrdApp.Documents.Open(«c:Blob03.doc»)
    wrdDoc.Saved = True
    Exit Sub
    End Sub
    ———————-

    But when i use EARLY binding it works fine.
    (After refering to Microsoft Word 11.0 Object library)
    ————————
    Option Explicit
    Dim mwrdApp As New Word.Application
    Dim wrdDoc As New Word.Document
    Private Sub Form_Load()
    mwrdApp.Visible = True
    Set wrdDoc = mwrdApp.Documents.Open(«C:Blob03.doc»)
    wrdDoc.Saved = True
    End Sub

    As i need to user late binding in my application, i need a solution of that.
    Also, the word doc contains macros. If i remove macros both bindings work fine. But unfortunately i do not have that luxury to remove macro.

    Please help me

    Regards
    Vishal


  2. Dec 19th, 2006, 03:17 PM


    #2

    Re: Application defined or object defined error

    Never have done late binding… but don’t you still need to create the word document object?

    VB Code:

    1. Dim wrdDoc As New Word.Document

    2. ie

    3. Set wrdDoc = CreateObject("Word.Document")


  3. Dec 19th, 2006, 04:07 PM


    #3

    woodyz is offline


    Addicted Member


    Re: Application defined or object defined error

    Quote Originally Posted by randem

    Never have done late binding… but don’t you still need to create the word document object?

    VB Code:

    1. Dim wrdDoc As New Word.Document

    2. ie

    3. Set wrdDoc = CreateObject("Word.Document")

    That would not be late binding.
    using

    VB Code:

    1. Dim wrdDoc As New Word.Document

    is early binding.

    It is also instantiating the object, which is not needed in this case since the return of the call to the Documents.Open method returns an object.

    However, it is unclear why the error that is being seen is occuring. Sorry I can’t be of assistance.


  4. Dec 19th, 2006, 04:10 PM


    #4

    Re: Application defined or object defined error

    Quote Originally Posted by woodyz

    That would not be late binding…

    I think that randem meant:

    VB Code:

    1. Set wrdDoc = CreateObject("Word.Document")

    … is late binding.


  5. Dec 19th, 2006, 11:51 PM


    #5

    Re: Application defined or object defined error

    Exactly gavio… One really has to read the whole post…

    Example

    I.E


  6. Dec 20th, 2006, 06:43 AM


    #6

    Re: Application defined or object defined error

    Weird. Would you mind posting the whole code? Maybe there are some conflicts, which I don’t have any idea yet.


  7. Dec 20th, 2006, 08:29 AM


    #7

    woodyz is offline


    Addicted Member


    Re: Application defined or object defined error

    Quote Originally Posted by randem

    Exactly gavio… One really has to read the whole post…

    Example

    I.E

    You are suggesting I didn’t read the whole, 3-line post?
    To reiterate: It is not late binding when you declare a variable as a specific type. That IS early binding, regardless of how you instantiate the object.

    This line is NOT an example of early binding, it is only an example of instantiating an object of type Word.Document:

    Set wrdDoc = CreateObject(«Word.Document»)

    And I was referring to your suggestion that the wrdDoc needs to be instantiated before using it in the following line, which it does not:

    Set wrdDoc = mwrdApp.Documents.Open(«C:Blob03.doc»)

    The Documents.Open method returns a word.document object, which the early bound wrdDoc then refers to. There is no need to first create a Document object because that object will be dereferenced as soon as it is set in the call to mwrdApp.Documents.Open.

    Last edited by woodyz; Dec 20th, 2006 at 08:56 AM.


  8. Dec 20th, 2006, 08:42 AM


    #8

    woodyz is offline


    Addicted Member


    Re: Application defined or object defined error

    Quote Originally Posted by gavio

    I think that randem meant:

    VB Code:

    1. Set wrdDoc = CreateObject("Word.Document")

    … is late binding.

    This is not late binding unless the wrdDoc variable itself were declared as Object. The form of instantiation does not make any difference. Just because he is using CreateObject does not mean that it is an example of late binding. When a variable is declared as a specific type, this is known as early binding — or vtable binding. VB has direct access to each method via the type library when using early binding, and therefore the executable code generated is more efficient.

    Here is an example of late binding (note that even though the vb app must have a reference to the Example dll in this example, the oLate variable is declared as Object):

    Dim oLate As Object
    Set oLate = New Example.Late
    Call oLate.DoSomething

    This is early binding:

    Dim oLate as Example.Late
    Set oLate = New Example.Late
    Call oLate.DoSomething

    Again: How the object is instantiated is not what determines late binding, it is how the variable is declared.


  9. Dec 20th, 2006, 09:28 AM


    #9

    Ember is offline


    Hyperactive Member


    Re: Application defined or object defined error

    VB Code:

    1. wordApp.ActiveDocument.Saved = True

    means that you will not be asked to save changes.

    VB Code:

    1. wordApp.ActiveDocument.SaveAs "C:Blob03.doc"

    saves the document.


  10. Dec 20th, 2006, 09:47 AM


    #10

    Re: Application defined or object defined error

    Quote Originally Posted by woodyz

    This is not late binding unless the wrdDoc variable itself were declared as Object…

    That’s what we both meant…


  11. Dec 20th, 2006, 10:04 AM


    #11

    woodyz is offline


    Addicted Member


    Re: Application defined or object defined error

    Quote Originally Posted by gavio

    That’s what we both meant…

    Then why didn’t you say that instead of giving an example of instantiating an object, which has nothing to do with late binding? It doesn’t make sense to me that you would belittle me for pointing out your error. randem’s post very clearly does not provide an example of late binding (He states that he has never done late binding), and I was merely clarifying that. Why not accept the correction and move on?

    Last edited by woodyz; Dec 20th, 2006 at 10:38 AM.


  • Home
  • VBForums
  • Visual Basic
  • Visual Basic 6 and Earlier
  • Application defined or object defined error


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Forum Rules


Click Here to Expand Forum to Full Width

Понравилась статья? Поделить с друзьями:
  • Application cutcopymode vba excel описание
  • Application commandbars add vba excel
  • Apple numbers to excel
  • Apple alternative for word
  • Appendix table in word