Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228

    Data Failing to update


    In Access im running code to update a word document. This code also runs some excel macros shown below:

    Code:
          xl.Run "ThisWorkbook.datarefresh"
       xl.Run "ThisWorkbook.BetterExcelDataToWord"
    In excel I have a table of data from access. I cant consistantly get this to update before its pasted into word. Code shown here:

    Code:
    Sub BetterExcelDataToWord()
    
    Dim objWord As Object, objDoc As Object
    Dim strFolder As String, strName As String
    Dim ws As Worksheet
    Dim lngLastRow As Long
    On Error GoTo Errorcatch
    
    ActiveWorkbook.RefreshAll
      Dim Ticker As Range
      
    Sheets("PasteSpecial").Activate
    
        Sheets("PasteSpecial").Range("A4:H65").Delete
     
     Application.Wait (Now + TimeValue("00:00:05"))
        Sheets("RISKS").Activate
        Set Ticker = Range(Cells(4, 1), Cells(65, 8))
        Ticker.Copy
        
    
        Sheets("PasteSpecial").Activate
        Cells(4, 1).PasteSpecial xlPasteValues
    strFolder = "\\server\general\RAMS\RAM_RAMS\" & Cells(1, 9).Value
    Debug.Print strFolder
    
    Set ws = ThisWorkbook.Sheets("PasteSpecial")
    lngLastRow = [LOOKUP(2,1/(A1:A65000<>""),ROW(A1:A65000))]
    Set objWord = CreateObject("Word.Application")
    ws.Range("A4" & ":H" & lngLastRow).Copy
    
    Debug.Print
    'open the word doc
    
    With objWord
      .Visible = True
      Set objDoc = .Documents.Open(strFolder)
      'pastes the value of cell at the bookmark
      With objDoc.Bookmarks("RISKS").Range
        .Characters.Last.Next.PasteAppendTable
        '.Tables(1).Rows(1).HeadingFormat = True
      End With
      .Activate
    End With
    Set objWord = Nothing: Set objDoc = Nothing
    'Clear The Clipboard
    Application.CutCopyMode = False
    
        'Application.DisplayAlerts = False
        'ActiveWorkbook.Close
        'Application.DisplayAlerts = True
    Exit Sub
    Errorcatch:
    Debug.Assert False
    MsgBox err.Description
    ' This is temporary, if you leave it in it will go into an endless loop so do not forget to remove
    Resume
    
    
    
    End Sub
    
    Sub datarefresh()
    Sheets("RISKS Import").Range("A1").Select
    
    ActiveWorkbook.RefreshAll
    'Worksheets("RISKS Import").ListObjects("Table1").Refresh
    
    End Sub
    some other issues:

    If I run datarefresh from inside excel it updates no problem. If i run it from access I get error 1004 “Application-defined or Object-defined error”.

    any suggestions appreciated.
    Last edited by Homegrownandy; 09-26-2018 at 04:43 AM.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Quote Originally Posted by Homegrownandy View Post
    If I run datarefresh from inside access it updates no problem. If i run it from access I get error 1004 “Application-defined or Object-defined error”.
    Those two statements contradict each other.

    Which line throws error?
    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
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    If I run datarefresh from inside access it updates no problem.
    I'll wager that was a typo and this is an Excel procedure since it would never run from Access. It doesn't define and set the required automation objects that you'd need in order to run from the Access side of things.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    I have updated the first post, there were several mistakes in it.

    sorry, If i run it from inside excel* it runs fine.
    Its when I call the macro in access its a problem.

    Ive only provided a small part of the code in access because there is so much of it.

    Ive edited the datarefresh macro several times for testing, its also included in the main macro (just to clear up any confusion).

    datarefresh macro now looks like this:

    Code:
    Sub datarefresh()
    Sheets("RISKS Import").Range("A1").Select
    
    ActiveWorkbook.RefreshAll
    End Sub

    Running this inside excel it does work.

    Micron im not sure what to do with your advice there, but it's important to note I have to use late binding. Could you elabourate please?

    thanks, Andy

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Just as code declares and sets Word application and document object variables, to manipulate Excel from Access need to also declare and set Excel application and workbook object variables.

    However, if this code is in Access then it should error with "Object variable or With block variable not set."

    So, I am still confused.
    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.

  6. #6
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Right, Its the line in access;

    Code:
     xl.Run "ThisWorkbook.datarefresh"
    I get the error mentioned earlier. "1004 “Application-defined or Object-defined error”."

    but again; inside excel it runs no problem.

  7. #7
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    @ june the data refresh code is in excel and run from access. Sorry for any earlier confusion I caused.

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    the answers in posts 3 and 5 have basically pinpointed the problem, but june7 said it better. You cannot just take code that runs in Excel and use it in Access as is. If you need code in both places, you will need 2 entirely different procedures - 1 in Excel written for Excel, 1 in Access written to manipulate Excel. The Access one needs to properly declare, set and use the Excel objects.
    See here http://www.accessmvp.com/kdsnell/EXCEL_MainPage.htm
    The one you want should be "Exporting to Excel..."

  9. #9
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    I think you are missunderstanding what im doing.

    the excel code only ever runs inside excel.

    the only code from access i have shown on here is this:

    Code:
          xl.Run "ThisWorkbook.datarefresh"
       xl.Run "ThisWorkbook.BetterExcelDataToWord"
    Which is part of much longer set of code that I didnt think was relevent.

    I'm a lot closer to resolving the issue. I should be okay.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    If you want Access to call code that is in Excel workbook, still have to declare/set/open Excel objects. You haven't shown any code doing that. Review http://access-excel.tips/run-excel-m...om-access-vba/

    I could call the Excel procedure only if it is in a general module, not behind Workbook or Sheet.
    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.

  11. #11
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    sorry for being so unclear, ill sork on it and make a new post if i fail to achieve wwhat i want to do.

    Thanks.

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

Similar Threads

  1. Query Failing When No Data
    By gazzieh in forum Queries
    Replies: 1
    Last Post: 12-17-2013, 09:42 AM
  2. Replies: 13
    Last Post: 11-14-2013, 04:13 PM
  3. Double dlookup failing
    By JeroenMioch in forum Programming
    Replies: 3
    Last Post: 02-07-2013, 01:50 PM
  4. Export to Excel failing
    By JChalmers in forum Import/Export Data
    Replies: 1
    Last Post: 11-11-2011, 11:02 AM
  5. Append to SQL table failing on data type
    By tpcervelo in forum Queries
    Replies: 2
    Last Post: 10-13-2011, 12:07 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