FileDialog不起作用 [英] FileDialog doesn't work

查看:452
本文介绍了FileDialog不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看上去非常紧张,但是找不到直接解决我的问题的帖子.

I've looked quite intensely, but couldn't find a post that directly solves my problem.

下面为我创建的表单编写的代码在Access 2003中可用,我在工作中使用

The following code for a form I created works in Access 2003, which I use at work.

Dim FileName As FileDialog
Set FileName = Application.FileDialog(msoFileDialogFilePicker)
Dim Name As Variant

With FileName
    .AllowMultiSelect = False
    .Show
    If .SelectedItems.Count = 0 Then
    MsgBox "No file selected."
    Exit Sub
    End If
End With

For Each Name In FileName.SelectedItems
FileNameTextBox.Text = Mid$(Name, InStrRev(Name, "\") + 1)
Next Name

但是,当我尝试在个人计算机上的Access 2010中的表单上运行相同的代码时,它不起作用.错误消息突出显示了第一行并显示未定义用户定义的类型".我也尝试声明FileName as Office.FileDialog,但是也没有运气.我确实将Microsoft Access 14.0对象库作为使用中的引用之一,所以我不知道这是怎么回事.

However, when I tried to run the same code on a form in Access 2010 on my personal computer, it doesn't work.The error message highlights the first line and says "User-defined type not defined." I also tried declaring FileName as Office.FileDialog, but no luck either. I do have Microsoft Access 14.0 Object Library as one of the references in use, so I don't know what's wrong with that.

我只使用Access两周了,我的全部知识都来自于谷歌搜索,所以很可能我遗漏了一些明显的东西.

I've only been using Access for two weeks, and all my knowledge come from googling, so it's very likely that I'm missing something obvious.

推荐答案

FileDialog对象不是由Access库提供的,而是由Office库提供的.因此,如果您设置对 Microsoft Office [版本号]对象库的引用,则您的代码应该可以工作.您没有该参考集,或者它已损坏.

The FileDialog object is not provided by the Access library, but by the Office library. So your code should work if you set a reference to the Microsoft Office [version number] Object Library. Either you don't have that reference set, or it's broken.

但是,如果是我,我将保留未设置的引用并像这样修改代码.看看它是否对您有用.

However if it were me, I would leave the reference unset and modify the code like this. See if it works for you.

Const msoFileDialogFilePicker As Long = 3
Dim objDialog As Object

Set objDialog = Application.FileDialog(msoFileDialogFilePicker)

With objDialog
    .AllowMultiSelect = False
    .Show
    If .SelectedItems.Count = 0 Then
        MsgBox "No file selected."
    Else
        Me.FileNameTextBox.Value = Dir(.SelectedItems(1))
    End If
End With

这篇关于FileDialog不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆