编辑宏以在桌面上为任何用户创建文件夹

时间:2016-02-17 15:07:48

标签: excel vba save create-directory

我有一张电子表格,每张纸上都有一个保存按钮。按钮当前将纸张作为PDF文件保存到任何用户的桌面上。我被问到是否可以让按钮创建一个名为" BSInHouseAssets"这样做时在桌面上。我猜我们会在某个时候利用MKdir ......但我需要帮助。 以下是保存文件的宏的当前部分。

Function SpecialFolderPath() As String

  Dim objWSHShell As Object
  Dim strSpecialFolderPath

  'On Error GoTo ErrorHandler
  ' Create a shell object
  Set objWSHShell = CreateObject("WScript.Shell")
  ' Find out the path to the passed special folder,
  '  just change the "Desktop" for one of the other options
  SpecialFolderPath = objWSHShell.SpecialFolders("desktop")
  ' Clean up
  Set objWSHShell = Nothing
  Exit Function
ErrorHandler:

  MsgBox "Error finding " & strSpecialFolder, vbCritical + vbOKOnly, "Error"
End Function

2 个答案:

答案 0 :(得分:1)

mkdir CreateObject("wscript.shell").specialfolders("desktop") & "\MyFolder"

应该做的伎俩

答案 1 :(得分:0)

strPath = "C:\Users\" & Environ("UserName") & "\Desktop\"
strFolderName = "test1"
strFullPath = strPath & strFolderName & "\"

    If Dir(strPath & strFolderName, vbDirectory) = "" Then
    MkDir strFullPath
    End If

ActiveWorkbook.SaveAs Filename:=strFullPath & "workbookname1", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled