i'm new in VBA, so i dont know how to remove the file extension of the object

Viewed 107

how can i removing the file extension of the object i input, so the "A" column just showing the file name without the extension.

Sub AddOlEObject()
Dim mainWorkBook As Workbook

Set mainWorkBook = ActiveWorkbook
Sheets("Object").Activate
Folderpath = "C:\Users\Sumit Jain\Pictures"
Set fso = CreateObject("Scripting.FileSystemObject")
NoOfFiles = fso.GetFolder(Folderpath).Files.Count
Set listfiles = fso.GetFolder(Folderpath).Files
For Each fls In listfiles
    strCompFilePath = Folderpath & "\" & Trim(fls.Name)
    If strCompFilePath <> "" Then
        If (InStr(1, strCompFilePath, "jpg", vbTextCompare) > 1 _
        Or InStr(1, strCompFilePath, "jpeg", vbTextCompare) > 1 _
        Or InStr(1, strCompFilePath, "png", vbTextCompare) > 1) Then
        counter = counter + 1
        Sheets("Object").Range("A" & counter).Value = fls.Name
        Sheets("Object").Range("B" & counter).ColumnWidth = 25
        Sheets("Object").Range("B" & counter).RowHeight = 100
        Sheets("Object").Range("B" & counter).Activate
        Call insert(strCompFilePath, counter)
        Sheets("Object").Activate
    End If
End If
Next
mainWorkBook.Save
End Sub

Function insert(PicPath, counter)
‘MsgBox PicPath
With ActiveSheet.Pictures.insert(PicPath)
With .ShapeRange
    .LockAspectRatio = msoTrue
    .Width = 50
    .Height = 70
End With
.Left = ActiveSheet.Range("B" & counter).Left
.Top = ActiveSheet.Range("B" & counter).Top
.Placement = 1
.PrintObject = True
End With
End Function

thank you for the helping (sorry for my bad english)

4 Answers

Use the InStrRev function which returns the position of an occurrence of one string within another, from the end of the string. So we look for . from the right of the string and then get the file name without extension.

Is this what you want?

Sheets("Object").Range("A" & counter).Value = _
Left(fls.Name, (InStrRev(fls.Name, ".", -1, vbTextCompare) - 1))

As you already use filesystem object you could also use the GetBaseName method.

Sheets("Object").Range("A" & counter).Value = fso.getbasename(fls.name)

This function will return the file name without path and without extension.

Function NettFileName(ByVal Fn As String) As String

    Dim Sp()    As String
    
    Sp = Split(Fn, ".")
    ' cut off the extension
    If UBound(Sp) Then ReDim Preserve Sp(UBound(Sp) - 1)
    Fn = Join(Sp, ".")                 ' file name without extension
    
    ' remove the path
    Sp = Split(Fn, "\")
    NettFileName = Sp(UBound(Sp))
End Function

It works by dividing the file name given as an argument into portions separated by periods and then dropping the last one. This method is more reliable than using the Instr() function because there might be periods in the file name that don't mean that the extension follows. That logic only applies to the last period in the name.

You might change the line Fn = Join(Sp, ".") to NettFileName = Join(Sp, ".") and end the function there. I let it continue for another two lines to drop the path as well if it was included in the file name. A similar system is deployed here: The nett file name follows the last back-slash.

The function returns the nett file name also if there was no path and/or no extension to begin with (such as when you have a workbook that was never saved). And this is how you might call the function:-

Sub GetFileName()

    Dim Fn      As String

    ' create a name for testing
    With ActiveWorkbook
        Fn = .Path & "\" & .Name
    End With
    
    MsgBox NettFileName(Fn)
End Sub

This procedure was written for testing and you can use it for that purpose. When you deploy the function in your code you would just use the line NettFileName(Fn) in the place where you want the nett of Fn.

Siddharth answered your question correctly so I will just address things I saw in other parts of your code. Since you are new to VBA this is not intended to be harsh but rather helpful guidance for you as you continue to learn.

Sheets("Object").Activate

As a general rule you do not want to obtain a reference to a worksheet object using the name that is displayed on the WB tab in Excel, because your code will break if you decide to rename it there. Every worksheet object has a CodeName that is distinct from this displayed name; if you look at the list of objects in the VBE it is the string of text outside of the parentheses before the display name. I would imagine you see something like Sheet1(Object). Use this to refer to the sheet instead, either directly Sheet1.Range("A1") or by assigning to a variable:

Dim wS as Worksheet
Set wS = Sheet1
wS.Range("A1")

Also note that you do not need to .Activate worksheets to perform work on them. Looking at your code the only reason you appear to be calling this is so that you can refer to that sheet as ActiveSheet in the other routine. If the sheet is always the same you can simply directly refer to it using the CodeName and if not you can pass the variable reference wS as a parameter instead.

Function insert(PicPath, counter)

This does not appear to return any value, so it is better declared as Sub than Function. Also it is good practice to put the variable types in the declaration like so (PicPath As String, count as Integer).

Let me know if you need any clarification. :)

Related