Results 1 to 3 of 3
  1. #1
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544

    Open Excel and fill in certain cells (inherited code)

    Hello all,



    I have a client who refuses to give up their Excel spreadsheet for estimating (even thought I've given them a perfectly good interface within Access), so I must give them a button that takes them to the template stored on their network (Cost Sheet_Template) and completes certain cells with the Estimate information from Access. I inherited this code and have made a few minor changes to the field names, and path information. The correct worksheet will open but the data will not populate. I don't know what I'm missing here (or just don't understand the whole thing), would love another set of eyes!

    Code:
    ' Late Binding (Needs no reference set)
    Dim oXL As Object
    Dim oExcel As Object
    Dim sFullPath As String
    Dim sPath As String
    Dim addr As String
     
    ' Create a new Excel instance
    Set oXL = CreateObject("Excel.Application")
     
    ' Only XL 97 supports UserControl Property
    On Error Resume Next
    oXL.UserControl = True
    On Error GoTo 0
     
    ' Full path of excel file to open
    'On Error GoTo ErrHandle
    sFullPath = Me.networkpath & "\Templates\Cost Sheet_Template.xlsm"
     
    ' Open it
    Set xl = CreateObject("Excel.Sheet")
    With oXL
    .Visible = True
    .Workbooks.Open (sFullPath)
    End With
        Dim fl_s As String
        Dim draw_num As String
        Dim draw_date As Date
        
    Dim fso
    Dim file As String
    file = Me.Estpath & ".xlsm"
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FileExists(file) Then
        If Len(Forms!currentjobs!Addendums.Form!AddendumNum) > 0 Then
            If Len(Forms!currentjobs!Addendums.Form!AddendumFloors) > 0 Then
                fl_s = Forms!currentjobs!Addendums.Form!AddendumFloors
            Else
                fl_s = Me.FLOORS
            End If
            If Len(Forms!currentjobs!Addendums.Form!AddendumDrawings) > 0 Then
                draw_num = Forms!currentjobs!Addendums.Form![AddendumDrawings]
            Else
                draw_num = Me.[DRAWINGnum]
            End If
            If Len(Forms!currentjobs!Addendums.Form!AddendumDrawingsDate) > 0 Then
                draw_date = Forms!currentjobs!Addendums.Form!AddendumDrawingsDate
            Else
            draw_date = Me.[DRAWING DATE]
            End If
        Else
            'fl_s = Me.FLOORS
            'draw_num = Me.[DRAWINGnum]
            'draw_date = Me.[DRAWING DATE]
        End If
        If Forms!currentjobs!addjobid > 0 Then
            If Forms!currentjobs!addjobid = 1 Then
            Set objXLApp = CreateObject("Excel.Application")
            Set objXLBook = objXLApp.Workbooks.Open(sFullPath)
           
            ElseIf Me.[Forms!currentjobs!Addendums.Form!Addendumnum] > 1 Then
            Set objXLApp = CreateObject("Excel.Application")
            Set objXLBook = objXLApp.Workbooks.Open(sFullPath & Left(Me.[JOBID], 7) & "R" & Format(Forms!currentjobs!Addendums.Form!AddendumNum - 1) & "\Estimates\" & Left(Me.[JOBID], 7) & "R" & Format(Forms!currentjobs!Addendums.Form!AddendumNum - 1) & ".xls")
            End If
                If Len(Forms!currentjobs!Addendums.Form!AddendumNum) > 0 Then
            If Len(Forms!currentjobs!Addendums.Form!AddendumFloors) > 0 Then
                fl_s = Forms!currentjobs!Addendums.Form!AddendumFloors
            Else
                fl_s = Me.FLOORS
            End If
            If Len(Forms!currentjobs!Addendums.Form!AddendumDrawings) > 0 Then
                draw_num = Forms!currentjobs!Addendums.Form![AddendumDrawings]
            Else
                draw_num = Me.[DRAWINGnum]
            End If
            If Len(Forms!currentjobs!Addendums.Form!AddendumDrawingsDate) > 0 Then
                draw_date = Forms!currentjobs!Addendums.Form!AddendumDrawingsDate
            Else
            draw_date = Me.[DRAWING DATE]
            End If
        Else
            fl_s = Me.FLOORS
            draw_num = Me.[DRAWINGnum]
            draw_date = Me.[DRAWING DATE]
        End If
            With objXLBook
            .ActiveSheet.Range("B1") = Me.[JOBID]
            .ActiveSheet.Range("B2") = Me.[JOB NAME]
            .ActiveSheet.Range("B3") = Me.[JOB ADDRESS 1]
            .ActiveSheet.Range("B4") = Me.FLOORS
            .ActiveSheet.Range("B5") = Me.ENGINEER
            .ActiveSheet.Range("G1") = Forms!currentjobs!GCTABLE1.Form![AllGCs]
            .ActiveSheet.Range("G2") = Forms!currentjobs!GCTABLE1.Form![gc1contact]
            .ActiveSheet.Range("G3") = draw_num
            .ActiveSheet.Range("G4") = draw_date
            .ActiveSheet.Range("G5") = Forms!currentjobs!Addendums.Form!AddendumNum
            .ActiveSheet.Range("l5") = Forms!currentjobs!Addendums.Form!AddendumNum
             DoEvents
            
            End With
    objXLBook.SaveAs [sFullPath] & ".xlsm"
     'objXLBook.SaveAs "sfullpath &" \ "& [Forms]![currentjobs]![JOB NAME] & .xls"
    objXLApp.Application.Visible = True
    End If
    End If
    ErrExit:
    Set oXL = Nothing
    Exit Sub
     
    ErrHandle:
    oXL.Visible = False
    MsgBox Err.Description
    GoTo ErrExit
    End Sub
    I have verified the cell references are correct and there is data in Access.

    Thank you in advance.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    The code worked before the minor changes?

    Now it doesn't but no error message?

    I have better luck if I use actual name of sheet instead of ActiveSheet.
    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
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Perhaps a sample database with some garbage data in it too?

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

Similar Threads

  1. Replies: 6
    Last Post: 10-02-2014, 03:22 PM
  2. Replies: 1
    Last Post: 04-15-2014, 02:45 PM
  3. fill blank cells of duplicate records
    By deso in forum Queries
    Replies: 3
    Last Post: 03-28-2013, 07:59 AM
  4. Running a code in an inherited Database
    By jlgray0127 in forum Forms
    Replies: 12
    Last Post: 03-14-2012, 02:58 PM
  5. Excel code not working with Excel open
    By jgelpi16 in forum Programming
    Replies: 1
    Last Post: 07-11-2011, 12:12 PM

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