Results 1 to 2 of 2

Thread: VBA Code - Please assist

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2017
    Posts
    12

    VBA Code - Please assist

    Good day,

    So i have the code that works on my Workbook and this saves the files as i need them, However i see that i will encounter a problem with "flooding the folder" pretty soon

    Would there be a way for VBA to automatically create a folder within the specified target location which is also named according to the text contained in a cell (Value in Sheets(1).Cells(7, 2)), this then subsequently saves the below .pdf files into this folder? essentially creating 1 folder for each document pack?

    (Let's say the value in cells (7,2) is 517321, VBA then creates a folder in
    C:\Users\JJ\Desktop\Exports\*517321*
    And subsequently saves the following (code below to save into this folder)

    C:\Users\JJ\Desktop\Exports\*517321*\ 517321 Packing List
    C:\Users\JJ\Desktop\Exports\*517321*\ 517321 Commercial Invoice
    C:\Users\JJ\Desktop\Exports\*517321*\ 517321 SDV
    etc



    Private Sub CommandButton1_Click()

    Dim wb As Workbook

    numberofcopies = InputBox("Number of copies to print", "Number of copies")

    Dim controlname

    If Me.CheckBox1 = True Then
    Set wsI = Sheets(3)
    With wsI
    NewFileName = "C:\Users\JJ\Desktop\Exports\" & Sheets(1).Cells(7, 2) & "_Packing List.pdf"

    .Range("A1:N32").ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=NewFileName, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True

    .PrintOut , , numberofcopies
    End With
    End If

    If Me.CheckBox2 = True Then
    Set wsI = Sheets(4)
    With wsI
    NewFileName = "C:\Users\JJ\Desktop\Exports\" & Sheets(1).Cells(7, 2) & "_Commercial Invoice.pdf"

    .Range("A1:G35").ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=NewFileName, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True

    .PrintOut , , numberofcopies
    End With
    End If

    If Me.CheckBox3 = True Then
    Set wsI = Sheets(5)
    With wsI
    NewFileName = "C:\Users\JJ\Desktop\Exports\" & Sheets(1).Cells(7, 2) & "_SDV Shipping instruction.pdf"

    .Range("A1:S75").ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=NewFileName, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True

    .PrintOut , , numberofcopies
    End With
    End If

    If Me.CheckBox4 = True Then
    Set wsI = Sheets(6)
    With wsI
    NewFileName = "C:\Users\JJ\Desktop\Exports\" & Sheets(1).Cells(7, 2) & "_SIF.pdf"

    .Range("A1:K53").ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=NewFileName, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True

    .PrintOut , , numberofcopies
    End With
    End If

    If Me.CheckBox5 = True Then
    Set wsI = Sheets(7)
    With wsI
    NewFileName = "C:\Users\JJ\Desktop\Exports\" & Sheets(1).Cells(7, 2) & "_DGPA.pdf"

    .Range("A1:R58").ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=NewFileName, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True

    .PrintOut , , numberofcopies
    End With
    End If
    If Me.CheckBox6 = True Then
    Set wsI = Sheets(10)
    With wsI
    NewFileName = "C:\Users\JJ\Desktop\Exports\" & Sheets(1).Cells(7, 2) & "_SDS Class 9.pdf"

    .Range("A1:J300").ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=NewFileName, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True

    .PrintOut , , numberofcopies
    End With
    End If

    If Me.CheckBox7 = True Then
    Set wsI = Sheets(11)
    With wsI
    NewFileName = "C:\Users\JJ\Desktop\Exports\" & Sheets(1).Cells(7, 2) & "_SDS Class 2.2.pdf"

    .Range("A1:J300").ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=NewFileName, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True

    .PrintOut , , numberofcopies
    End With
    End If

    MsgBox ("PDF Saved and Generated")

    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Code - Please assist

    i will encounter a problem with "flooding the folder" pretty soon
    so you would prefer to flood with folders?

    of course it is easy to do
    Code:
    mkdir "C:\Users\JJ\Desktop\Exports\*" & Sheets(1).Cells(7, 2) & "*"
    NewFileName = "C:\Users\JJ\Desktop\Exports\*" & Sheets(1).Cells(7, 2) & "*\" & Sheets(1).Cells(7, 2) & "_Commercial Invoice.pdf"
    i did not test this, and i am not sure if * are valid in file (folder) names
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width