|
-
May 14th, 2019, 12:12 PM
#1
Thread Starter
Fanatic Member
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.
-
May 14th, 2019, 04:29 PM
#2
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
-
May 14th, 2019, 04:31 PM
#3
Thread Starter
Fanatic Member
Re: Specifying time-out for objects?
What would I do with nDoc then?
-
May 15th, 2019, 05:38 AM
#4
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
-
May 15th, 2019, 08:25 AM
#5
Thread Starter
Fanatic Member
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?
-
May 15th, 2019, 08:37 AM
#6
Re: Specifying time-out for objects?
Is the n prefix in nWord and nDoc like hungarian notation's n for Integer?
cheers,
</wqw>
-
May 15th, 2019, 08:51 AM
#7
Thread Starter
Fanatic Member
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.
-
May 15th, 2019, 09:19 AM
#8
Fanatic Member
Re: Specifying time-out for objects?
 Originally Posted by tmighty2
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.
-
May 15th, 2019, 09:21 AM
#9
Thread Starter
Fanatic Member
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()?
-
May 15th, 2019, 09:34 AM
#10
Fanatic Member
Re: Specifying time-out for objects?
 Originally Posted by tmighty2
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.
-
May 15th, 2019, 09:39 AM
#11
Fanatic Member
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
-
May 15th, 2019, 09:45 AM
#12
Thread Starter
Fanatic Member
Re: Specifying time-out for objects?
-
May 15th, 2019, 10:11 AM
#13
Fanatic Member
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
-
May 15th, 2019, 04:28 PM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|