Results 1 to 2 of 2
  1. #1
    Bzbzb1 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    2

    Error with VBA Code

    I'm trying to send value from a form to an excel template. Here's my code but I'm getting an error on line 7 saying "Run-time error '9'". Any suggestions would be appreciated. I'm a novice coder.



    Code:
    Private Sub Generate_Report__Excel__Click()
        Dim MyXL As Object
        FileCopy "D:\Testing\AB Trans 1-12_5 Template.xlsx", "D:\Testing\Sieve Report " & Me![Sample Number] & ".xlsx"
        Set MyXL = GetObject("D:\Testing\Sieve Report " & Me![Sample Number] & ".xlsx")
        MyXL.Application.Visible = True
        MyXL.Application.WindowState = 3
        MyXL.Parent.Windows(1).Visible = True
        MyXL.Parent.ActiveWindow.WindowState = 2
        
        With MyXL.worksheets(1)
            .Range("H7").Value = Me![Client]
            .Range("H8").Value = Me![Project]
            .Range("A23").Value = Me![Source]
            .Range("B23").Value = Me![Sample Description (spec)]
            .Range("C23").Value = Me![Sample Number]
        End With
        
        MyXL.worksheets(1).Activate
    OpenEnd:
        Set MyXL = Nothing
        Exit Sub
        
    OpenErr:
        Select Case Err.Number
            Case 1004, 3265  ' can't write to protected cell; OR Item not found in this collection.
                Resume Next
            Case Else
                MsgBox Err.Number & "  -  " & Err.Description, vbCritical, "Critical error: isExcelSetup()"
                Resume OpenEnd
        End Select
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522
    dont call an excel sheet by :MyXL.Parent.Windows(1)
    call it by its name:

    MyXL.workbooks("myfile").select
    or
    MyXL.sheets("Sheet1").select


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

Similar Threads

  1. Error in code vba
    By azhar2006 in forum Forms
    Replies: 5
    Last Post: 12-12-2017, 02:02 PM
  2. SQL code error
    By Lou_Reed in forum Access
    Replies: 49
    Last Post: 06-21-2017, 11:57 AM
  3. SQL Code Error?
    By Lou_Reed in forum Access
    Replies: 5
    Last Post: 08-28-2015, 11:59 AM
  4. Error in code
    By cbende2 in forum Programming
    Replies: 5
    Last Post: 06-04-2015, 10:39 AM
  5. VBA Code Returning Error Run Time Error 3061
    By tgwacker in forum Access
    Replies: 2
    Last Post: 11-24-2013, 11:00 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