Results 1 to 18 of 18

Thread: [RESOLVED] VBA to VBScript

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    Resolved [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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VBA to VBScript

    Are you trying to get this to work in Excel VBA or VBScript?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    Re: VBA to VBScript

    Need it to run via VBScript - .vbs file so as to run with Win server 2003 scheduled tasks over night.

  4. #4
    Hyperactive Member
    Join Date
    Jan 2008
    Location
    Merseyside
    Posts
    456

    Re: VBA to VBScript

    Have you checked to see if it does step into the 'Case 76' part in code?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    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.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    Re: VBA to VBScript

    You wouldn't have the VB Script code for creating Excel objects would you?

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    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.

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

    Re: VBA to VBScript

    use the existing file scripting object to write a text file
    vb Code:
    1. Set f1 = fs.CreateTextFile("C:\Documents and Settings\peter\My Documents\basic\vbs\mylog.txt", True)
    2. f1.Write mystr
    3. 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

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    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?

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

    Re: VBA to VBScript

    vb Code:
    1. If InStr(f1.Name, ",") > 0 Then
    2. On Error Resume Next
    3. SourceFile.Copy f1 & "\Databases\normal_PM\"
    4. If Err.Number <> 0 Then
    5. Select Case Err.Number ' Evaluate error number.
    6. Case 76 ' "Path not found" error.
    7. 'ActiveCell.Value = f1
    8. mystr = mystr & f1 & " path not found" & vbnewline  ' add to string here
    9. 'ActiveCell.Offset(1, 0).Select
    10. 'LogInformation f1
    11. Err.Clear
    12. Case Else
    13. mystr = mystr & " " & err.description & vbnewline   ' or here
    14. 'ActiveCell.Value = f1
    15. 'ActiveCell.Offset(1, 0).Select
    16. End Select
    17. Else
    18. End If
    19. On Error GoTo 0
    20. End If
    21. Next
    22.        Set f1 = fso.CreateTextFile("C:\Documents and Settings\peter\My Documents\basic\vbs\mylog.txt", True)
    23.       f1.Write mystr               'write string to file
    24.       f1.Close
    25. set f1 = nothing    'destroy objects
    26. 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

  13. #13
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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

    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

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    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!

  16. #16
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: VBA to VBScript

    Quote 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.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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

    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

  18. #18
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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