【问题标题】:Open a pdf document from selected value in combobox - Userform从组合框中的选定值打开 pdf 文档 - 用户表单
【发布时间】:2018-05-05 01:12:39
【问题描述】:

我正在为我的情况寻找解决方案。

我正在为我的部门制作宏,但我卡住了。

我有 6 种不同的职业道路,我想将 pdf 文档链接到组合框中的值。这就是它的样子。我有一个组合框,如下图所示:

组合框的数据在隐藏的其他工作表中:

我想要做的是,如果组合框中的值是例如“实践中的自信”,那么在单击命令按钮“读取”后,我想打开位于B1 列中的 pdf 文件。

有什么办法吗?

【问题讨论】:

    标签: excel vba pdf combobox userform


    【解决方案1】:

    是的,您可以打开该文件...以下是一个如何执行此操作的示例(加上一个用于测试代码的子程序)。只需按如下所述更改代码即可。

    修改现有按钮 单击以添加 'OpenFile 行,如图所示。 传递完整路径和文件名。 我假设您的组合框将路径作为组合框中的列?如果 不是,您应该将其添加为隐藏列

     Private Sub cmdRead_Click()
            ' whatever code you have or need
            OpenFile Me.cboXXXX.Column(1)     ' Change to the name of your combobox AND column # of Full Path
            ' whatever other code you want
        End Sub
    
    
    '---- Create a new module and paste all of the following code in it
    
    Option Explicit
    Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Public Function OpenFile(sFileName As String)
    On Error GoTo Err_OpenFile
    
        OpenFile = ShellExecute(Application.hWndAccessApp, "Open", sFileName, "", "C:\", 1)
    
    Exit_OpenFile:
        Exit Function
    
    Err_OpenFile:
        MsgBox Err.Number & " - " & Err.Description
        Resume Exit_OpenFile
    
    End Function
    
    Public Function TestOpeningFile()
    On Error GoTo Err_TestOpeningFile
    
        OpenFile "C:\Windows\Win.ini"
    
    Exit_TestOpeningFile:
        Exit Function
    
    Err_TestOpeningFile:
        MsgBox Err.Number & " - " & Err.Description
        Resume Exit_TestOpeningFile
    
    End Function
    

    【讨论】:

    • 好的,谢谢你的信息。我没有看到命令按钮的任何行,所以我应该将上面的代码分配给命令按钮?还是我应该只插入用户表单?
    • 我会在一分钟内用一个“通用调用”更新答案,你需要更改控件的名称。这样就行了……
    • 无论如何我可以在网站上上传我的宏吗?我认为看到正在发生的事情会容易得多
    • 它是“宏”还是 VBA 代码?如果是 VBA,则只需复制和粘贴。如果是宏,并且很小,您可以丝网打印或将其转换为 VBA 代码,然后复制/粘贴。要进行转换,请右键单击宏,选择“设计视图”。然后查看左侧的功能区,您应该会看到“将 Macrso 转换为 Visual Basic”单击该按钮,然后复制/粘贴代码。
    • 很高兴它正在工作。代码有帮助还是您采取了不同的方法?
    【解决方案2】:
     Sub Training()
    
    Call Workbook_Open
    
    End Sub
    
    Private Sub Workbook_Open() 'open the msgbox with the message and username.
    MsgBox "            " & ("Welcome") & vbCrLf & vbCrLf & "        " & Application.UserName
    
    Userform1.Show
    
    End Sub
    
    
    
    In Userform I got:
    
    
        Private Sub Analytical_Button_Click()
    
    Analytical_userform.ComboBox1.Clear
    
    For Each cell In Range(ThisWorkbook.Worksheets("Analytical").Range("A1"), ThisWorkbook.Worksheets("Analytical").Range("A200"))
        If cell.Value <> "" Then Analytical_userform.ComboBox1.AddItem cell.Value
    Next cell
    
    Analytical_userform.Show
    
    
    End Sub
    
    
    Private Sub Client_Button_Click()
    
    Client_userform.ComboBox1.Clear
    
    For Each cell In Range(ThisWorkbook.Worksheets("Client").Range("A1"), ThisWorkbook.Worksheets("Client").Range("A200"))
        If cell.Value <> "" Then Client_userform.ComboBox1.AddItem cell.Value
    Next cell
    
    Client_userform.Show
    
    
    End Sub
    
    Private Sub Expert_Button_Click()
    
    Expert_userform.ComboBox1.Clear
    
    For Each cell In Range(ThisWorkbook.Worksheets("Expert").Range("A1"), ThisWorkbook.Worksheets("Expert").Range("A200"))
        If cell.Value <> "" Then Expert_userform.ComboBox1.AddItem cell.Value
    Next cell
    
    Expert_userform.Show
    
    End Sub
    
    
    Private Sub Managerial_Button_Click()
    
    Managerial_userform.ComboBox1.Clear
    
    For Each cell In Range(ThisWorkbook.Worksheets("Managerial").Range("A1"), ThisWorkbook.Worksheets("Managerial").Range("A200"))
        If cell.Value <> "" Then Managerial_userform.ComboBox1.AddItem cell.Value
    Next cell
    
    Managerial_userform.Show
    
    End Sub
    
    Private Sub Project_Button_Click()
    
    Project_Userform.ComboBox1.Clear
    
    For Each cell In Range(ThisWorkbook.Worksheets("Project").Range("A1"), ThisWorkbook.Worksheets("Project").Range("A200"))
        If cell.Value <> "" Then Project_Userform.ComboBox1.AddItem cell.Value
    Next cell
    
    Project_Userform.Show
    
    End Sub
    
    Private Sub Trainer_Button_Click()
    
    Trainer_userform.ComboBox1.Clear
    
    For Each cell In Range(ThisWorkbook.Worksheets("Trainer").Range("A1"), ThisWorkbook.Worksheets("Trainer").Range("A200"))
        If cell.Value <> "" Then Trainer_userform.ComboBox1.AddItem cell.Value
    Next cell
    
    Trainer_userform.Show
    
    End Sub
    
    Private Sub UserForm_Click()
    
    End Sub
    

    请检查快照:

    screenshot screenshot1

    我想通过点击“阅读”按钮打开培训

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多