PDA

View Full Version : Solved: Text box instead of message box



Gil
02-23-2011, 12:24 PM
I am using this code to confirm if a Folder exists which confirms with a message box. I would like to copy the path to paste elsewhere but unable to with the message box. Can a text box be used instead.
I tried a few different things but have failed. Any ideas.

Thanks Gil


Option Explicit

Function FileOrDirExists(PathName As String) As Boolean
'Macro Purpose: Function returns TRUE if the specified file
' or folder exists, false if not.
'PathName : Supports Windows mapped drives or UNC
' : Supports Macintosh paths
'File usage : Provide full file path and extension
'Folder usage : Provide full folder path
' Accepts with/without trailing "\" (Windows)
' Accepts with/without trailing ":" (Macintosh)

Dim iTemp As Integer

'Ignore errors to allow for error evaluation
On Error Resume Next
iTemp = GetAttr(PathName)

'Check if error exists and set response appropriately
Select Case Err.Number
Case Is = 0
FileOrDirExists = True
Case Else
FileOrDirExists = False
End Select

'Resume error checking
On Error GoTo 0
End Function

Sub TestItWithWindows()
'Macro Purpose: To test the FileOrDirExists function with Windows
'Only included to demonstrate the function. NOT required for normal use!

Dim sPath As String

'Change your directory here
sPath = "C:\Desktop\Any Name You Like"

'Test if directory or file exists
If FileOrDirExists(sPath) Then
TxtBox sPath & " exists!"
Else
MsgBox sPath & " does not exist."
End If
End Sub

bdsii
02-23-2011, 12:55 PM
Hello ! I am relatively new to this but it seems to me that the directory path is already stored in your variable sPath so you could use that later in your code to "copy" elsewhere. Is that what you are wanting to do or are you wanting to display the data from sPath on-screen so you can use your mouse to highlight and then copy and paste ?

Gil
02-23-2011, 01:49 PM
Hello
Yes I am wanting to be able to highlight then copy and paste. If it could paste directly into a cell that would be fine also.
Gil

MarkNumskull
02-23-2011, 01:56 PM
Hi Gil,

You could just create a simple userform with a textbox. then you could call it as such:

If FileOrDirExists(sPath) Then
Userform1.textbox1.text = sPath & " exists!"
Userform1.show
Else
MsgBox sPath & " does not exist."
End If


You could then add any text you need to the userform in a label to explain what it does.

Gil
02-23-2011, 02:10 PM
Thanks Mark, I will try it later as I have been called out.
Gil

bdsii
02-23-2011, 02:26 PM
Yeah, what MarkNumskull (http://www.vbaexpress.com/forum/member.php?u=23879) said ! :-) I learned something myself with his answer. If you just wanted to populate a cell, something like this may work:


'Test if directory or file exists
If FileOrDirExists(sPath) Then

' change A2 with the cell of your choice
Range("A2").Value = sPath

TxtBox sPath & " exists!"
Else
MsgBox sPath & " does not exist."
End If




uhhh, beware of my advice though, I am kinda new at this :-)

Gil
02-23-2011, 05:41 PM
Hello Mark & Bdsii

Spoilt for choice with 2 great suggestions that work for me.
Many thanks for you input.

Gil

:clap:

MarkNumskull
02-24-2011, 01:02 AM
Hi, glad i could be of help! :*)