Msgbox help

L

LEU

In my document form I have a command button that pastes information into my
form. How would I add to my existing macro that if “c:\TempA.doc†does not
exist it would give a message that the file does not exist and then it
returns to the open form. Right now if “c:\TempA.doc†does not exist it gives
an error “5174†and stops the macro.

Private Sub CmdPaste1_Click()
Dim DocA As Document
Dim oFF As FormFields
Set DocA = Documents.Open("c:\TempA.doc")
Set oFF = ActiveDocument.FormFields
Me.Text3.Value = oFF("Text41").Result
Me.Text4.Value = oFF("Text27").Result
Me.Text5.Value = oFF("Text28").Result
‘AND SO ON
DocA.Close
Kill "c:\TempA.doc"
Set DocA = Nothing
Set oFF = Nothing
End Sub
 
G

Greg Maxey

Use error handling:

Private Sub CommandButton1_Click()
Dim DocA As Document
Dim oFF As FormFields
On Error GoTo Err_Handler
Set DocA = Documents.Open("c:\TempA.doc")
Set oFF = ActiveDocument.FormFields
'Me.Text3.Value = oFF("Text41").Result
'Me.Text4.Value = oFF("Text27").Result
'Me.Text5.Value = oFF("Text28").Result
DocA.Close
Kill "c:\TempA.doc"
Set DocA = Nothing
Set oFF = Nothing
Exit Sub
Err_Handler:
If Err.Number = 5174 Then MsgBox "File does not exists"
End Sub

See:
http://gregmaxey.mvps.org/Error_Handling_Basics.htm
 
L

LEU

Greg,

When I run it, it gives me a compile error 'Label not Defined'
It stops at the following line in the macro:

On Error GoTo Err_Handler


LEU
 
G

Greg Maxey

That is what you will see if you don't have

Err_Handler:

after the Exit Sub line in the code I posted. Are you sure you applied
"all" of the code I provided to you?
 
L

LEU

I found my problem. Fat fingers when typing. It works fine just the way you
had it. Thanks for the fast responce.
LEU
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top