Error Saving File in Document Folder of Library - vbscript

I am writing an HTA file with VBScript as the scripting language. I have a function where the user is prompted to choose the folder in which they would like to save a document. I didn't write this code myself, but instead borrowed it from here.
Function SelectFolder(myStartFolder)
Dim objFolder, objItem, objShell
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "Select Folder to Save New File", 0, myStartFolder)
If IsObject(objFolder) Then SelectFolder = objFolder.Self.Path
End Function
I call this function in another one in order when I make the file and prompt the user to choose where to save it:
Sub Example()
Dim destPath, destFile, objWorkbook
destPath = SelectFolder(libPath)
destPath = destPath & "\Sample Export"
Set destFile = CreateObject("Excel.Application")
Set objWorkbook = destFile.Workbooks.Add()
objWorkbook.SaveAs(destPath)
(code to edit excel file)
End Sub
Example() works fine except when someone chooses to save their document in one of the four default libraries in Windows (Documents, Music, Pictures, Videos). In that case, I receive an error saying "The file could not be accessed" and indicating the error is at the line that says "objWorkbook.SaveAs(destPath)". The error box then gives me the following suggestions:
Make sure the specified folder exists
Make sure the folder that contains the file is not read-only
Make sure the file name does not contain any of the following characters: < > ? { } : | or *
Make sure the file/path name doesn't contain more than 218 characters.
The error occurs when I open the HTA file and click a button calling Example(), which then opens a dialog box to ask the user to choose the file location. When Documents, Music, Pictures, or Videos is chosen, a "Script Error" box pops up with the error message listed above. I am not familiar enough with VBScript to know what the problem is exactly. I would appreciate any suggestions as to what to do.

I don't know exactly what the solution to the above issue was. However, I noticed that the path I pass to SelectFolder had a typo in it. libPath was supposed to be to the %userprofile% from the environment variable. However, I had pluralized "userprofiles" so that it was %userprofiles% instead. This took me to somewhere that looked right but wasn't. I fixed the typo and now BrowseForFolder takes me to C:/Users/*username* like it's supposed to. It turns out that I was being sent to the Desktop location because of the fourth argument in BrowseForFolder, instead of the user profile like I wanted.

Related

Move files to folders based on first X characters of filename (vbs)

Hello my coding friends.
Sorry to ask this, but I thought it might be quicker to ask if someone has a script like this lying around.
I have about 2000+ files of audio mp3 files logged for a radio station I'm at, and I'd like to put them in to folders according to their recorded log date.
(Yes, I've now fixed the recording to do this correctly from now on, but this is in referent to what I've been doing with it: https://stephenmonro.wordpress.com/2015/05/22/setting-up-an-audio-logger/ )
The files I have are like this: (YYYYMMDD_HH00)
logs\20150424_0300.mp3
logs\20150424_0400.mp3
logs\20150424_0500.mp3
etc.
What I'd like is something like this:
\logs\8 digit date\filename with the same 8 digit date.mp3
Actual
\logs\20150424\20150424_0300.mp3
\logs\20150424\20150424_0400.mp3
\logs\20150424\20150424_0500.mp3
etc.
This is my pseudo code, I've made, but as I'm a little pressed for time and don't have hours to mess around guessing, I just wondered if anyone knew how to do it quickly.
A .VBS file is my prefered language.
Do
Read a filenames first 8 characters {left(8, filename)} (the date)
If not exist, create a folder called that first 8 characters
Move that file into the folder name
Loop (until all files are moved to the right locations)
Your pseudocode looks spot on to me. Assuming every file in your logs folder is consistently named, here's how it could be accomplished using the FileSystemObject library:
Const LOGS_FOLDER = "c:\logs"
Dim objFSO, objFile, strDate, strSub
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(LOGS_FOLDER).Files
strDate = Left(objFile.Name, 8)
strSub = objFSO.BuildPath(LOGS_FOLDER, strDate)
' Create the folder if it doesn't already exist...
If Not objFSO.FolderExists(strSub) Then objFSO.CreateFolder strSub
' Move the file into its proper folder. Use "\" to indicate dest is folder...
objFile.Move strSub & "\"
Next

.VBS get current Windows Media Player song name

Is it possible to get the name of the current song playing in WMP with a VBS script? My goal is to have a .txt file that contains the name of the currently playing song. I'm using a livestream program (OBS) which can display text from a file, and since so many people ask me for my playlist while streaming, I'd like to have OBS display the name of the current song. To accomplish this, I'm just going to have it change the "Current Song" text to whatever is in currentsong.txt located on my Desktop, but I just don't know how update that txt to contain the current song.
I've searched around on Google and I haven't been able to find any way to get the current MWP song :(
Help please :(
Note: Retrieving information on a sequence can be done only when the load status "Transitioning" is reached.
Option Explicit
Dim Sound,Name,NameLog,fso,ws
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject("wscript.Shell")
Set Sound = CreateObject("WMPlayer.OCX.7")
Sound.URL = "C:\FaceSong.mp3"
'Disable the sound
Sound.settings.mute = True
Sound.Controls.play
'Note: Retrieving information on a sequence can be done only when the load status "Transitioning" is reached.
While Sound.playState = 9
Name = Sound.currentMedia.getItemInfo("Name")
NameLog = Name & ".txt"
if fso.FileExists(NameLog) Then
fso.DeleteFile NameLog
end If
MsgBox Name,VbInformation,Name
Call WriteLog(Name,NameLog)
Wend
ws.run NameLog
'***********************************************************************************************
Sub WriteLog(strText,LogFile)
Dim fs,ts
Const ForAppending = 8
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
ts.WriteLine strText
ts.Close
End Sub
'************************************************************************************************
You need to either read Media Player's documentation (so when you looked on the internet you did look at MSDN.com which is where it lives, like all Microsoft's documentation.?). If you have a programming IDE (like VBA in Office) then use it's object browser.
Start an office app, press Alt + F11, then F2.
Right click somewhere and choose References add Windows Media Player from list. Change drop down from All Libraries to just Media Player.
You have a property for filename. And a method to get any info on the song.
Property FileName As String
Member of MediaPlayer.MediaPlayer
Returns or sets the current file name and path
and
Function GetMediaInfoString(MediaInfoType As MPMediaInfoType) As String
Member of MediaPlayer.MediaPlayer
Returns an Information String for the Media
Remember VBS doesn't have access to the type library so you have to use constant's values instead of constant's names. So in the above you woulduse 8 and not mpClipTitle to get the title.

Error in vb script for .xls location finding

I'm facing a problem while writing the vb script for opening a .xls file that is given below..
Dim XLAPP
Set XLAPP = createobject("excel.application")
XLAPP.Visible =true
XLAPP.Workbook.open"d:\book1.xls"
When I run this script the pop window display an error like this:
The test run cannot continue due to an unrecoverable error.
'd:\book1.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.
If you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.
Line (1): "Dim XLAPP".
When I write the script XLAPP.Workbook.Add then it adds an excel file but the above given script is not opening the excel file.
The .Open method is within the Workbooks object collection (plural), not the Workbook object (singular)¹.
XLAPP.Workbooks.open "d:\book1.xls"
fwiw, you may have used the Workbooks collection without thinking about it in XLAPP.Workbooks.Add. The .Add method is not a member of the singular Workbook object and I could not get that to run; Run-time error 424: Object required.
¹ Kudos to Matt Donnan and his comment above for supplying the correct response.

Getting Type mismatch for my function

I am getting an error message i.e.
Type mismatch: 'EMXWEB_IE_LAUNCH'
Line (1): "' ==============================================================================".
the function is
Option Explicit
Public Function EMXWEB_IE_LAUNCH (dicArguments, sErrMsg)
Dim strVersion
Dim strExeVersion
Dim WshShell
Dim strEMXWebBrowserTitleBarText
Dim ie
Const strFunctionName = "EMXWEB_IE_LAUNCH"
Set ie = CreateObject( "InternetExplorer.Application" )
ie.Navigate "www.google.com"
ie.Visible=True
End Function
Could any one let me know where i am wrong and why i am getting this issue
I see errors like this in QTP with "line(1)" and it is sometimes related to a library being loaded previous to the library you have that function in. Missing an end of block or end of function in other library.
Maybe you can relate, but I also see this strange stuff when the QTP's rich text format it saves files in gets corrupted. you can try pasting the entire code into notepad to get plain text, then replace the entire QTP file with the text from notepad and save. This craziness has worked for me several times.

ActiveX, VBScript, Word cannot create file

I am working with an application that uses VBscript/ ActiveX to embed Word in a browser.
This application is not able to create the Word file to edit in the browser if it is not run as an administrator- instead I get and popup from Word saying: "Word could not create the work file. Check the temp environment variable"
I figured this is a permission issue, but I'm not sure how to fix it. First I was wondering where the file is being written to. I'm not sure if it's the TEMP variable for the current user (C:\users\chris\appdata\local\Temp) or the actual environment variable (C:\Windows\TEMP) or the Internet Explorer temporary internet files directory (C:\users\chris\appdata\local\Temp\Temporary Internet Files).
Also, which user would need permission for the correct folder? Would it be the user that is running IE?
I get an error in the browser once the the Word file fails to load saying that the error is on line 60. The following is the relevant code from the page source:
<SCRIPT Language="VBScript">
Sub InitWrapper()
'MsgBox "First line of onLoad"
oframe.EnableFileCommand(0) = False
oframe.EnableFileCommand(1) = False
oframe.EnableFileCommand(2) = False
oframe.EnableFileCommand(3) = False
oframe.EnableFileCommand(4) = False
oframe.CreateNew "Word.Document" --------------------------------This is line 60
oframe.ReadRTF ( "{\rtf1\...fonttbl....*MORE HERE*......
oframe.Activate
End Sub
sub Save_Data
document.THEFORM.content.value = oframe.RTF
end sub
sub InsertString(str)
dim doc
Set doc = oframe.ActiveDocument
doc.ActiveWindow.Selection = str
doc.ActiveWindow.Selection.Collapse wdCollapseEnd
'Paste directly
'doc.ActiveWindow.Selection.Paste
oframe.Activate
document.THEFORM.content.value = oframe.RTF
end sub
sub wrapperUnload()
oframe.Close
End Sub
</SCRIPT>
Can anybody point me in the right direction as to what I should be looking for to fix this problem?
I saw a similar issue, when I am opening a word file embedded in IE browser through activeX, MS word throws the below error message in one of the pop-up boxes
Word could not create the work file. Check the temp environment
variable
This is resolved by adding the web applications/site URL to the Trusted Sites List in the IE Browser Security settings.
Hope this helps.

Resources