|
-
Jan 23rd, 2008, 06:28 AM
#1
Thread Starter
Lively Member
[RESOLVED] VBA to VBScript
I've been trying to develop a File distribution script, with some success, I've managed to get the script to copy one file and then distribute it into the many folders, but now I'd like it to record the folders it has skipped due to errors IE: Error 76 "File path not found". But I can't get it to work, Any suggestions?
Code:
Workbooks.Add
Range("A1").Value = "Folders Omitted"
Range("A1").Font.Bold = True
ActiveCell.Offset(1, 0).Select
Dim fso, f, fc, f1, SourceFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set SourceFile = fso.GetFile("E:\Test Area\NormalUpdates\NormalPM\normal.dot")
Set f = fso.Getfolder("E:\Test Area\")
Set fc = f.SubFolders
For Each f1 In fc
If InStr(f1.Name, ",") > 0 Then
On Error Resume Next
SourceFile.Copy f1 & "\Databases\normal_PM\"
If Err.Number <> 0 Then
Select Case Err.Number ' Evaluate error number.
Case 76 ' "Path not found" error.
ActiveCell.Value = f1
ActiveCell.Offset(1, 0).Select
'LogInformation f1
Err.Clear
Case Else
ActiveCell.Value = f1
ActiveCell.Offset(1, 0).Select
End Select
Else
End If
On Error GoTo 0
End If
Next
Objexcel.ActiveWorkbook.SaveAs("E:\Test Area\Noticeboard\NormalUpdates\omitted files.xls")
ObjWorkbook.Close
Last edited by Hack; Jan 23rd, 2008 at 07:14 AM.
Reason: Added Code Tags
-
Jan 23rd, 2008, 07:14 AM
#2
Re: VBA to VBScript
Are you trying to get this to work in Excel VBA or VBScript?
-
Jan 23rd, 2008, 07:29 AM
#3
Thread Starter
Lively Member
Re: VBA to VBScript
Need it to run via VBScript - .vbs file so as to run with Win server 2003 scheduled tasks over night.
-
Jan 23rd, 2008, 10:18 AM
#4
Re: VBA to VBScript
Have you checked to see if it does step into the 'Case 76' part in code?
-
Jan 23rd, 2008, 10:25 AM
#5
Thread Starter
Lively Member
Re: VBA to VBScript
Thats part of the problem, when I run it through VBA, the full script works like a charm, as soon as I change it to VB Script (.vbs) file, it doesn't. I think it's conflict between VBA Labels and VB script, but I'm not getting any error messages.
-
Jan 23rd, 2008, 11:47 AM
#6
Re: VBA to VBScript
But if you are doing this in VB Script then you would need to create the Excel objects as in Excel VBA its already created.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 23rd, 2008, 11:54 AM
#7
Thread Starter
Lively Member
Re: VBA to VBScript
You wouldn't have the VB Script code for creating Excel objects would you?
-
Jan 23rd, 2008, 12:22 PM
#8
Re: VBA to VBScript
It should be using Late Binding techniques but Im not fluent with VBS.
Just
CreateObject("Excel.Application")
to start an instance of Excel.
Declare your workbook object variable as Object
Declare your Excel constants
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 24th, 2008, 07:22 AM
#9
Thread Starter
Lively Member
Re: VBA to VBScript
I'm getting this all wrong, only just realised, I was asking the script to copy a file and distribute it into multipule folders and then log the ones it's skipped (for what ever reason, as long as it tells us) into an Excel file. Just bleedin dawned on me that our server doesn't have MS Office on it, the script would never have work properly from the start. Duurrr.....
If anyone knows the script code for entering in the skipped file paths into a .txt or .rtf that would be fantastic!
Last edited by Kubull; Jan 24th, 2008 at 12:13 PM.
-
Jan 24th, 2008, 03:48 PM
#10
Re: VBA to VBScript
use the existing file scripting object to write a text file
vb Code:
Set f1 = fs.CreateTextFile("C:\Documents and Settings\peter\My Documents\basic\vbs\mylog.txt", True) f1.Write mystr f1.Close
add all the failed installs to a string variable (mystr), (separated by vbnewline) during the for loop then write the file before destoying your fso object
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
-
Jan 25th, 2008, 05:25 AM
#11
Thread Starter
Lively Member
Re: VBA to VBScript
Without sounding dumb (which I probably will) but not sure what you mean by "add all the failed installs to a variable (mystr)" do you mean change all the "ActiveCell.Value = f1" inputs?
-
Jan 25th, 2008, 05:41 AM
#12
Re: VBA to VBScript
vb Code:
If InStr(f1.Name, ",") > 0 Then On Error Resume Next SourceFile.Copy f1 & "\Databases\normal_PM\" If Err.Number <> 0 Then Select Case Err.Number ' Evaluate error number. Case 76 ' "Path not found" error. 'ActiveCell.Value = f1 mystr = mystr & f1 & " path not found" & vbnewline ' add to string here 'ActiveCell.Offset(1, 0).Select 'LogInformation f1 Err.Clear Case Else mystr = mystr & " " & err.description & vbnewline ' or here 'ActiveCell.Value = f1 'ActiveCell.Offset(1, 0).Select End Select Else End If On Error GoTo 0 End If Next Set f1 = fso.CreateTextFile("C:\Documents and Settings\peter\My Documents\basic\vbs\mylog.txt", True) f1.Write mystr 'write string to file f1.Close set f1 = nothing 'destroy objects set fso = nothing
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
-
Jan 25th, 2008, 06:02 AM
#13
Re: VBA to VBScript
Your code should be indented, it will be easier to read.
Try this: (I cannot test it)
Code:
Option Explicit
Dim xlApp, wb, aCell
Dim fso, f, f1, SourceFile
Set xlApp = CreateObject("Excel.Application")
Set wb = Workbooks.Add
Set aCell = wb.Sheets(1).Range("A1")
aCell.Value = "Folders Omitted"
aCell.Font.Bold = True
Set fso = CreateObject("Scripting.FileSystemObject")
Set SourceFile = fso.GetFile("E:\Test Area\NormalUpdates\NormalPM\normal.dot")
Set f = fso.Getfolder("E:\Test Area\")
For Each f1 In f.SubFolders
If InStr(f1.Name, ",") > 0 Then
Set aCell = aCell.Offset(1, 0)
aCell.Value = f1.Name
On Error Resume Next
SourceFile.Copy f1 & "\Databases\normal_PM\"
If ErNum <> 0 Then
aCell.Offset(0, 1).Value = Err.Description
Err.Clear
End If
On Error GoTo 0
End If
Next
wb.SaveAs ("E:\Test Area\Noticeboard\NormalUpdates\omitted files.xls")
wb.Close
xlApp.Quit
-
Jan 25th, 2008, 06:25 AM
#14
Re: VBA to VBScript
@ anhn
he does not have excel installed, so needs to save errors to file
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
-
Jan 25th, 2008, 06:30 AM
#15
Thread Starter
Lively Member
Re: VBA to VBScript
Westconn1, You are a God! the script works like a charm! Many thanks for the help! gimmie a PO Box address and there'll be a case of Beer waiting for ya!
-
Jan 25th, 2008, 06:41 AM
#16
Re: VBA to VBScript
 Originally Posted by westconn1
@ anhn
he does not have excel installed, so needs to save errors to file
I didn't know that. I thought he wanted the logs in Excel.
Anyway save the logs to a text file is quicker, but look at my For Loop, you should capture any error, not just error 76.
-
Jan 25th, 2008, 06:49 AM
#17
Re: VBA to VBScript
see post #9
i thought i had caught all other errors, in case else
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
-
Jan 25th, 2008, 07:03 AM
#18
Re: VBA to VBScript
But why you have to split 76 and others? No need to use Case statement, the same manner : the file cannot be coppied.
"Path not found" is the Err.Description of Err 76.
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
|