Results 1 to 2 of 2

Thread: Excel VBA Patch Workbook

  1. #1

    Thread Starter
    Lively Member fba1's Avatar
    Join Date
    Aug 2007
    Posts
    119

    Excel VBA Patch Workbook

    Hello again been a while since I've posted, but been a little busy with things (kid should be ready to pop out in a few months ) Created a little script to update a program I created for work but it just does not run the way I want it to thought I would post it to see if anyone can tell me what I'm doing wrong.

    The code btw does almost completely work the only part it gets stuck on is closing the previous workbook and then deleting it.

    Here it is:

    On Workbook Open Code:
    1. Private Sub Workbook_Open()
    2. On Error Resume Next
    3. Workbooks(ThisWorkbook.Path & "\heatfinderold.xls").Close
    4. On Error Resume Next
    5. Kill (ThisWorkbook.Path & "\heatfinderold.xls")
    6. UserForm1.Show
    7. Heat.Show
    8. Run "macro2"
    9. End Sub

    Macro2 Code:
    1. Private Sub macro2()
    2. Application.AutomationSecurity = msoAutomationSecurityLow
    3.     'Microsoft Scripting Runtime
    4.     Dim fso As New FileSystemObject
    5.     Dim wFile As File    'Handle for fso 'File' Object
    6.     Dim wPath As String
    7.     Dim wName As String
    8.     Dim cwb
    9.    
    10.     'File name's and path
    11.     wPath = "H:\"
    12.     wName = "heatfinder2.xls"
    13.    
    14.     If fso.FileExists(wPath & wName) Then
    15.         Set wFile = fso.GetFile(wPath & wName)     'Setting Handle
    16.     End If
    17.    
    18.     If wFile.DateCreated > ThisWorkbook.BuiltinDocumentProperties("Creation Date") Then Updater = MsgBox("There is an update available for this application would you like to download it?", vbYesNo, "Update Found") Else Exit Sub
    19.     Application.DisplayAlerts = False
    20.     If Updater = vbYes Then ThisWorkbook.SaveAs ThisWorkbook.Path & "\heatfinderold.xls" Else Exit Sub
    21.     If Updater = vbYes Then FileCopy "H:\heatfinder2.xls", ThisWorkbook.Path & "\heatfinder2.xls" Else Exit Sub
    22.    
    23.    
    24.      'Clean House
    25.     Set wFile = Nothing
    26.     Workbooks.Open (ThisWorkbook.Path & "\heatfinder2.xls")
    27.     Application.DisplayAlerts = True
    28. End Sub

    Also does anyone know a better way to find the path of the document besides ThisWorkbook.Path Ive found that it does not work if the document is in the users My Documents folder in which it just saves in the folder above.

    Thanks again for any ideas

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA Patch Workbook

    the close command only requires the workbook name, not the path

    take out the on error resume next statements then the errors will show up, do proper error handling unless you know you are going to get a specific error you need to ignore, then set back to proper error handling
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width