+ Post New Thread
Results 1 to 8 of 8
Scripts Thread, Problem with MsgBox and Variables in Coding and Web Development; Ok people I am learning vbs coding from looking at others and then tweaking them to suit my purposes. I ...
  1. #1

    LeightonJames's Avatar
    Join Date
    Sep 2009
    Posts
    634
    Thank Post
    26
    Thanked 92 Times in 60 Posts
    Rep Power
    268

    Problem with MsgBox and Variables

    Ok people

    I am learning vbs coding from looking at others and then tweaking them to suit my purposes. I learn best this way you see. The problem I have is this.

    I have located a vbs script to locate the shortcuts in a folder and return them and their targets and list them in a text file. It works perfectly however it is not very user friendly as you have to edit the script to change certain variables. To that end I have included an Input Box to ask the user where they want to scan and for the time being left the target file as a hardcoded location. Now the problem comes when including both these variables in a confirmation MsgBox.

    This works:

    Code:
    'LocateSC.vbs
    
    '' /// Setup environment declarations and object references ///
    Dim path
    Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objShell:Set objShell = CreateObject("Wscript.Shell")
    Dim objFolder:Set objFolder = objFSO.GetFolder(InputBox ("Enter the full path to the folder","Enter Full Path","C:\test"))
    Dim objOutput:Set objOutput = objFSO.CreateTextFile("C:\Shortcuts.txt")
    
    If MsgBox("This program will scan for shortcuts in your selected location - " & objFolder _
      & vbNewline & vbNewline & "It will then list them in a text file under the following path - " _
      & vbNewline & vbNewline & "Click Ok to Proceed.", vbOkCancel, "Shortcut Analyser") _
      = vbOk Then
    This doesn't

    Code:
    'LocateSC.vbs
    
    '' /// Setup environment declarations and object references ///
    Dim path
    Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objShell:Set objShell = CreateObject("Wscript.Shell")
    Dim objFolder:Set objFolder = objFSO.GetFolder(InputBox ("Enter the full path to the folder","Enter Full Path","C:\test"))
    Dim objOutput:Set objOutput = objFSO.CreateTextFile("C:\Shortcuts.txt")
    
    If MsgBox("This program will scan for shortcuts in your selected location - " & objFolder _
      & vbNewline & vbNewline & "It will then list them in a text file under the following path - " & objOutput _
      & vbNewline & vbNewline & "Click Ok to Proceed.", vbOkCancel, "Shortcut Analyser") _
      = vbOk Then
    If I try to run the second one I get the following error box:

    Script: (path to script)
    Line: 10
    Char: 1
    Error: Object doesn't support this property or method
    Code: 800A01B6
    Source Microsoft VBScript runtime error

    Now I know that this means the error is on line 10 and that is the MsgBox code but what I don't understand is why if I remove the objOutput variable from the MsgBox script it works fine. If needed I can provide my script in it's current form.

    Thanks for looking people

  2. #2
    sister_annex's Avatar
    Join Date
    Jan 2009
    Location
    Wolverhampton
    Posts
    616
    Thank Post
    103
    Thanked 144 Times in 125 Posts
    Rep Power
    52
    OK as a total guess (not done vbs for years) it looks like your objOutput is creating a text file, I dont think that you can then use that declaration to give back information.

    I'm not sure what you are attempting to do but if it is just displaying the path to the folder, why not create this as a variable that you can pass to the CreateTextFile method and then pass the variable into the message box?

  3. #3

    LeightonJames's Avatar
    Join Date
    Sep 2009
    Posts
    634
    Thank Post
    26
    Thanked 92 Times in 60 Posts
    Rep Power
    268
    Ok, yep. That makes sense. As soon as i read your first sentence i knew you were right.

    Thanks, will post back if it doesn't work

  4. #4

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,405
    Thank Post
    109
    Thanked 267 Times in 198 Posts
    Rep Power
    169
    From what I can see you are trying to write a textstream object to the msgbox rather than a path...

    If you store the return from the InputBox in a variable you can use that to create the file and write the path to the msgbox...

    [EDIT] Doh, got interrupted while typing my response and the got beaten to it... [/EDIT]

  5. #5

    LeightonJames's Avatar
    Join Date
    Sep 2009
    Posts
    634
    Thank Post
    26
    Thanked 92 Times in 60 Posts
    Rep Power
    268
    Ok, you can tell i'm a complete novice now

    I have attached my current code as i'm still having problems with it

    Shortcut Lister test.vbs

    I hope someone can help me

  6. #6

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,824
    Thank Post
    372
    Thanked 544 Times in 507 Posts
    Rep Power
    184
    Quote Originally Posted by LeightonJames View Post
    Ok, you can tell i'm a complete novice now

    I have attached my current code as i'm still having problems with it

    Shortcut Lister test.vbs

    I hope someone can help me
    You're not passing it a path, You're passing a string.

    Simple example:

    Code:
    Dim objfso, objshell, objfolder, path, objoutput
    
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("Wscript.Shell")
    Set objFolder = objFSO.GetFolder(InputBox ("Enter the full path to the folder","Enter Full Path","C:\test"))
    path = InputBox("Enter the full path to the target file: ","Enter Full Path","C:\shortcuts.txt")
    
    FullPath = objFSO.BuildPath(path, "")
    
    Set objOutput = objFSO.CreateTextFile(FullPath)
    Steve

  7. #7

    LeightonJames's Avatar
    Join Date
    Sep 2009
    Posts
    634
    Thank Post
    26
    Thanked 92 Times in 60 Posts
    Rep Power
    268
    Thank You Thank You Thank You Thank You Steve

    I understand now, sorry if i was coming across as an idiot. First time doing anything like this but i'm enjoying it none the less.

    Thanks again

  8. #8

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,824
    Thank Post
    372
    Thanked 544 Times in 507 Posts
    Rep Power
    184
    Quote Originally Posted by LeightonJames View Post
    Thank You Thank You Thank You Thank You Steve

    I understand now, sorry if i was coming across as an idiot. First time doing anything like this but i'm enjoying it none the less.

    Thanks again
    No prob, If you get stuck on the rest just shout and I'll see what I can do, but hopefully that'll sort the rest.

    Steve



SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 2
    Last Post: 4th December 2008, 12:23 PM
  2. Problems with wireless and an HP Laptop
    By mark in forum Windows Vista
    Replies: 5
    Last Post: 31st December 2007, 06:56 PM
  3. Replies: 11
    Last Post: 6th October 2007, 05:27 PM
  4. LTSP Problems with TFTP and PXE boot
    By Joedetic in forum Thin Client and Virtual Machines
    Replies: 3
    Last Post: 18th May 2006, 05:40 PM
  5. Replies: 4
    Last Post: 10th October 2005, 11:12 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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