Sagittariu5
New Member
- Joined
- Oct 30, 2013
- Messages
- 5
Hello All,
I'm trying to ascertain why the below macro works 90% on the time and the other 10% doesn't enable you to insert OLE_Object.
Thus hoping someone could point me in the right direction.
Thanks in advance
Kind Regards
Sagittariu5
I'm trying to ascertain why the below macro works 90% on the time and the other 10% doesn't enable you to insert OLE_Object.
Thus hoping someone could point me in the right direction.
Code:
Public Function GetFileNameWithExt(ByVal FullPath As String) As String
Dim fileName As String
Dim fileNameWithExt As String
Dim lastSlash As Integer
Dim positionOfDot As Integer
lastSlash = InStrRev(FullPath, "\")
fileName = Mid(FullPath, lastSlash + 1)
positionOfDot = InStr(1, fileName, ".")
fileNameWithExt = Mid(fileName, 1, positionOfDot + 6)
GetFileNameWithExt = fileNameWithExt
End Function
Code:
Private Sub Insert_OLE_Object()
Dim WSH As Worksheet, FRng As Range, FR As Long
Dim iconToUse, fullFileName, FNExtension As String
Dim r As Long
Dim ObjectList As Integer
'Prompts user to choose which file to Insert
x = Application.GetOpenFilename( _
FileFilter:="All Files (*.*), *.*", Title:="Choose File to Insert", MultiSelect:=False)
'Check in case no files were selected, if so Exit Macro
If x = "False" Then Exit Sub
'If file has been selected, confirms the file name and type (i.e. Statement.pdf)
MyMsg = "You've selected :- " & GetFileNameWithExt(x) & _
vbNewLine & "Do you wish to Continue?"
Response = MsgBox(MyMsg, vbExclamation + vbYesNo, "File Selected")
'Runs one of two groups of statements, depending on the chosen response
Select Case Response
'If 'Yes'
Case Is = vbYes
'Variable ObjectList stores a count of all embedded files.
ObjectList = Sheets("BackUp").OLEObjects.Count
'If there are no attachment/OLEObjects the ensure the 1st row chose is row2 (C2)
If ObjectList = 0 Then r = 1 Else r = 1 + ObjectList
'-------------------------------------------------------------------------------------------------------
'If their are attachment/OLEObjects, then counts the number and choses the last available/empty row
Set WSH = ThisWorkbook.Worksheets("BackUp")
Set FRng = WSH.Range("$A$1")
'Locating the Final Row via Column C
FR = WSH.Columns("C").Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows, LookIn:=xlValues).Row
FRng.Offset(r, 2).Select
'-------------------------------------------------------------------------------------------------------
'Choose an icon based on filename extension get all after last "." in filename
FNExtension = Right(x, Len(x) - InStrRev(x, "."))
'Select icon based on filename extension
Select Case UCase(FNExtension)
Case Is = "PDF"
iconToUse = "C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-AB0000000001}\PDFFile_8.ico"
'Insert the Backup file name on the cell affected
FRng.Offset(r, 2).Value = GetFileNameWithExt(x)
Case Else
'If attached file isn't PDF, informs user and Exits Macro
MsgBox "File '" & GetFileNameWithExt(x) & "' isn't a PDF file!" & _
vbNewLine & "PLEASE check and reattach"
End
End Select
'Insert chosen back file into the current cell
WSH.OLEObjects.Add(fileName:=x, Link:=False, _
DisplayAsIcon:=True, IconFileName:=iconToUse, IconIndex:=0, IconLabel:=GetFileNameWithExt(x)).Activate
'If 'No' Exit Macro
Case Is = vbNo
Exit Sub
End Select
End Sub
Thanks in advance
Kind Regards
Sagittariu5
Last edited: