如何检查是否有一个对象已经在Excel表,如果没有,然后插入文件?

这段代码打开select文件窗口,然后将文件包含到工作表中。 问题是你可以做多less次没有限制。 我如何执行检查并阻止它包含多个文件?

Public Sub insertFile() 'Select the cell in which you want to place the attachment Range("F92").Select 'Get file path fpath = Application.GetOpenFilename("All Files,*.*", Title:="Select file") If LCase(fpath) = "false" Then Exit Sub 'Insert file ActiveSheet.OLEObjects.Add _ Filename:=fpath, _ Link:=False, _ DisplayAsIcon:=True, _ IconFileName:="excel.exe", _ IconIndex:=0, _ IconLabel:=extractFileName(fpath) End Sub 

 Public Function extractFileName(filePath) For i = Len(filePath) To 1 Step -1 If Mid(filePath, i, 1) = "\" Then extractFileName = Mid(filePath, i + 1, Len(filePath) - i + 1) Exit Function End If Next End Function 

如何简单

 If ActiveSheet.OLEObjects.Count > 0 Then Exit Sub 

在你的sub开始?