Excel:VBA 中的文件打开对话框设置为详细信息视图并按修改日期排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5500780/
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-11 12:56:09  来源:igfitidea点击:

Excel : File Open Dialog in VBA that is set to Detail View and sorted by Date Modified

windowsexcelvbadialog

提问by FinancialRadDeveloper

I have tried to Google for this quite extensively but I hope someone here can come up trumps.

我已经尝试在谷歌上进行了相当广泛的搜索,但我希望这里有人能胜出。

As a lot of people ( I imagine ) would like, I want to spawn a File Open dialog box from Excel ( no problem with that using GetOpenFilename ).

正如很多人(我想)想要的那样,我想从 Excel 生成一个文件打开对话框(使用 GetOpenFilename 没问题)。

I think I am trying to achieve the same thing most people want when they open up a dialog, they want to choose a file, and normally this is the latest one of that type?

我想我正在努力实现大多数人在打开对话框时想要的东西,他们想要选择一个文件,通常这是该类型的最新版本?

So say I have a C:\Temp dir full of .piv files and I want to grab the last created. I have to open the dialog, change to details and then click the Date Modified column and then this will be the view and set up I want.

所以说我有一个充满 .piv 文件的 C:\Temp 目录,我想获取最后创建的文件。我必须打开对话框,更改为详细信息,然后单击修改日期列,然后这将是我想要的视图和设置。

I have to do this EVERY TIME, as do the users. Is there any way of setting the sort and the view type from Excel VBA.

我每次都必须这样做,用户也是如此。有没有办法从 Excel VBA 设置排序和视图类型。

The Chapter 9. stuff from Professional Excel Development gives me some hope, but doesn't get me all the way there.

第 9 章来自 Professional Excel Development 的内容给了我一些希望,但并没有让我一路走好。

So strange seeing as it must be a common thing for people to want to do?

看到如此奇怪的东西,人们想要做的事情一定是一件很平常的事情吗?

采纳答案by FinancialRadDeveloper

With my OS and office version (XP with Office 2007) as far as I can tell, it is not possible.

据我所知,使用我的操作系统和办公版本(XP 和 Office 2007),这是不可能的。

回答by David Heffernan

There's no official way to do this. There are hacks but they are very hard to make work across a wide variety of Windows machines. Getting such hacks to work in VBA sounds exceedingly challenging.

没有官方的方法可以做到这一点。有一些黑客,但它们很难在各种 Windows 机器上工作。让这样的黑客在 VBA 中工作听起来非常具有挑战性。

The new file dialogs introduced in Vista remember settings like list view mode, which column was sorted and so on. I think Microsoft took note of the frustrations people had with the older versions when they designed the new versions.

Vista 中引入的新文件对话框会记住列表视图模式、已排序的列等设置。我认为微软在设计新版本时注意到了人们对旧版本的不满。

回答by Jean-Fran?ois Corbett

Dude, you gotta get with FileDialog! This object offers way more flexibility than GetOpenFilename(and its sibling GetSaveAsFilename).

伙计,你必须得到FileDialog!这个对象提供了比GetOpenFilename(及其兄弟GetSaveAsFilename)更多的灵活性。

It will at least get you into Details view automatically. Example:

它至少会让您自动进入详细信息视图。例子:

Dim fdgOpen As FileDialog
Set fdgOpen = Application.FileDialog(msoFileDialogOpen)
With fdgOpen
    .Title = "Please open a PIV file..."
    .InitialFileName = "C:\MyDocuments\MyDir\"
    .InitialView = msoFileDialogViewDetails ' Aha!!!
    .Show
End With

The above will save you the two clicks required to get to Details view. I don't know whether the last click on "Date modified" can be automated, but hey, at least you're 2/3 of the way.

以上将为您节省进入详细信息视图所需的两次点击。我不知道最后一次点击“修改日期”是否可以自动化,但是嘿,至少你已经完成了 2/3。

See VBA helpon FileDialog. It has quite a few useful properties.

VBA的帮助FileDialog。它有很多有用的属性。

回答by Bill

For XP and Office 2010, msoFileDialogViewDetailsworks.

对于 XP 和 Office 2010,msoFileDialogViewDetails有效。