|
-
Feb 2nd, 2008, 04:08 AM
#1
Thread Starter
Lively Member
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:
Private Sub Workbook_Open()
On Error Resume Next
Workbooks(ThisWorkbook.Path & "\heatfinderold.xls").Close
On Error Resume Next
Kill (ThisWorkbook.Path & "\heatfinderold.xls")
UserForm1.Show
Heat.Show
Run "macro2"
End Sub
Macro2 Code:
Private Sub macro2()
Application.AutomationSecurity = msoAutomationSecurityLow
'Microsoft Scripting Runtime
Dim fso As New FileSystemObject
Dim wFile As File 'Handle for fso 'File' Object
Dim wPath As String
Dim wName As String
Dim cwb
'File name's and path
wPath = "H:\"
wName = "heatfinder2.xls"
If fso.FileExists(wPath & wName) Then
Set wFile = fso.GetFile(wPath & wName) 'Setting Handle
End If
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
Application.DisplayAlerts = False
If Updater = vbYes Then ThisWorkbook.SaveAs ThisWorkbook.Path & "\heatfinderold.xls" Else Exit Sub
If Updater = vbYes Then FileCopy "H:\heatfinder2.xls", ThisWorkbook.Path & "\heatfinder2.xls" Else Exit Sub
'Clean House
Set wFile = Nothing
Workbooks.Open (ThisWorkbook.Path & "\heatfinder2.xls")
Application.DisplayAlerts = True
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
-
Feb 2nd, 2008, 05:00 AM
#2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|