Excel 从注册表调用路径以引用文件夹时出现对象必需错误
我的现有userform需要选择一个文件夹路径以保存到。如果没有,则禁用输出功能 请注意,选择文件夹和输出功能可以正常工作 我已将系统设置为将用户上次使用的文件夹保存到注册表中。我可以从注册表中将其作为字符串调用。当我尝试将路径字符串行:set WorkFolder设置为作为对象返回的文件夹字符串值时,由于424对象必需错误而崩溃 我找到了如何进出注册表的方法。让Set WorkFolder接受从注册表中提取的内容让我感到悲伤 文本框将被禁用,并在有效的文件对话框选择完成后更新,以便用户知道正在使用的路径 我想用有效的注册表调用值填充它,然后将WorkFolder设置为路径字符串值,删除nothing 当调试并将鼠标悬停在sResult上时,工具提示中有正确的字符串,我只需要将其插入Set WorkFolderExcel 从注册表调用路径以引用文件夹时出现对象必需错误,excel,vba,Excel,Vba,我的现有userform需要选择一个文件夹路径以保存到。如果没有,则禁用输出功能 请注意,选择文件夹和输出功能可以正常工作 我已将系统设置为将用户上次使用的文件夹保存到注册表中。我可以从注册表中将其作为字符串调用。当我尝试将路径字符串行:set WorkFolder设置为作为对象返回的文件夹字符串值时,由于424对象必需错误而崩溃 我找到了如何进出注册表的方法。让Set WorkFolder接受从注册表中提取的内容让我感到悲伤 文本框将被禁用,并在有效的文件对话框选择完成后更新,以便用户知道正在
Private Sub BtnSelectFolder_Click()
' ===== FOLDER SELECTION BY USER (MANDATORY) =====
Dim fd As FileDialog
Dim result As Long, errNum As Long
'-------------------------------
'Trouble section
Dim oWSH As Object
Dim sResult
Dim KeyId, Rootkey
Rootkey = "HKCU"
KeyId = "Software\VB and VBA Program Settings\MyApplication\WorkbookPath\SaveFolder"
If oWSH Is Nothing Then
Set oWSH = CreateObject("WScript.Shell")
End If
sResult = oWSH.RegRead(Rootkey & "\" & KeyId)
Set WorkFolder = sResult '<<<<<<<<< Why won't this work? Error 424 Object required, but path is pulled from Registry successfully
'Trouble section
'-------------------------------
'All works below
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.AllowMultiSelect = False
.ButtonName = "Select"
.Title = "Choose Output Folder"
If InStr(UCase(.InitialFileName), "SYSTEM32") Then
.InitialFileName = Environ("USERPROFILE") & "\Documents"
End If
result = .Show
End With
' Drop if box cancelled
If result = 0 Then Exit Sub
' Made it here; try updating the linked folder, with error handling
On Error Resume Next
Set WorkFolder = fs.GetFolder(fd.SelectedItems(1))
errNum = Err.Number: Err.Clear: On Error GoTo 0
If errNum <> 0 Then
MsgBox "Invalid folder selection", _
vbOKOnly + vbCritical, _
"Error"
Exit Sub
End If
' Update display textbox
TxBxFolder.Value = WorkFolder.Path
'Save path to registry
SaveSetting "MyApplication", "WorkbookPath", "SaveFolder", WorkFolder.Path
' Update the Export button
setExportEnabled
Call CheckExportIsEnabled
End Sub
假设在某处声明了WorkFolder?作为字符串,则字符串不是VBA中的对象-您无法设置它
我怀疑WScript.Shell是否返回一个可以设置的对象。如果它是一个字符串,那么您需要一个简单的Let赋值:
WorkFolder = sResult '<~ implicit: Let WorkFolder = sResult
如果我不得不猜测的话,我会认为fs是一个Scripting.FileSystemObject,它在哪里声明和分配,为什么在使用它的地方看不到它?它的GetFolder方法返回一个Scrpiting.Folder对象引用
您正在重用变量,赋予它们不同的含义和责任:错误就是这样发生的
一个变量,一个目的。WorkFolder是在哪里定义的?对不起,应该提到….>'==GLOBALS===Dim WorkFolder As folder不应该成为全局变量。它是我构建的开放源代码,位于那里。我会努力解决这个问题…泰。明白了,我会重新修改moto上的变量名…也许应该从git开始。我会继续努力,看看是否能解决这个问题。。。
WorkFolder = sResult '<~ implicit: Let WorkFolder = sResult
Set WorkFolder = fs.GetFolder(fd.SelectedItems(1))