VBA - determine name of the file

eurano

New Member
Joined
Sep 13, 2015
Messages
25
Hi
In following directory I have file wich in every computer is named differently and I want to write function wich allow me to determine the name of this file. The extension is ".nsf" and its allways the only file in this directory. I made the code but it doesnt work.

C:\Users\kuckam\AppData\Local\IBM\Notes\Data\mail\gdansk

Function GetNSF(ByRef strFileName As String) As Boolean


On Error Resume Next
strFileName = Dir(Environ("COMPUTERNAME") & "\Local\IBM\Notes\Data\Mail\gdansk\*.nsf")

GetNSF = (strFileName <> vbNullString)

End Function

Sub test()


Dim strFileName As String
If GetNSF(strFileName) Then
Debug.Print strFileName
End If

End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi
In following directory I have file wich in every computer is named differently and I want to write function wich allow me to determine the name of this file. The extension is ".nsf" and its allways the only file in this directory. I made the code but it doesnt work.

C:\Users\kuckam\AppData\Local\IBM\Notes\Data\mail\gdansk

Function GetNSF(ByRef strFileName As String) As Boolean


On Error Resume Next
strFileName = Dir(Environ("COMPUTERNAME") & "\Local\IBM\Notes\Data\Mail\gdansk\*.nsf")

GetNSF = (strFileName <> vbNullString)

End Function

Sub test()


Dim strFileName As String
If GetNSF(strFileName) Then
Debug.Print strFileName
End If

End Sub
Hi eurano,

How about something like this to get you started?

Code:
Sub LoopThroughFileNames()
' Define variable
Dim FileName As Variant
    ' For each file in the specified folder
   FileName = Dir("C:\Users\kuckam\AppData\Local\IBM\Notes\Data\mail\gdansk\")
   ' If the filename is not nothing then...
   While (FileName <> "")
        ' If the last 4 characters of the filename are .nsf then...
        If Right(FileName, 4) = ".nsf" Then
            ' Display a message box of that file name
            MsgBox FileName
            ' Exit the sub early
            Exit Sub
        End If
     FileName = Dir
  Wend
End Sub
 
Upvote 0
Heres another using the Dir method:

Code:
Function GetNSF() As Variant

strFileName = Dir("C:\Users\" & Environ("Username") & "\Local\IBM\Notes\Data\Mail\gdansk\*.nsf")

If Len(strFileName) = 0 Then
    GetNSF = CVErr(xlErrNA)
Else
    GetNSF = strFileName
End If

End Function
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top