Results 1 to 2 of 2

Thread: Error 52: bad file name or number with OneDrive for Business

  1. #1
    VBAX Newbie
    May 2021

    Question Error 52: bad file name or number with OneDrive for Business


    We have a piece of software which automatically generates Word files and populates data using VBA. Since moving to OneDrive for Business, we are not using the personal version of OneDrive, we get the error 'Error 52: Bad file name or number' when running reports.

    I have investigated and the issue happens when vba attempts to open another file, which is generated successfully in the same folder as the Word file. I believe the issue is that VBA can't decipher the online address of OneDrive that the code seems to get when referencing ActiveDocument.Path, I checked with msgbox(fieldNameFile). The format is 'https://<companyname><username>/Documetns/Documents/SoftwareCompany'.

    I can get it to work if I change the fieldNameFile in VBA to the static local path of OneDrive i.e. 'C:\Users\<username>\OneDrive - <companyname>\Documents\SoftwareCompany'. Alternatively if I turn off collaboration in OneDrive VBA will attempt to load the local copy of the files in the same location as listed in the last sentence, which also works. To turn off collaboration in OneDrive go to Help & Settings > Settings, click the Office tab and deselect 'Use Office applications to sync Office files that I open'.

    Neither of these options is great as we can't ask users to edit every single report they open and they also need to collaboratively use files on OneDrive on a regular basis. Does anyone have any ideas?

    'populate the menu from the fields in ReportFields.txt
    fieldNameFile = ActiveDocument.Path + "\ReportFields.txt"
    'the next line is where it fails, so you don't really need the rest of the code
    Open fieldNameFile For Input As #1
       '(the rtf template name is in the first line)
       Line Input #1, templateName
       While Not EOF(1)
          Line Input #1, fieldName
          With oPopup.Controls.Add
           .Caption = fieldName
           .Parameter = fieldName
           .OnAction = "OnInsertField"
        End With
    Close #1
    I would be eternally grateful if you have any ideas.

    Last edited by Reverend; 05-26-2021 at 07:46 AM. Reason: making the code clearer

  2. #2
    It is clear that your code cannot resolve the address. Where you go from here I suspect depends on your ability to resolve the variable <companyname>. The following might then work
    fieldNameFile = Environ("USERPROFILE") & "\OneDrive - " & "<companyname>" & "\Documents\SoftwareCompany\ReportFields.txt"
    alternatively change the direction of the path separator in your original code, however this may then produce issues relating to password access.
    'MsgBox Left(ActiveDocument.path, 8)    
        If Left(ActiveDocument.path, 8) = "https://" Then
            fieldNameFile = ActiveDocument.path + "/ReportFields.txt"
            fieldNameFile = ActiveDocument.path + "\ReportFields.txt"
        End If
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts