在 Excel 2010 VBA 中刷新数据连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20378363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:48:29  来源:igfitidea点击:

Refreshing Data Connections in Excel 2010 VBA

excelvbaexcel-vba

提问by Adjit

So I have all of my VBA code down and it seems to be right, but it just is not refreshing the workbooks. The only thing that I can think of is when I open up the sheets I have to enable editting, so would I need to add something to my code?

所以我把我所有的 VBA 代码都写下来了,它似乎是对的,但它只是没有刷新工作簿。我唯一能想到的是当我打开工作表时我必须启用编辑,所以我需要在我的代码中添加一些东西吗?

Edit: I am running this through a VBScript

编辑:我正在通过 VBScript 运行它

Public wb As Workbook

Sub executeUpdate()
    `some code
     openBook path & testArray(i) & ext, True
     saveBookAs path & testArray(i)
End Sub

Sub openBook(ByVal fileName As String, ByVal refresh As Boolean)

    Set wb = Workbooks.Open(fileName, 0, False)

    If refresh = True Then
        wb.RefreshAll
    End If
End Sub

Sub saveBookAs(ByVal fName As String)
    wb.SaveAs fileName:=fName & "_posReport.xlsx"
End Sub

回答by Michael

I found this code over here:

我在这里找到了这段代码:

If Application.ProtectedViewWindows.Count > 0 Then
    Application.ActiveProtectedViewWindow.Edit
End If

Have not tested it, but maybe it works for you.

还没有测试过,但也许它适合你。

-- Quote:

-- 引用:

code will bypass the need for the user to select the "Enable Editing" button on the message bar below the ribbon. One additional note [...] the code will error if the workbook isn't in "ProtectedView". So to avoid this the following code will only enable editing if the workbook has "ProtectedView" enabled.

代码将绕过用户选择功能区下方消息栏上的“启用编辑”按钮的需要。另一个注意事项 [...] 如果工作簿不在“ProtectedView”中,代码​​将出错。因此,为避免这种情况,以下代码仅在工作簿启用了“ProtectedView”时才启用编辑。