Try...Catch...Finally - how to use?
Try...Catch...Finally - how to use?
(OP)
This follows on from Thread1251-924215
I am using VFP8 but haven't really got to grips with TRY...CATCH...FINALLY (TCF).
Here is a snippet of code (there is more code before and after this bit):
The line oDoc = oWord.Documents.Open(THIS.txtLetter.Value) sometimes causes an error.
To implement TCF, should I:
Stewart
I am using VFP8 but haven't really got to grips with TRY...CATCH...FINALLY (TCF).
Here is a snippet of code (there is more code before and after this bit):
CODE
IF NOT TextCopied
oWord = CREATEOBJECT("Word.Application")
WAIT WINDOW NOWAIT "Mailmerge - copying text"
oDoc = oWord.Documents.Open(THIS.txtLetter.Value)
oRange = oDoc.Range()
oRange.Copy()
CopiedText = _CLIPTEXT
&& ....assign the clipboard contents to a variable
&& to prevent problems should the user copy
&& some text to the clipboard in another application
TextCopied = .T.
ENDIF
oWord = CREATEOBJECT("Word.Application")
WAIT WINDOW NOWAIT "Mailmerge - copying text"
oDoc = oWord.Documents.Open(THIS.txtLetter.Value)
oRange = oDoc.Range()
oRange.Copy()
CopiedText = _CLIPTEXT
&& ....assign the clipboard contents to a variable
&& to prevent problems should the user copy
&& some text to the clipboard in another application
TextCopied = .T.
ENDIF
The line oDoc = oWord.Documents.Open(THIS.txtLetter.Value) sometimes causes an error.
To implement TCF, should I:
- put the whole IF...ENDIF inside the TRY and, if it caused an error, set a variable to FALSE and test that variable in the line beyond the TRY...ENDTRY to see if the rest of the code should execute
- put just the offending line inside the TCF structure
- put the preceding lines and all the lines that follow inside the TCF structure?
Stewart
RE: Try...Catch...Finally - how to use?
CODE
TRY
WAIT WINDOW NOWAIT "Mailmerge - copying text"
oWord = CREATEOBJECT("Word.Application")
oDoc = oWord.Documents.Open(THIS.txtLetter.Value)
oRange = oDoc.Range()
oRange.Copy()
CopiedText = _CLIPTEXT
&& ....assign the clipboard contents to a variable
&& to prevent problems should the user copy
&& some text to the clipboard in another application
TextCopied = .T.
CATCH TO oExc
messagebox('There was a problem: '+oExc.Message,0,'Error')
FINALLY
RELEASE oRange
RELEASE oDoc
RELEASE oWord
ENDTRY
* if it wasn't successful, TextCopied never was set .T.
ENDIF
It really all depends on where you want user interface.
- Bill
Get the best answers to your questions -- See FAQ481-4875.