2012-01-31 32 views

回答

5

我正在使用這些函數進行文件名處理。最後一個是你需要的一個。

Public Function FilePathOf(ByVal s As String) As String 
    Dim pos As Integer 

    pos = InStrRev(s, "\") 
    If pos = 0 Then 
     FilePathOf = "" 
    Else 
     FilePathOf = Left$(s, pos) 
    End If 
End Function 

Public Function FileNameOf(ByVal s As String) As String 
    Dim pos1 As Integer, pos2 As Integer 

    pos1 = InStrRev(s, "\") + 1 
    pos2 = InStrRev(s, ".") 
    If pos2 = Len(s) Then pos2 = pos2 + 1 
    If pos2 = 0 Then pos2 = Len(s) + 1 
    FileNameOf = Mid$(s, pos1, pos2 - pos1) 
End Function 

Public Function FileExtOf(ByVal s As String) As String 
    Dim pos As Integer 

    pos = InStrRev(s, ".") 
    If pos = 0 Then 
     FileExtOf = "" 
    Else 
     FileExtOf = Mid$(s, pos + 1) 
    End If 
End Function 

Public Function FileNameExtOf(ByVal s As String) As String 
    FileNameExtOf = Mid$(s, InStrRev(s, "\") + 1) 
End Function 
+1

+1,但我更願意使用「\」作爲分隔符的分割功能。 – JimmyPena 2012-01-31 21:24:19

+1

爲什麼要重新發明輪子並寫下大量的樣板代碼?只需使用現有的FileSystemObject ... – 2012-02-01 07:53:11

+1

FileSystemObject位於外部DLL中。這可能會導致問題,如果您的開發環境版本與客戶環境中的版本不同,那麼請參閱該版本。您可以使用後期綁定,但無法再享受智能感知。 – 2012-02-01 13:22:45

8

爲什麼重新發明輪子並寫下大量的樣板代碼?只需使用現有FileSystemObject的用GetFileName方法,已經編寫和測試和調試你:

filename = FSO.GetFileName(path) 

這裏有一個工作示例:

Dim path As String 
Dim filename As String 
Dim FSO As Scripting.FileSystemObject 
Set FSO = New FileSystemObject 

path = "C:\mydir\myotherdir\myfile.txt" 

filename = FSO.GetFileName(path) 'Bingo. Done. 

Debug.Print filename ' returns "myfile.txt" 

' Other features: 
Debug.Print FSO.GetBaseName(path) ' myfile 
Debug.Print FSO.GetExtensionName(path) ' txt 
Debug.Print FSO.GetParentFolderName(path) ' C:\mydir\myotherdir 
Debug.Print FSO.GetDriveName(path) ' C: 
' et cetera, et cetera. 

您將需要設置一個參考如下: 工具> References ...>在Microsoft Scripting Runtime旁邊設置複選標記。

否則使用後期綁定:

Dim FSO As Object 
Set FSO = CreateObject("Scripting.FileSystemObject") 
+0

好點。不知道FileSystemObject類。感謝您指出了這一點。 – stanigator 2012-02-01 19:00:13

+0

是的,爲什麼添加一個2行函數來解析一個字符串,當你可以導入整個類時,並使用OOP ...來解析一個字符串... – 2013-11-20 14:34:47

+1

因爲它已經被寫入並且已經被調試過了,並且沒有下行使用它(除非有人能給我一個實際的具體例子)。你聽起來像OOP是神祕的,但真正使用'FileSystemObject'是非常標準的東西。 – 2013-11-21 07:51:12

1

激活有問題的文件,然後:

Function getname() 

arr = Split(ActiveDocument.FullName, "\") 
Debug.Print arr(UBound(arr)) 

End Function 

我假設你使用Word,因而有 「的ActiveDocument」。在適當情況下將其更改爲「ActiveWorksheet」等

0

'更簡單永遠是更好的! (替代適用小區位置R1C1路徑和字符串長度)

Dim TheFile As String 
Dim TheFileLessPath As String 

Function getname() 
Workbooks.Open filename:=TheFile 
TheFileLessPath = Mid(TheFile, 12, 7) 

ActiveCell.FormulaR1C1 = TheFileLessPath 
End Function 
0

在這種情況下,您正在使用Application.GetOpenFilename(),所以你一定該文件在磁盤上的物理存在,所以最簡單的方法將是使用Dir()。

fileName = Dir(filePath) 

的完整代碼:

Dim fileName, filePath As Variant 

filePath = Application.GetOpenFilename("Excel files (*.xlsm), *.xlsm", , "Select desired file", , False) 

If filePath = False Then 
    MsgBox "No file selected.", vbExclamation, "Sorry!" 
    Exit Sub 
Else 

    'Remove path from full filename 
    fileName = Dir(filePath) 

    'Print file name (with extension) 
    MsgBox "File selected." & vbCr & vbCr & fileName, vbInformation, "Sucess!" 

End If 
相關問題