Results 1 to 14 of 14

Thread: Specifying time-out for objects?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    760

    Specifying time-out for objects?

    Hello!

    I'm using the following code to automate Word:


    Dim nWord As Object
    Set nWord = CreateObject("Word.Application")

    nWord.DisplayAlerts = 0 'wdAlertsNone
    nWord.Documents.Open (uFile)
    nWord.Selection.WholeStory

    However, it sometimes happens that the code is stuck at

    nWord.Documents.Open (uFile)

    The only thing that helps then is to manually kill the Word process in the taskmanager.

    How could I define time-out so that VB6 stops trying and I don't have to kill Word in order to make my application working again?

    Thank you!

    ps: The problem why it would hang was that there was a loading error. Word prompted me this error when I started Word from outside VB6). I was expecting that using the "OpenAndRepair" option would automatically fix this, but it still hung when I used these arguments:

    nWord.Documents.Open uFile, False, True, , , , , , , , , , True 'the last argument defines "OpenAndRepair"
    Last edited by tmighty2; May 14th, 2019 at 12:56 PM.

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

    Re: Specifying time-out for objects?

    if the file is a word docuement, you could try
    Code:
    set ndoc = getobject(ufile)
    that way you should be able to handle any error

    better to fix the file so it opens correctly
    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
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    760

    Re: Specifying time-out for objects?

    What would I do with nDoc then?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Specifying time-out for objects?

    ndoc would be the document object, so you can use any property or method of a document, or ndoc.application would be the application object
    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

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    760

    Re: Specifying time-out for objects?

    I should have stated the entire code. I'm trying to get the text of a Word document like this:

    Dim nWord As Word.Application
    Set nWord = CreateObject("Word.Application")

    nWord.DisplayAlerts = 0 'wdAlertsNone
    nWord.Documents.Open uFile, False, True, , , , , , , , , , True
    nWord.Selection.WholeStory

    GetText = nWord.Selection.Range.Text'return the text of the Word document

    nWord.ActiveDocument.Close False

    nWord.Quit False
    Set nWord = Nothing

    Can you please tell me in which way getting nDoc separately would help me?

  6. #6

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    760

    Re: Specifying time-out for objects?

    No. Before "var" become popular, I used "n" to indicate to myself that a variable would only be used within a sub or a function.

  8. #8
    Fanatic Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    760

    Re: Specifying time-out for objects?

    Quote Originally Posted by tmighty2 View Post
    Can you please tell me in which way getting nDoc separately would help me?
    Code:
    Dim nWord As Word.Application
    Dim nDoc  As Word.Document
    
    Set nWord = CreateObject("Word.Application")
    
    If nWord is nothing then
      'unable to automate Word
      'handle this situation here
    
      GoTo PROC_EXIT
    End If
    
    'set properties
    With nWord
        .Visible = False
        .DisplayAlerts = 0   'wdAlertsNone
    End With
        
    'open the document
    Set nDoc = nWord.documents.open(uFile)
    
    If nDoc is Nothing Then
      'unable to get the document contents
      'handle this condition here
    
      GoTo PROC_EXIT
    End If
    
    'read the contents from the Doc object
    GetText = nDoc.Content
    
    PROC_EXIT:
    Set nDoc = Nothing
    Set nWord = Nothing
    I use something like this and have never seen the lockup you experience.
    Last edited by AAraya; May 15th, 2019 at 09:46 AM.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    760

    Re: Specifying time-out for objects?

    Thank you very much. If nDoc is NOT nothing, do you think I should destroy the nDoc and try to load the file with nWord.Documents.Open()?

  10. #10
    Fanatic Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    760

    Re: Specifying time-out for objects?

    Quote Originally Posted by tmighty2 View Post
    Thank you very much. If nDoc is NOT nothing, do you think I should destroy the nDoc and try to load the file with nWord.Documents.Open()?
    If your goal is to get the text contained in the document, you don't need to interact with the nWord application object any more once you get a reference to the Doc. The nDoc object represents the Document itself and can be used to get the text from the document with this single line:

    Code:
    GetText = nDoc.Content
    I've edited my original code to include this as well.
    Last edited by AAraya; May 15th, 2019 at 09:41 AM.

  11. #11
    Fanatic Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    760

    Re: Specifying time-out for objects?

    There's a tutorial of sorts on Word Automation on this forum. Check it out:
    http://www.vbforums.com/showthread.p...-to-a-document

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    760

    Re: Specifying time-out for objects?

    Thank you very much!!!

  13. #13
    Fanatic Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    760

    Re: Specifying time-out for objects?

    How could I define time-out so that VB6 stops trying and I don't have to kill Word in order to make my application working again?
    To control the timeout period for OLE automation attempts, these two properties of the App object can be used:

    App.OleRequestPendingTimeout
    App.OleServerBusyTimeout

    See "Controlling Timeout Intervals" article: https://msdn.microsoft.com/en-us/lib...(v=vs.60).aspx

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Specifying time-out for objects?

    if opening the document is still a problem, there are ways to open xml type documents as xml file and read the text from that, there are several examples in this forum

    if the document will open using getobject all you need is
    Code:
    ntext =  getobject(ufile).content.text
    where ntext is a string or variant
    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

Tags for this Thread

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