Application.ActiveWorkbook.Path change

jono_oh

New Member
Joined
Sep 24, 2021
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have been using the macro below to find the filepath of the active workbook and open/loop through particular files. I did it in this manner so that it did not matter where users stored the workbook.

VBA Code:
sub open_files()

Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object

Dim sFilePath As String

sFilePath = Application.Activeworkbook.Path & "\"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sFilePath)

For Each oFile In oFolder.Files

    If InStr(1, oFile.Name, "TSP") > 0 And InStr(1, oFile.Name, "Blank Template") = 0 Then
    
         Workbooks.Open (oFile)
         
    End If
    
Next oFile

End Sub

However, my new work version of excel has changed this and now Application.Activeworkbook.Path returns as:

https://NameOfMyWorkplaceHeremy.sharepoint.com/personal/jonathan_ohara_name_of_my_workplace_here/Documents/2111wk17/Conversion

Previously, Application.Activeworkbook.Path returned in the following manner:

C:\Users\46802537\OneDrive - Name Of My Workplace Here\2111wk17\Conversion\


My question is whether is it possible to amend how Application.Activeworkbook.Path operates?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
My question is whether is it possible to amend how Application.Activeworkbook.Path operates?
What is it you want to amend? It is giving you the correct path, which is now on a hosted SharePoint site.
 
Upvote 0
What is it you want to amend? It is giving you the correct path, which is now on a hosted SharePoint site.
The filepath- the location of the files has not changed; the macro returns an error of 'path not found' when using the macro above
 
Upvote 0
The path is returning as a SharePoint path, so all indications are that the location of the files has changed. How are you opening the file that contains this code?
 
Upvote 0
The path is returning as a SharePoint path, so all indications are that the location of the files has changed. How are you opening the file that contains this code?
I am opening the file from OneDrive, thank you for your help so for @6StringJazzer, very new to excel/tech so appreciate your patience
 

Attachments

  • Application Activeworkbook Path.jpg
    Application Activeworkbook Path.jpg
    141.9 KB · Views: 14
Upvote 0
Unfortunately I do not have access to a SharePoint site for testing. However, OneDrive is replicated storage, so if you are opening a file from OneDrive it should show up as a local file. What your code does makes it look like you are opening the file in SharePoint.

I am stuck but let me see what else I can find.
 
Upvote 0
I
Unfortunately I do not have access to a SharePoint site for testing. However, OneDrive is replicated storage, so if you are opening a file from OneDrive it should show up as a local file. What your code does makes it look like you are opening the file in SharePoint.

I am stuck but let me see what else I can find.
think I have found a solution from StackOverflow:

I have inserted the below code into a module:

VBA Code:
Sub TestLocalFullName()
    Debug.Print "URL: " & ActiveWorkbook.FullName
    Debug.Print "Local: " & LocalFullName(ActiveWorkbook.FullName)
    Debug.Print "Test: " & Dir(LocalFullName(ActiveWorkbook.FullName))
End Sub

Public Function LocalFullName$(ByVal fullPath$)
    'Finds local path for a OneDrive file URL, using environment variables of OneDrive
    'Reference https://stackoverflow.com/questions/33734706/excels-fullname-property-with-onedrive
    'Authors: Philip Swannell 2019-01-14, MatChrupczalski 2019-05-19, Horoman 2020-03-29, P.G.Schild 2020-04-02

    Dim ii&
    Dim iPos&
    Dim oneDrivePath$
    Dim endFilePath$

    If Left(fullPath, 8) = "https://" Then 'Possibly a OneDrive URL
        If InStr(1, fullPath, "my.sharepoint.com") <> 0 Then 'Commercial OneDrive
            'For commercial OneDrive, path looks like "https://companyName-my.sharepoint.com/personal/userName_domain_com/Documents" & file.FullName)
            'Find "/Documents" in string and replace everything before the end with OneDrive local path
            iPos = InStr(1, fullPath, "/Documents") + Len("/Documents") 'find "/Documents" position in file URL
            endFilePath = Mid(fullPath, iPos) 'Get the ending file path without pointer in OneDrive. Include leading "/"
        Else 'Personal OneDrive
            'For personal OneDrive, path looks like "https://d.docs.live.net/d7bbaa#######1/" & file.FullName
            'We can get local file path by replacing "https.." up to the 4th slash, with the OneDrive local path obtained from registry
            iPos = 8 'Last slash in https://
            For ii = 1 To 2
                iPos = InStr(iPos + 1, fullPath, "/") 'find 4th slash
            Next ii
            endFilePath = Mid(fullPath, iPos) 'Get the ending file path without OneDrive root. Include leading "/"
        End If
        endFilePath = Replace(endFilePath, "/", Application.PathSeparator) 'Replace forward slashes with back slashes (URL type to Windows type)
        For ii = 1 To 3 'Loop to see if the tentative LocalWorkbookName is the name of a file that actually exists, if so return the name
            oneDrivePath = Environ(Choose(ii, "OneDriveCommercial", "OneDriveConsumer", "OneDrive")) 'Check possible local paths. "OneDrive" should be the last one
            If 0 < Len(oneDrivePath) Then
                LocalFullName = oneDrivePath & endFilePath
                Exit Function 'Success (i.e. found the correct Environ parameter)
            End If
        Next ii
        'Possibly raise an error here when attempt to convert to a local file name fails - e.g. for "shared with me" files
        LocalFullName = vbNullString
    Else
        LocalFullName = fullPath
    End If
End Function

Then, from a different module I have:

VBA Code:
Sub path_test()
'macro to test the application.activeworkbook.path
'to see whether I may have saved the previous version
'in sharepoint as opposed to OneDrive

ThisWorkbook.Activate

Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object

Dim sFilePath As String


sFilePath = LocalFullName(ActiveWorkbook.Path)
Debug.Print sFilePath

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sFilePath)

For Each oFile In oFolder.Files

    If InStr(1, oFile.Name, "TSP") > 0 And InStr(1, oFile.Name, "Blank Template") = 0 Then
       
        Workbooks.Open (oFile)

    End If

Next oFile

End Sub

A link to the source where I found the solution:

 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top