2015-10-19 58 views
1

我在一個命令按鈕下點擊一小段代碼,在新位置保存工作簿文件的新名稱,我想知道是否可以自動創建快捷方式新保存的工作簿在不同的位置?自動創建文件的快捷方式

Private Sub CommandButton1_Click() 
Dim SelectedFNumber As String 
Dim DateStr As String 
Dim myFileName As String 
Dim StorePath As String 

    DateStr = Format(Now, "dd.mm.yy HH.mm") 

    SelectedFNumber = Range("B4").Text 

    If SelectedFNumber <> "SELECT F NUMBER" And Range("D11") > "0" Then 

     StorePath = "G:\Targets\" & SelectedFNumber & "\" 

     myFileName = StorePath & SelectedFNumber & " " & DateStr & ".xlsm 

     If Len(Dir(StorePath, vbDirectory)) = 0 Then 
     MkDir StorePath 
     End If 

     ActiveWorkbook.SaveAs Filename:=myFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled 
    Else 
     MsgBox "Select an F Number" 
    End If 
End Sub 

回答

2

你基本上需要添加這樣的事情:

Dim sShortcutLocation As String 

sShortcutLocation = "C:\blah\workbook shortcut.lnk" 

With CreateObject("WScript.Shell").CreateShortcut(sShortcutLocation) 
    .TargetPath = myFileName 
    .Description = "Shortcut to the file" 
    .Save 
End With 

改變位置,無論你想要的。

+0

完美謝謝!我不知道爲什麼我無法找到像其他任何地方一樣簡單的答案 –

相關問題