【问题标题】:Open A File From Dialog Box从对话框打开文件
【发布时间】:2022-01-23 19:38:16
【问题描述】:

我正在尝试自动化流程。

此问题关注的领域是验证活动工作簿是否是我们共享驱动器中正确文件夹中的正确工作簿。否则,用户将保存数据并且不会被共享。确保数据输入的安全措施。

一些用户将上述“活动工作簿”的副本从共享驱动器保存到他们的 PC,这会导致数据丢失。

如今,人们对 IT 缺乏安全感,但又害怕不熟悉的自动化功能。为了迎合这些情况,我想使用一个消息框来提醒用户他们不会将数据保存在组文件中,然后询问他们是否要打开该文件。点击“是”后,将打开另一个文件,同时保持现有文档处于打开状态。选择“否”或“取消”将结束。

目标:

  • 使用消息框提醒用户他们没有处理共享文档。选择后,“是”按钮将打开正确的文档。

代码(在工作簿中):

Private Sub Workbook_Open()

Dim Sheet1 As Worksheet
Set Sheet1 = Sheets("Invoices")
Dim folpath As String
Dim mypath As String

    Application.ScreenUpdating = False
    
    folpath = "K:\Purchasing_Utilities\1_UTILITIES\4_VENDOR_INVOICES\GHOST_CARD\Active_Pay_Tracker_22.xlsm"
    mypath = Application.ActiveWorkbook.FullName
    
    If mypath = folpath Then
        GoTo Skip
    Else
        MsgBox "This file source is a locally saved file. To share changes, please open the Tracker in the K: drive." _
            & " Would you like the system to open this file now?", VbMsgBoxStyle = vbOKCancel + vbCritical
            
            'Here is where I am trying to get the message box to open the document
    

Skip
        Sheet1.Range("P:P").Sort Key1:=Sheet1.Range("P:P"), Order1:=xlAscending, Header:=xlYes
    
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Activate
    
    Application.ScreenUpdating = True

【问题讨论】:

    标签: excel vba automation


    【解决方案1】:

    只是一个例子:

    Result = MsgBox("This file source is a locally saved file. To share changes, please open the Tracker in the K: drive. Would you like the system to open this file now?", vbOKCancel + vbCritical)
    If Result = vbOKCancel Then
    MsgBox "You clicked OK"
    Else: MsgBox "You clicked Critical"
    End If
    

    如下所示:

    如果您按“确定”,您会看到以下内容:

    因此,如果您想要vbYes,则需要将其添加到原始消息框(当前没有“是”按钮)并添加相应的Result 处理(if Result = vbYes then ... (open file))。

    【讨论】:

    • 谢谢多米尼克!您知道是否可以通过 vbYes 选择作为操作打开文件?
    • @Beej:我相应地调整了我的答案。
    猜你喜欢
    • 2010-12-11
    • 1970-01-01
    • 2013-05-12
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多