Consulting

Results 1 to 11 of 11

Thread: vba to check if folder exists in SharePoint and if not then create folder

  1. #1

    vba to check if folder exists in SharePoint and if not then create folder

    Hi

    So I want my code to loop through and check if folder in sharepoint exists and if so then save PDF into it depending on the business unit. if there is no folder then create it.

    Sub SaveTo_SharePoint
    busUnitList = Array( "A", "B", "C", "D", "E", "F", "G" )
    Dim strFile As String
    strFile = "http://site/Reporting%20Documents/"
    For Each busUnit In busUnitList
        If Dir(strFile, vbDirectory) = busUnit Then
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:="http://site/Reporting%20Documents/" & fileName, IgnorePrintAreas:=False, OpenAfterPublish:=False
        Else
            MkDir (strFile & fileName)
        End If
    Next busUnit
    End Sub
    I get an error saying 'bad file name'

    Anyone know why?

    Thanks
    Last edited by Aussiebear; 04-19-2023 at 04:59 AM. Reason: Adjusted the code tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I would think it is because the variable filename is not given any value in the ExportAsFixedFormatType method.

    Why do you set the variable strFile to the base path and then not use it later but repeat the base path name?

    Shouldn't it be
    MkDir strFile & busUnit
    and then call ExportAsFixedFormatType after creating the directory?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Hey xld,

    Yes busUnit is what it should be, I just thought I could also use fileName since I have called it before in other loops.

    What do you mean by 'call ExportAsFixedFormatType after creating the directory'?

    Also I get an error on line

    MkDir (strFile & busUnit)
    saying path/file access error

    Thank you!!!

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Anne,

    If filename is declared as a public variable, you could re-use it here, but if declared in another procedure it will not exist in this one. It will get created implicitly when referenced if you are not using Option Explicit (more on this later), but you should not rely on either in my view. If it has been setup earlier, it is far better to pass it as a parameter in the call to this procedure in my view, make it clear where the values are coming from. Deliberate obfuscation is bad enough, accidental or unaware obfuscation is a tragedy.

    You should always use Option Explicit at the start of your modules. I know Bill Jelen does believe this, but as I have told him many times, he is wrong, it makes for far better code development, and forces better practices (declare all of your variables). You might have gotten an error on filename if you used this if it isn't a public variable (but still better to be more obvious on what filename is as I said above).

    As for ... 'call ExportAsFixedFormatType after creating the directory', I mean that if the directory doesn't exist you want to create it, but you still want to save trhe pdf, so you need another call to that method after MkDir.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Okay thanks xld, I will try and come up with a new solution

  6. #6
    Does anyone know how the MkDir is formatted?

    MkDir "//site/Reporting%20Documents/" & fileName
    Gives me an error
    Last edited by anne.gomes; 06-11-2014 at 02:37 PM.

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    I would use a space instead of %20

  8. #8
    Hey snb,

    It still gives me an error: Path/File access error

    Any ideas?

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I think the format depends on if the SharePoint or other server iss hosted on a Unix or Windows Servers, etc.

    Try \ instead if / in your ...


    MkDir "//site/Reporting%20Documents/" & filename
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10

    [Solved]

    Thank you Paul_Hossler!!! It did the trick!

    Here is the code everyone

    Sheets(graphTabList).Select
        
        If Dir("//site/Reporting%20Documents/& fileName", vbDirectory) = "" Then
        
        MkDir "//site\fbusharedservices\Reporting%20Documents\" & fileName
        
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        fileName:="http://site/Reporting%20Documents/" _
        & fileName & "\" & fileName, IgnorePrintAreas:=False, OpenAfterPublish:=False
        
        Else
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        fileName:="http://site/Reporting%20Documents/" _
        & fileName & "\" & fileName, IgnorePrintAreas:=False, OpenAfterPublish:=False
        End If
    Last edited by anne.gomes; 06-11-2014 at 08:11 PM.

  11. #11
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    I'd prefer:

    If Dir("//site/Reporting Documents/& fileName", 16) = "" Then   MkDir "//site\Reporting Documents"
      sheets(graphTabList).ExportAsFixedFormat 0,"http://site/Reporting Documents/"  & fileName

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
  •