The Rev
Registered User.
- Local time
- Today, 14:45
- Joined
- Jan 15, 2003
- Messages
- 118
I have a script that is assigned to a button that open an Excel template and then some auto filling. I am running into an issue when the file path is too long for Access' VBA. I am trying to work around it using ShortPath. But when I try to open the template, it says it can't find the path. Below is my code snippet
And on. When the file path isn't > 218, it opens fine. When it is > 218, it shows an error at the "set wb " that the script can't find the shortpath folder, however, I can put a MSGBOX instream and it shows the whole file name as the strSelectedItem variable. Any ideas?
Code:
Set objSFolders = CreateObject("WScript.Shell").SpecialFolders
Path = Application.CurrentProject.Path & "\Templates\"
Set fdg = Application.FileDialog(msoFileDialogFilePicker)
With fdg
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "File Type", "*.xlsx", 1
.InitialFileName = Path
If .Show = -1 Then
For Each strSelectedItem In .SelectedItems
'Make sure File Path isn't too long because Microsoft is stupid
FPLength = 0
FPLength = Len(strSelectedItem)
If FPLength > 218 Then
TempFldr = Dir(Path, vbDirectory)
If TempFldr = vbNullString Then
VBA.FileSystem.MkDir (Path)
End If
Set StrShortItem = Objfso.GetFile(strSelectedItem)
strSelectedItem = StrShortItem.ShortPath & "\" &StrShortItem.Name
Else
TempFldr = Dir(Path, vbDirectory)
If TempFldr = vbNullString Then
VBA.FileSystem.MkDir (Path)
End If
End If
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Open(strSelectedItem)
xl.Visible = True
....
And on. When the file path isn't > 218, it opens fine. When it is > 218, it shows an error at the "set wb " that the script can't find the shortpath folder, however, I can put a MSGBOX instream and it shows the whole file name as the strSelectedItem variable. Any ideas?