Sub GetFile()
Dim myObject As Object
Dim fileSelected As String
Dim myPath As String
Dim myFile As String
Dim strLen As Integer
Set myObject = Application.FileDialog(msoFileDialogOpen)
myPath = ThisWorkbook.Worksheets("Admin").Range("B7").Value
myPath = GetDefaultLocation(myPath)
With myObject
.Title = "Choose File"
.InitialFileName = myPath & ""
.AllowMultiSelect = False
If .Show <> -1 Then
MsgBox ("No File Selected")
Exit Sub
End If
fileSelected = .SelectedItems(1)
End With
fileSelected = GetLocalPath(fileSelected)
strLen = Len(fileSelected) - InStrRev(fileSelected, "")
myFile = Right(fileSelected, strLen)
strLen = Len(fileSelected) - strLen - 1
myPath = Left(fileSelected, strLen)
With Worksheets("Admin")
.Range("B7") = myPath
.Range("B10") = myFile
End With
End Sub
Public Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object
On Error Resume Next
Set myWS = CreateObject("WScript.Shell")
RegKeyRead = myWS.RegRead(i_RegKey)
End Function
Private Function GetLocalPath(docPath As String) As String
Const strcOneDrivePart As String = "https://d.docs.live.net/"
Dim strRetVal As String, bytSlashPos As Byte
strRetVal = docPath & ""
If Left(LCase(docPath), Len(strcOneDrivePart)) = strcOneDrivePart Then
bytSlashPos = InStr(Len(strcOneDrivePart) + 1, strRetVal, "/")
strRetVal = Mid(docPath, bytSlashPos)
strRetVal = RegKeyRead("HKEY_CURRENT_USEREnvironmentOneDrive") & strRetVal
strRetVal = Replace(strRetVal, "/", "")
strRetVal = Replace(strRetVal, "%20", " ")
End If
GetLocalPath = strRetVal
End Function
Function GetDefaultLocation(ByVal myString As String) As String
Dim folderExists As Boolean
On Error Resume Next
folderExists = (GetAttr(myString) And vbDirectory) = vbDirectory
On Error GoTo 0
If folderExists Then
GetDefaultLocation = myString
Else
GetDefaultLocation = Application.ThisWorkbook.Path
End If
End Function
|