Results 1 to 3 of 3
  1. #1
    crxftw is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    30

    VBA to create PDF and folder if doesn't exist!

    Hello. I'm not very good at using VBA, but I've managed to write a code that saves the current report as pdf to specific folder, if this folder already exists, then pdf will be created, if folder doesn't exist then folder will be created and then pdf, which will be attached to Outlook mail after.

    It's somehow working ok, but sometimes when I execute it, it creates the specific folder, but doesn't save the pdf as I get OutputTo error ' Microsoft Access can't save the output data to the file you've selected.'

    I can't find where is the problem or is it correct even in the code, because it is working sometimes.



    Code:
    Private Sub cmdEmail_Click()
    
    'Sets report caption name according to the invoice number
    Reports!Invoice.Caption = [txtInvoiceNumber]
    
    'Check if e-mail is set for a client
    If Not IsNull([txtEmail]) Then
    
        Dim myCurrentDir As String
        Dim myInvoiceDir As String
        Dim myInvoiceOutput As String
    
        'Set directories
        myCurrentDir = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
        myInvoiceDir = myCurrentDir & "Invoices" & "\" & txtClient & "\"
        myInvoiceOutput = myInvoiceDir & txtInvoiceNumber & ".pdf"
        
        'Check if directory exists
        If Len(Dir(myInvoiceDir, vbDirectory)) = 0 Then
            'Make new one if directory doesn't exist
            MkDir myInvoiceDir
        Else
            'Save report as PDF and set OutputFile to myInvoiceOutput
            DoCmd.OutputTo acOutputReport, [Report].[Name], acFormatPDF, myInvoiceOutput, , , , acExportQualityPrint
            'Execute Outlook SendMessage function with attachment path myInvoiceOutput
            SendMessage True, myInvoiceOutput
        End If
    
        Else
            MsgBox ("No E-mail address available for this client")
    
    End If
    End Sub
    Please I hope someone can help me with this. Thanks!

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Well, for starters your report name needs to be put in using a string.

    So:

    DoCmd.OutputTo acOutputReport, "Report Name Here", acFormatPDF, myInvoiceOutput, , , , acExportQualityPrint

    Where is this click event? Is it on the report itself or on a form?

  3. #3
    crxftw is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    30
    OnClick event is on the report itself. If I use Report.Name then it automatically picks the report I have open, but guess I could fix that.

    Anyway, I moved all my database files to hard drives root folder and all suddenly seems to work, no errors so far.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Browse For Folder Example
    By pkstormy in forum Code Repository
    Replies: 6
    Last Post: 01-08-2012, 04:13 PM
  2. Check path in another form and create folder
    By anandram in forum Programming
    Replies: 6
    Last Post: 05-02-2011, 12:23 PM
  3. Enter a folder name and open that folder
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 09-05-2010, 04:39 PM
  4. create On Click to go to specific server folder
    By airhud86 in forum Programming
    Replies: 1
    Last Post: 01-05-2010, 12:45 PM
  5. Replies: 0
    Last Post: 12-16-2009, 09:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums