0

表单输入 spredhseet我已经创建了 excel 表单,其中分配了一个命令按钮来上传 Excel 电子表格中的文件。

这段代码的问题是,我用它来点击按钮,现在我想用它来点击命令按钮。 Excel 表单 在下面的代码中,路径没有定义为从活动范围中选择,其中要求是根据 excel 表单条目固定的 excel 路径。

Private Sub CommandButton1_Click()

    Worksheets("DataBase").Range("L3").Select
    Dim fd As FileDialog
    Dim selectedFile As String
    Dim docObj As OLEObject
    Dim destCell As Range
    
    
    If IsError(Application.Caller) Then
        MsgBox "The Insert_Document macro must be called from a button click, not directly.", vbCritical
        Exit Sub
    End If
    
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .AllowMultiSelect = False
        .Title = "Select document to insert"
        .Filters.Clear
        
        .Filters.Add "excel documents", "*.xlsx"
        .Filters.Add "All files", "*.*"
        If Not .Show Then Exit Sub
        selectedFile = .SelectedItems(1)
    End With
    
    Set destCell = ActiveSheet.Buttons(Application.Caller).TopLeftCell.Offset(, -1)   'This is where i am getting error
    
    Set docObj = ActiveSheet.OLEObjects.Add(Filename:=selectedFile, Link:=False, DisplayAsIcon:=False)
    With docObj
        .Name = Left(Mid(selectedFile, InStrRev(selectedFile, "\") + 1), 32)
        .Top = destCell.Top
        .Left = destCell.Left
        .Width = destCell.Width
        .Height = destCell.Height
    End With
    
4

0 回答 0