Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如果目录是桌面路径,则BrowseForFolder返回错误_Excel_Vba - Fatal编程技术网

Excel 如果目录是桌面路径,则BrowseForFolder返回错误

Excel 如果目录是桌面路径,则BrowseForFolder返回错误,excel,vba,Excel,Vba,下面的代码用于允许用户导航目录,如果位置是文件夹,则该代码工作正常,如果输出目录是桌面路径,则该代码失败。调试时,它会显示“PickFolder=f.Items.Item.Path”上的错误。错误消息是运行时错误“91”:未设置对象变量或With block变量。你知道我如何重写代码来解决这个问题吗?谢谢 Public Function PickFolder() As String Dim SA As Object, f As Object Dim OutputPath As String '

下面的代码用于允许用户导航目录,如果位置是文件夹,则该代码工作正常,如果输出目录是桌面路径,则该代码失败。调试时,它会显示“PickFolder=f.Items.Item.Path”上的错误。错误消息是运行时错误“91”:未设置对象变量或With block变量。你知道我如何重写代码来解决这个问题吗?谢谢

Public Function PickFolder() As String
Dim SA As Object, f As Object
Dim OutputPath As String

'Ensure user has enter business date value before process PickFolder function
If BDTextBox.Text <> "" Then
Set SA = CreateObject("Shell.Application")
Set f = SA.BrowseForFolder(0, "Choose a folder", _
     16 + 32 + 64)
If (Not f Is Nothing) Then
PickFolder = f.Items.Item.Path
OutputPath = PickFolder

FinalFileName = ActiveWorkbook.FullName
'InStrRev will find the last occurrence of a character in a string. Search for \ and split it     there
 FinalFileName = Mid(FinalFileName, InStrRev(FinalFileName, "\") + 1)
'Take off the extension
FinalFileName = Left(FinalFileName, InStrRev(FinalFileName, ".") - 1)
 FinalFileName = FinalFileName

WriteTextBox = OutputPath & "\" & FinalFileName & "_" & FinalBusinessDate
'MsgBox "value is " & FinalFileName & "_" & FinalBusinessDate
 End If
 Set f = Nothing
 Set SA = Nothing

 Else
 MsgBox "Unable to process. Please ensure Business Date was entered.", vbCritical
 End If
 End Function
公共函数PickFolder()作为字符串
将SA设置为对象,将f设置为对象
将输出路径设置为字符串
'确保用户在处理PickFolder功能之前输入了业务日期值
如果是BDTextBox.Text“”,则
Set SA=CreateObject(“Shell.Application”)
设置f=SA.BrowseForFolder(0,“选择文件夹”_
16 + 32 + 64)
如果(不是f什么都不是)那么
PickFolder=f.Items.Item.Path
OutputPath=PickFolder
FinalFileName=ActiveWorkbook.FullName
'InStrRev将查找字符串中最后出现的字符。搜索\并将其拆分到那里
FinalFileName=Mid(FinalFileName,InStrRev(FinalFileName,“\”)+1)
“把分机拿下来
FinalFileName=左(FinalFileName,InStrRev(FinalFileName,“.”)1)
FinalFileName=FinalFileName
WriteTextBox=OutputPath&“\”和FinalFileName&“\”和FinalBusinessDate
“MsgBox”的值为“&FinalFileName&”&FinalBusinessDate”
如果结束
设置f=无
设置SA=无
其他的
MsgBox“无法处理。请确保输入了业务日期”。vbCritical
如果结束
端函数

尝试将您的逻辑嵌入以下内容:

Function GetFolder() As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function
函数GetFolder()作为字符串 Dim fldr As FILE对话框 以字符串形式显示 设置fldr=Application.FileDialog(msoFileDialogFolderPicker) 与fldr .Title=“选择一个文件夹” .AllowMultiSelect=False .InitialFileName=Application.DefaultFilePath 如果.Show-1,则转到下一个代码 sItem=.SelectedItems(1) 以 下一个代码: GetFolder=sItem 设置fldr=无 端函数 我的最爱(但不是我的):

子样本() 暗网 将文件夹设置为字符串

InitFolder = "C:\Users\Siddharth Rout\Desktop"

Ret = BrowseForFolder(InitFolder)
端接头

函数BrowseForFolder(可选OpenAt作为变量)作为变量 将ShellApp设置为对象

Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0

Set ShellApp = Nothing

Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
    If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
    If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
    GoTo Invalid
End Select

Exit Function
无效: BrowseForFolder=False 端函数

Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0

Set ShellApp = Nothing

Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
    If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
    If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
    GoTo Invalid
End Select

Exit Function