Forum Home
Press F1
 
Thread ID: 109138 2010-04-26 03:08:00 VB Script variable not populating jaketrsw (15733) Press F1
Post ID Timestamp Content User
880021 2010-04-26 03:08:00 I am trying to put a simple check for a file extension at the beginning of a script that prints documents for me . I want the script to run a check for . xls files and populate a variable which can be used in an If statement to trigger a different method for those files .

I found this piece of script online that should take the filename extension and populate the variable getextension .

Public Function gogetextension(filename)

Dim getextension

For i = Len(filename) To 2 Step -1
c = Mid(filename, i, 1)
If c = " . " Then
pos = i + 1
End If
Next

getextension = Mid(filename, pos, (Len(filename) + 1 - pos))
End Function

problem is that it doesn ' t populate the getextension variable - when I call that variable in a subsequent if statement the variable is blank .

I suspect it is because the script above is not actually checking the file properly or isn ' t being triggered at all . The vb script is triggered from a batch file that uses a for /f statement to produce the following command for a list of files taken from a . txt file:

" r:\print . vbs " " h:\printtest . xls "
which if I understand correctly triggers the vb code and feeds it the filename .

The full vb script is below for reference .

<print . vbs>
Public Function gogetextension(filename)

Dim getextension

For i = Len(filename) To 2 Step -1
c = Mid(filename, i, 1)
If c = " . " Then
pos = i + 1
End If
Next

getextension = Mid(filename, pos, (Len(filename) + 1 - pos))

End Function

Msgbox getextension

If getextension = xls Then

Dim intRC, objExcel, strXlsFile

With WScript . Arguments
If . Unnamed . Count = 1 Then
strXlsFile = WScript . Arguments . Unnamed(0)
Else
Syntax
End If
If . Named . Count > 0 Then Syntax
End With

Set objExcel = CreateObject( " Excel . Application " )

objExcel . WorkBooks . Open( strXlsFile )

objExcel . Visible = False
objExcel . ScreenUpdating = False

objExcel . ActiveWorkbook . PrintOut

objExcel . ActiveWorkbook . Close 0

objExcel . Quit

Set objExcel = Nothing


Sub Syntax
Dim strMsg
strMsg = " PrintDoc . vbs, Version 1 . 00 " _
& vbCrLf _
& " Print a specified MS Word document on the default printer " _
& vbCrLf & vbCrLf _
& " Usage: PRINTDOC . VBS word_document " _
& vbCrLf & vbCrLf _
& " Note: This script requires MS Word to print the document . " _
& vbCrLf & vbCrLf _
& " Based on a script by Arnout van der Vorst " _
& vbCrLf _
& " arnoutvandervorst . blogspot . com/2008/03/vbscript-to-automatically-insert . html " _
& vbCrLf & vbCrLf _
& " Written by Rob van der Woude " _
& vbCrLf _
& " http://www . robvanderwoude . com "
WScript . Echo strMsg
WScript . Quit 1
End Sub

Else

Msgbox " Hi "


' Check the command line arguments
If WScript . Arguments . Unnamed . Count <> 1 Then Syntax
If WScript . Arguments . Named . Count > 0 Then Syntax

' Check if a valid file was specified
Set objFSO = CreateObject( " Scripting . FileSystemObject " )
strFile = WScript . Arguments(0)
If Not objFSO . FileExists( strFile ) Then Syntax
strFolder = objFSO . GetParentFolderName( strFile )
Set objFSO = Nothing

' Open the Shell Folders object
Set objShell = CreateObject( " Shell . Application " )

' Create an object for the specified file ' s parent folder
Set objFolder = objShell . Namespace( strFolder )

' Create a collection for the folder ' s contents
Set colFiles = objFolder . Items

' Loop through the collection to find the file specified
If colFiles . Count > 0 Then
For Each objFile In colFiles
If LCase( objFile . Path ) = LCase( strFile ) Then
' Print the file with its associated print command
objFile . InvokeVerbEx( " Print " )
End If
Next
End If


Sub Syntax
Dim strMsg
strMsg = " Print . vbs, Version 1 . 00 " _
& vbCrLf _
& " Print a file - ANY file - on the default printer " _
& vbCrLf & vbCrLf _
& " Usage: " & UCase( WScript . ScriptName ) & " filename " _
& vbCrLf & vbCrLf _
& " Where: " " filename " " specifies the file to be printed (no wildcards) " _
& vbCrLf & vbCrLf _
& " Notes: This script will only work if a print command for the " _
& vbCrLf _
& " file ' s associated file type is defined in the registry . " _
& vbCrLf _
& " When the associated program is used to open and print " _
& vbCrLf _
& " the file, the program will not be closed automatically . " _
& vbCrLf _
& " This script may conflict with my DefOpen . bat script . " _
& vbCrLf & vbCrLf _
& " Written by Rob van der Woude " _
& vbCrLf _
& " http://www . robvanderwoude . com "
WScript . Echo strMsg
WScript . Quit 1
End Sub

End If
<print . vbs>
jaketrsw (15733)
880022 2010-04-26 03:54:00 I can't see anywhere in the script where the "gogetextension" function is called. inphinity (7274)
1