+ Reply to Thread
Results 1 to 4 of 4

Save files to a Mapped Network Drive on server

Hybrid View

  1. #1
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Save files to a Mapped Network Drive on server

    The following VBA is to name and save multiple files to a mapped network drive on a server. I have the code working when saving locally, however when saving to the mapped network drive, my files keep saving to a default directory rather than the specified location. I have read/write permissions for the network drive.

    Thanks in advance...

    With ThisWorkbook
    
        Dim newFile1 As String, fName1 As String, newFile2 As String, fName2 As String, newFile3 As String, fName3 As String, newFile4 As String, fName4 As String, newFile5 As String, fName5 As String, newFile6 As String, fName6 As String
        fName1 = Range("=LISTS!A1").Value
        fName2 = Range("=LISTS!A2").Value
        fName3 = Range("=LISTS!A3").Value
        fName4 = Range("=LISTS!A4").Value
        fName5 = Range("=LISTS!A5").Value
        fName6 = Range("=LISTS!A6").Value
        
        newFile1 = fName1
        newFile2 = fName2
        newFile3 = fName3
        newFile4 = fName4
        newFile5 = fName5
        newFile6 = fName6
         ' Change directory to suit your PC, including USER NAME
        ChDir _
        "Z:\Individual_Files"
        ActiveWorkbook.SaveAs Filename:=newFile1, FileFormat:= _
            xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
        ActiveWorkbook.SaveAs Filename:=newFile2, FileFormat:= _
            xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
        ActiveWorkbook.SaveAs Filename:=newFile3, FileFormat:= _
            xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
        ActiveWorkbook.SaveAs Filename:=newFile4, FileFormat:= _
            xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
        ActiveWorkbook.SaveAs Filename:=newFile5, FileFormat:= _
            xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
        ActiveWorkbook.SaveAs Filename:=newFile6, FileFormat:= _
            xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    Application.DisplayAlerts = False
     
    End With
    Last edited by wotsup; 03-13-2012 at 09:06 AM.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Save files to a Mapped Network Drive on server

    Try using the full UNC name of the folder

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Save files to a Mapped Network Drive on server

    You don't need chdir.
    This is all you need (provided cells A1:A5 contain valid Workbooknames, including extensions.
    Sub snb()
      for j=1 to 5
        thisworkbook.savecopyas "Z:\Individual_files\" & sheets("Lists").cells(j,1)
      next
    end sub
    Last edited by snb; 03-12-2012 at 06:52 AM.



  4. #4
    Valued Forum Contributor
    Join Date
    12-16-2004
    Location
    Canada, Quebec
    Posts
    363

    Re: Save files to a Mapped Network Drive on server

    I would go with kyle on this one
    Declare Function WNetGetConnectionA Lib "mpr.dll" _
                                        (ByVal lpszLocalName As String, _
                                         ByVal lpszRemoteName As String, _
                                         cbRemoteName As Long) As Long
                                         Dim flag
                                         
    
    
    Function GetUNCPath(myDriveLetter As String) As String
    
        Dim lReturn As Long
        Dim szBuffer As String
    
        myDriveLetter = Left(myDriveLetter, 1) & ":"
    
        szBuffer = String$(256, vbNullChar)
        lReturn = WNetGetConnectionA(myDriveLetter, szBuffer, 256)
    
        If lReturn = 0 Then
            GetUNCPath = Left$(szBuffer, InStr(szBuffer, vbNullChar))
    
        Else
            GetUNCPath = "Invalid drive"
            flag = "No"
        End If
    
    End Function
    Sub test1()
     Dim letdrive As String
     'get active name
        namfile = ActiveWorkbook.FullName
     'isolate drive letter
        howlong = Len(namfile)
        nameonly = Right(namfile, (howlong - 2))
     'drive letter
        letdrive = Left(namfile, 1)
     'get server name path
        getuncpath1 = GetUNCPath(letdrive)
     'remove 2 special character last
        lenserver = Len(getuncpath1)
        servername = Left(getuncpath1, (lenserver - 2))
     'assemble new name
        getfullservername = servername & nameonly
     'display
         MsgBox getfullservername
    
    End Sub
    Denis

    Please always attach the sample workbook without sensitive information when asking for help

    To add a module
    Press Alt + F11 (this is the Visual Basic Environment)
    Insert Menu, select Module
    Past code there
    Close Visual Basic Environment (X)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1