如何从 VBA 中的文件对话框对象中获取单个文件名(对于 MS Access 2007)?

发布于 2024-08-19 16:55:03 字数 737 浏览 3 评论 0原文

如何更改代码以获取文件名而不是目录名? openDialog.InitialFilename 给我目录名称。
openDialog.FileName 给我错误“找不到方法或数据成员”。

Private Sub btnEditPhoto_Click()
    If (txtImageName > "") Then

        Application.FollowHyperlink txtImageName

    Else
        Dim openDialog As Office.FileDialog

        Set openDialog = Application.FileDialog(msoFileDialogFilePicker)

            openDialog.Filters.Clear
            openDialog.Filters.Add "JPEG Files", "*.jpg"

        Dim pickedFile As Boolean
            pickedFile = openDialog.Show

        If pickedFile Then
                txtImageName.SetFocus
                txtImageName.Text = openDialog.InitialFileName
        End If

    End If

End Sub

How do I change my code to get the file name instead of the directory name? openDialog.InitialFilename gives me the directory name.

openDialog.FileName gives me the error "Method or data member not found".

Private Sub btnEditPhoto_Click()
    If (txtImageName > "") Then

        Application.FollowHyperlink txtImageName

    Else
        Dim openDialog As Office.FileDialog

        Set openDialog = Application.FileDialog(msoFileDialogFilePicker)

            openDialog.Filters.Clear
            openDialog.Filters.Add "JPEG Files", "*.jpg"

        Dim pickedFile As Boolean
            pickedFile = openDialog.Show

        If pickedFile Then
                txtImageName.SetFocus
                txtImageName.Text = openDialog.InitialFileName
        End If

    End If

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧故 2024-08-26 16:55:03

您想要:

OpenDialog.SelectedItems.Item(1)

代替:

OpenDialog.InitialFileName

因为您不允许多重选择。


所以:

''Reference Microsoft Office x.x Object Library
Dim openDialog As Office.FileDialog

Set openDialog = Application.FileDialog(msoFileDialogFilePicker)
openDialog.Filters.Clear
openDialog.Filters.Add "JPEG Files", "*.jpg"

If openDialog.Show Then
    ''SelectedItems is not zero based

    ''Do not use .Text property in MS Access except
    ''in special cases, then you will not have to set focus
    ''txtImageName.SetFocus

    txtImageName = openDialog.SelectedItems(1)
End If

如果使用AllowMultiSelect,则需要迭代SelectedItems

''Reference Microsoft Office x.x Object Library
Dim openDialog As Office.FileDialog
Dim i As Integer

Set openDialog = Application.FileDialog(msoFileDialogFilePicker)
'Use ctl or shift + click to select more than one file
openDialog.AllowMultiSelect = True
openDialog.Filters.Clear
openDialog.Filters.Add "JPEG Files", "*.jpg"

If openDialog.Show Then
    For i = 1 To openDialog.SelectedItems.Count
        Imagelst = Imagelst & ";" & openDialog.SelectedItems(i)
    Next
End If

You want:

OpenDialog.SelectedItems.Item(1)

In place of:

OpenDialog.InitialFileName

As you have not allowed multiselect.


So:

''Reference Microsoft Office x.x Object Library
Dim openDialog As Office.FileDialog

Set openDialog = Application.FileDialog(msoFileDialogFilePicker)
openDialog.Filters.Clear
openDialog.Filters.Add "JPEG Files", "*.jpg"

If openDialog.Show Then
    ''SelectedItems is not zero based

    ''Do not use .Text property in MS Access except
    ''in special cases, then you will not have to set focus
    ''txtImageName.SetFocus

    txtImageName = openDialog.SelectedItems(1)
End If

If AllowMultiSelect is used, you need to iterate through SelectedItems

''Reference Microsoft Office x.x Object Library
Dim openDialog As Office.FileDialog
Dim i As Integer

Set openDialog = Application.FileDialog(msoFileDialogFilePicker)
'Use ctl or shift + click to select more than one file
openDialog.AllowMultiSelect = True
openDialog.Filters.Clear
openDialog.Filters.Add "JPEG Files", "*.jpg"

If openDialog.Show Then
    For i = 1 To openDialog.SelectedItems.Count
        Imagelst = Imagelst & ";" & openDialog.SelectedItems(i)
    Next
End If
死开点丶别碍眼 2024-08-26 16:55:03

我需要选择一个文本文件...这就是我所做的...它工作得很好。

' Get the File
'----------------------------------------------------------
Dim dialog As Object
Dim pickedfile As Boolean
Dim myfile As String
Set dialog = Application.FileDialog(msoFileDialogFilePicker)
With dialog
    .AllowMultiSelect = False
    .Title = "Please pick the file to convert."
    .Filters.Clear
    .Filters.Add "Text Files", "*.TXT"
    .Filters.Add "All Files", "*.*"
    pickedfile = False
    pickedfile = .Show
    If pickedfile Then
    myfile = .SelectedItems.Item(1)
    End If
End With
'----------------------------------------------------------

另外...您可以将对话框类型替换为...

Set dialog = Application.FileDialog(msoFileDialogOpen)

并且它同样有效。

I needed to select a single text file... this is what I did... it worked fine.

' Get the File
'----------------------------------------------------------
Dim dialog As Object
Dim pickedfile As Boolean
Dim myfile As String
Set dialog = Application.FileDialog(msoFileDialogFilePicker)
With dialog
    .AllowMultiSelect = False
    .Title = "Please pick the file to convert."
    .Filters.Clear
    .Filters.Add "Text Files", "*.TXT"
    .Filters.Add "All Files", "*.*"
    pickedfile = False
    pickedfile = .Show
    If pickedfile Then
    myfile = .SelectedItems.Item(1)
    End If
End With
'----------------------------------------------------------

Additionally... you can replace the dialog type with...

Set dialog = Application.FileDialog(msoFileDialogOpen)

and it worked equally well.

微凉徒眸意 2024-08-26 16:55:03
Private Sub Command135_Click()

Dim dialog As Object
Dim pickedfile As Boolean
Dim myfile As String
Set dialog = Application.FileDialog(1)
With dialog
    .AllowMultiSelect = False
    .Title = "Please pick the file to convert."
    .Filters.Clear
    .Filters.Add "Picture Files", "*.Jpg"
    .Filters.Add "All Files", "*.*"
    pickedfile = False
    pickedfile = .Show
    If pickedfile Then
    myfile = .SelectedItems.Item(1)
    End If
End With

Me.Form.Picture = myfile
End Sub


Command_135=Button Name
Me.Form.Picture = "The Control Name"
Private Sub Command135_Click()

Dim dialog As Object
Dim pickedfile As Boolean
Dim myfile As String
Set dialog = Application.FileDialog(1)
With dialog
    .AllowMultiSelect = False
    .Title = "Please pick the file to convert."
    .Filters.Clear
    .Filters.Add "Picture Files", "*.Jpg"
    .Filters.Add "All Files", "*.*"
    pickedfile = False
    pickedfile = .Show
    If pickedfile Then
    myfile = .SelectedItems.Item(1)
    End If
End With

Me.Form.Picture = myfile
End Sub


Command_135=Button Name
Me.Form.Picture = "The Control Name"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文