Results 1 to 3 of 3
  1. #1
    Authomas is offline Novice
    Windows 10 Access 2007
    Join Date
    Nov 2022
    Posts
    15

    Question Error when running access module


    I made a program to export a an excel file, edit it, and then attach it to an outlook email. Everything works half the time but the other half, i get an error "Run-time error '1004': Methos 'Columns'of object'_Global'failed", clicking debug brings me to my code "Columns("B:B").Delete Shift:=xlToLeft", any ideas as to why this works sometimes and why other times it doesn't, and is their a way to fix it?

    Code:

    Code:
    Public Function excelExportNewParts()
        DoCmd.OutputTo acOutputQuery, "qNewPartTemplate", acFormatXLSX, "G:\Tooling\Requests Sent\TBE Enter" & Format(Now(), "MM-DD-YYYY") & "TBE.xlsx", False
            
        Dim xlapp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
        Dim xldata As Excel.Range
        Dim ExcelPath As String
        Dim rowNo As Long
    
    
    
    
        ExcelPath = CurrentProject.Path & ""
        Set xlapp = CreateObject("Excel.Application")
        Set xlBook = xlapp.Workbooks.Open("G:\Tooling\Requests Sent\TBE Enter" & Format(Now(), "MM-DD-YYYY") & "TBE.xlsx")
        Set xlSheet = xlBook.Worksheets("qNewPartTemplate")
    
    
    
    
        xlapp.Visible = True
        xlSheet.Select
        xlSheet.Activate
     
        'With ActiveSheet
            Columns("B:B").Delete Shift:=xlToLeft
            Range("B1:B1") = "Description"
            Cells.Select
            Selection.ClearFormats
            Columns("F:F").NumberFormat = "0.00"
        'End With
        
        xlBook.Save
        xlBook.Close
        
        Set xlSheet = Nothing
        Set xlBook = Nothing
        Set xlapp = Nothing
        
        ' Outlook email
        Dim objOutlook As Outlook.Application
        Dim objOutlookMsg As Outlook.MailItem
        Dim objOutlookAttach As Outlook.Attachment
        
        ' Create the Outlook session.
        Set objOutlook = CreateObject("Outlook.Application")
        
        ' Create the message.
        Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
        With objOutlookMsg
            .To = "email"
            .Body = "Hello," & vbNewLine & vbNewLine & "Could you please enter these when you get a chance?" & vbNewLine & vbNewLine & "-Thank you!"
            ' Set the Subject of the message.
            If Not IsMissing("New Parts") Then
                .Subject = CStr("New Parts")
            End If
            ' Add attachments to the message.
            If Not IsMissing("G:\Tooling\Requests Sent\TBE Enter" & Format(Now(), "MM-DD-YYYY") & "TBE.xlsx") Then
                Set objOutlookAttach = .Attachments.Add("G:\Tooling\Requests Sent\TBE Enter" & Format(Now(), "MM-DD-YYYY") & "TBE.xlsx")
            End If
       End With
       
       objOutlookMsg.Display
    
    
       
       Set objOutlookMsg = Nothing
       Set objOutlook = Nothing
    
    
    End Function

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Try explicitly qualifying object referencing. Use the xlSheet object.

    xlSheet.Range("B1:B1")

    xlSheet.Columns("B:B")

    xlSheet.Cells.Select

    Can wrap in a With block:
    Code:
    With xlSheet
         .Columns("B:B").Delete Shift:=xlToLeft
         .Range("B1:B1") = "Description"
         .Cells.ClearFormats
         .Columns("F:F").NumberFormat = "0.00"
    End With
    Note for future: Please post code between CODE tags to retain indentation and readability.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Authomas is offline Novice
    Windows 10 Access 2007
    Join Date
    Nov 2022
    Posts
    15
    Awesome it worked! Thank you! Also sorry about that I updated it and will keep that in mind in the future.

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

Similar Threads

  1. Running a module from cmd button
    By Robert2150 in forum Modules
    Replies: 5
    Last Post: 10-23-2018, 02:13 AM
  2. MS ACCESS-Running Module to open Excel
    By BLUDEV in forum Macros
    Replies: 4
    Last Post: 02-10-2015, 04:52 PM
  3. Replies: 8
    Last Post: 06-27-2013, 11:58 AM
  4. Running a module
    By KevinMCB in forum Modules
    Replies: 2
    Last Post: 03-09-2011, 02:38 PM
  5. Running module from Macro
    By Harley Guy in forum Modules
    Replies: 6
    Last Post: 10-27-2010, 11:05 AM

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
  •  
Other Forums: Microsoft Office Forums