【问题标题】:Remove Path from the File Name of a Picture in VBA从 VBA 中图片的文件名中删除路径
【发布时间】:2012-03-07 21:49:48
【问题描述】:

我知道他们已经有很多与此类似的问题,但他们似乎都没有给我想要的答案。 我有一个要点要说明我正在尝试自动化,以便 VBA 会自动为我插入图片。我使用GetOpenFileName 允许用户选择文件,但由于这仅在 excel 中受支持,我已将 excel 添加到引用列表中,因此实际命令是 Excel.Application.GetOpenFileName()

因此我的 Sub 显示为

Sub Image()
Dim Pict
Dim ImgFileFormat As String
ImgFileFormat = "Image Files (*.jpg; *.jpeg; *.png; *.bmp; *.gif; *.tif; *.tiff ), *.jpg; *.jpeg; *.png; *.bmp; *.gif; *.tif; *.tiff"
Pict = Excel.Application.GetOpenFilename(ImgFileFormat)
If Pict = False Then End
End Sub

我想要做的是从文件路径(Pict 变量)中提取文件名。

任何帮助将不胜感激。

【问题讨论】:

    标签: vba


    【解决方案1】:
    Pict = Dir(Pict)
    

    应该只给你文件名。

    【讨论】:

    • 甜蜜!删除路径并仅保留文件名的好方法。谢谢!
    • 感谢这两个遮阳篷,但对我来说越简单越好
    • 注意事项:这是有效的,因为您知道该文件确实存在。如果您有路径 + 文件名,但不知道它是否存在于文件系统中,安德鲁的答案更通用并且效果更好。
    • 你可以像这样捕获不存在的文件,也许吧? If Len(Dir(Pict)) > 0 Then Pict = Dir(Pict)
    【解决方案2】:

    也许这会对你有所帮助:

    Function FunctionGetFileName(FullPath As String)
    Dim StrFind As String
        Do Until Left(StrFind, 1) = "\"
            iCount = iCount + 1
            StrFind = Right(FullPath, iCount)
                If iCount = Len(FullPath) Then Exit Do
        Loop
        FunctionGetFileName = Right(StrFind, Len(StrFind) - 1)
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      • 2019-08-18
      • 1970-01-01
      • 2014-03-16
      • 2015-08-17
      • 1970-01-01
      相关资源
      最近更新 更多