Update excel file when opened if a new version is available

user7393973

I have an Excel file which checks its local version with the current version from a database. The code to check the version isn't important to the question.

If there's a new version I want to download it, close the old file (because I can't change/modify it while opened), replace it with the new downloaded version and open the downloaded version.

What I have is something like this:

file.xlsm

Private Sub Workbook_Open()
  Workbooks.Open ThisWorkbook.Path & "\update.xlsm"
End Sub

update.xlsm

Private Sub Workbook_Open()
  Workbooks("file.xlsm").Close
  Dim num As Byte
  Dim WHTTP As Object
  On Error Resume Next
  Set WHTTP = CreateObject("WinHTTPrequest.5")
  If Err.Number <> 0 Then Set WHTTP = CreateObject("WinHTTPrequest.5.1")
  On Error GoTo 0
  WHTTP.Open "GET", "http://path/file.xlsm", False
  WHTTP.Send
  num = FreeFile
  On Error Resume Next
  Open ThisWorkbook.Path & "\file.xlsm" For Binary Access Write As num
  If Err.Number <> 0 Then
    Workbooks(ThisWorkbook.Path & "\file.xlsm").Close
    Open ThisWorkbook.Path & "\File.xlsm" For Binary Access Write As num
  End If
  On Error GoTo 0
  Put num, , WHTTP.ResponseBody
  Close num
  Workbooks.Open ThisWorkbook.Path & "\file.xlsm"
  ThisWorkbook.Close
End Sub

The issue is that since update.xlsm was opened from file.xlsm, once I close file.xlsm, the code from update.xlsm stops running.

I found this thread which is pretty much what I want to do but I couldn't figure out how to get the Application.OnTime working.

Here's where I got the code to download the file.

Edit:

Ok, so I got it almost fully working with the following:

server file.xlsm

Private Sub Workbook_Open()
  'Workbooks.Open ThisWorkbook.Path & "\update.xlsm"
End Sub

local file.xlsm

Private Sub Workbook_Open()
  Workbooks.Open ThisWorkbook.Path & "\update.xlsm"
End Sub

local update.xlsm

ThisWorkbook:

Private Sub Workbook_Open()
  Application.OnTime Now, "test"
End Sub

Module:

Sub test()
  Workbooks("file.xlsm").Close
  Dim num As Byte
  Dim WHTTP As Object
  On Error Resume Next
  Set WHTTP = CreateObject("WinHTTPrequest.5")
  If Err.Number <> 0 Then Set WHTTP = CreateObject("WinHTTPrequest.5.1")
  On Error GoTo 0
  WHTTP.Open "GET", "http://path/file.xlsm", False
  WHTTP.Send
  num = FreeFile
  On Error Resume Next
  Open ThisWorkbook.Path & "\file.xlsm" For Binary Access Write As num
  If Err.Number <> 0 Then
    Workbooks(ThisWorkbook.Path & "\file.xlsm").Close
    Open ThisWorkbook.Path & "\File.xlsm" For Binary Access Write As num
  End If
  On Error GoTo 0
  Put num, , WHTTP.ResponseBody
  Close num
  Workbooks.Open ThisWorkbook.Path & "\file.xlsm"
  If Workbooks.Count = 1 Then
    Application.Quit
  Else
    ThisWorkbook.Close
  End If
End Sub

The problem I'm getting now is the new downloaded file from the server gets corrupted in some way (it works after the message of Excel repairing the file).

Chronocidal

Split the Macro in 2 parts, and use OnTime to trigger the second part first. Here is an example:

Option Explicit

Private Sub Workbook_Open()
    On Error GoTo SkipErr
    Application.OnTime Now(), "ThisWorkbook.Part2" 'Run as soon other macros finish
    Workbooks("file.xlsm").Close
SkipErr:
    MsgBox "file.xlsm was not open...", vbCritical
End Sub

Public Sub Part2()
    MsgBox "This message will show!", vbInformation
End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Refreshing chart when a new file is opened

From Dev

plugin will not offer update even though a new version is available

From Dev

How to make notepad++ start with a blank new file when opened?

From Dev

How to make notepad++ start with a blank new file when opened?

From Dev

Exported excel file from datagridview shows error when opened VB

From Dev

Auto MsgBox when Excel file is opened and a condition is met

From Dev

Update sum when new rows added in Excel

From Dev

When a .csv file is opened in excel and saved as an excel sheet .does it have the same properties as that of an original excel sheet?

From Dev

itunesconnect - 'New Version' button not available

From Dev

A new version of configuration file /etc/default/grub is available, but the version installed currently has been locally modified

From Dev

Chocolatey: How to retain certain version of a package but install new version when available also?

From Dev

Excel file generated by table2excel jQuery plugin throws error when opened

From Dev

Warning: New version of Google Maps SDK for iOS available - How do I update?

From Dev

How to update Docker image when there is new image version?

From Dev

Error when create a new version update of iOS app to iTunes connect

From Dev

Excel VBA Code to select opened Excel file

From Dev

Excel vba userform crashes excel when opened

From Dev

How to eliminate new line character from text file when opened in python?

From Dev

Writing to a file with QFile fails, without error code, when already opened in Excel

From Dev

Old workbook (once opened by VBA) keeps opening when I open another excel file

From Dev

Excel: VBA reports different shape width when the same file is opened on different computers

From Dev

How to update Kubernetes Cluster to the latest version available?

From Dev

redundant 'update is available' message MFP version 8

From Dev

How to update Manjaro to the latest version available?

From Dev

Automatic update of (Excel-DNA) XLL with Excel opened

From Dev

Open new View when new ViewModel is available?

From Dev

Update Manager message "New Hardware Support is Available"

From Dev

Eclipse/LiClipse freezes when .py file is opened

From Dev

There is no such file when picture opened with Image.show()

Related Related

  1. 1

    Refreshing chart when a new file is opened

  2. 2

    plugin will not offer update even though a new version is available

  3. 3

    How to make notepad++ start with a blank new file when opened?

  4. 4

    How to make notepad++ start with a blank new file when opened?

  5. 5

    Exported excel file from datagridview shows error when opened VB

  6. 6

    Auto MsgBox when Excel file is opened and a condition is met

  7. 7

    Update sum when new rows added in Excel

  8. 8

    When a .csv file is opened in excel and saved as an excel sheet .does it have the same properties as that of an original excel sheet?

  9. 9

    itunesconnect - 'New Version' button not available

  10. 10

    A new version of configuration file /etc/default/grub is available, but the version installed currently has been locally modified

  11. 11

    Chocolatey: How to retain certain version of a package but install new version when available also?

  12. 12

    Excel file generated by table2excel jQuery plugin throws error when opened

  13. 13

    Warning: New version of Google Maps SDK for iOS available - How do I update?

  14. 14

    How to update Docker image when there is new image version?

  15. 15

    Error when create a new version update of iOS app to iTunes connect

  16. 16

    Excel VBA Code to select opened Excel file

  17. 17

    Excel vba userform crashes excel when opened

  18. 18

    How to eliminate new line character from text file when opened in python?

  19. 19

    Writing to a file with QFile fails, without error code, when already opened in Excel

  20. 20

    Old workbook (once opened by VBA) keeps opening when I open another excel file

  21. 21

    Excel: VBA reports different shape width when the same file is opened on different computers

  22. 22

    How to update Kubernetes Cluster to the latest version available?

  23. 23

    redundant 'update is available' message MFP version 8

  24. 24

    How to update Manjaro to the latest version available?

  25. 25

    Automatic update of (Excel-DNA) XLL with Excel opened

  26. 26

    Open new View when new ViewModel is available?

  27. 27

    Update Manager message "New Hardware Support is Available"

  28. 28

    Eclipse/LiClipse freezes when .py file is opened

  29. 29

    There is no such file when picture opened with Image.show()

HotTag

Archive