Onedrive-VBA-folder path-synchronization 2 computers

Ivo 20 Reputation points
2025-11-09T14:47:52.41+00:00

I have 2 computers that are synchronized with One Drive and the same account.

In the folder Documents, I have Excel with VBA code "folderPath = Application.ThisWorkbook.Path".

On PC1 folderPath="C:\Users\OneDrive\Documents\appTest",

and on PC2 folderPath="https://d.docs.live.net/5f75a6f54f16e10e/Documents/appTest"

when I turn off synchronization on PC2, I get folderPath="C:\Users\OneDrive\Documents\appTest

q: How to get the same folderPath on PC2 as on PC1 without turning off synchronization?

or

q: how to replace PC1 and PC2 so that vba app runs correctly on PC2 without turning off synchronization.?

Microsoft 365 and Office | OneDrive | For education | Windows
0 comments No comments
{count} votes

Answer accepted by question author
  1. Vivian-HT 9,405 Reputation points Microsoft External Staff Moderator
    2025-11-10T04:01:34.6066667+00:00

    Dear @Ivo,

    Thank you for posting your question in the Microsoft Q&A forum.

    Before giving you the best solution, could you please confirm these questions below to help me diagnose the issue more effectively: 

    • What version of Office/Excel are you using on each PC (e.g., Office 2016 vs Office 365)?
    • Is the file stored in a personal OneDrive or OneDrive for Business?
    • Could you please double-check whether the folder appTest is set to “Always keep on this device” on PC2? Open your OneDrive folder in File Explorer > Right-click the appTest folder > Select “Always keep on this device” from the context menu.

    This ensures the folder and its contents are fully downloaded and stored locally. If the Office integration setting is also disabled, this should result in ThisWorkbook.Path returning a local path instead of a cloud URL. Always keeping the folder appTest synced locally on both PCs removes any ambiguity in path resolution

    User's image

    Moreover, since modifying the VBA code to handle OneDrive paths can get quite technical, especially if you're considering registry lookups or dynamic path conversion, I’d strongly encourage you to post your specific scenario on Stack Overflow.

    • It is more suitable for publishing on Stack Overflow (English only). 
    • Post VBA programming questions to Stack Overflow by using the vba tag, along with any other relevant tags.

    Disclaimer: Microsoft provides no assurances and/or warranties, implied or otherwise, and is not responsible for the information you receive from the third-party linked sites or any support related to technology.  

    You’ll get direct input from developers who’ve solved similar issues, and many have shared robust, reusable code for handling OneDrive’s URL-to-local path mapping in VBA. Then, clearly describe your setup (e.g., two PCs synced with OneDrive, VBA using ThisWorkbook.Path, differences in path behavior).

    Please note that Stack Overflow has guidelines such as requiring a descriptive title, a complete and concise problem statement, and sufficient details to reproduce your issue. Feature requests or overly broad questions are considered off topic.

    I hope this information is helpful. Please follow these steps and let me know if it works for you. If not, we can work together to resolve this.

    Please understand that our initial response does not always resolve the issue immediately. However, with your help and more detailed information, we can work together to find a solution.

    Thank you for your patience and your understanding. If you have any questions, please feel free to reach out.

    I'm looking forward for your reply.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".   

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    User's image


1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2025-11-09T14:48:03.2633333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To achieve consistent folder paths across both PCs without turning off synchronization, you can modify your VBA code to dynamically determine the folder path based on the environment. Instead of hardcoding the path, you can use the Environ function to get the user's OneDrive folder path. Here’s an example of how you might do this:

    Dim folderPath As String
    folderPath = Environ("OneDrive") & "\Documents\appTest"
    

    This way, regardless of whether you're on PC1 or PC2, the folderPath variable will always point to the correct OneDrive folder, as it retrieves the path from the environment variable that corresponds to the user's OneDrive location.

    By using this method, you ensure that your VBA application runs correctly on both PCs without needing to turn off synchronization.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.