how to do reverse index in VBA - vba

I don't know anything about VBA but I need to get the file name out of a file full path.
So for a string like "c:\something\somethingelse\file.name" I need to get "file.name" out. Also how can you get a quote in a string such as " " "? backslash doesn't seem to work.

To get a quote in a string use two quotes ("")
To find the location of the slash from the end, use InstrRev()

the DIR() function will return the filename.
You need to add a reference to the Microsoft Scripting runtime.

Related

Multiple Fields for Hyperlink Expression

I am building an expression for a hyperlink in SSRS. I can currently get it to point to the correct directory, but also need it to point to the correct file within the directory and I'm not sure how to include multiple fields. I'm sure its something simple that I'm missing. Would appreciate any help you all can provide. Code is below that gets to me to the directory, need to add code to get to the FileName which is the actual file. Thanks in advance.
="file://phpfile01/analyst/AMHPData/Exports/"&Fields!XML_EXPORT_DIRECTORY.Value
You add more fields to the expression with the &:
="file://phpfile01/analyst/AMHPData/Exports/" & Fields!XML_EXPORT_DIRECTORY.Value & "/" & Fields!FileName.Value
The extra slash may not be needed if it's included in your XML_EXPORT_DIRECTORY field.
If not if would be:
="file://phpfile01/analyst/AMHPData/Exports/" & Fields!XML_EXPORT_DIRECTORY.Value & Fields!FileName.Value

System.ArgumentException: 'Illegal characters in path.' Error

Please help. I have a piece of code that's already working in other parks of my program, however fails to work when accessed by a certain form so i can't see there can be an error with it. Its an information storage project using text files. A screenshot of the exact code and the error:
I expected it to change the label text to the contents of the text file its trying to read.
Thanks everyone :)
Well, there must be one or more illegal characters coming from your "zoots1.txt" file!
Build the filename and see what it looks like:
Dim zoot1s As String
zoot1s = My.Computer.FileSystem.ReadAllText("zoot1s.txt")
Dim fileName As String
fileName = zoot1s + "c.txt"
MessageBox.Show(fileName)
Dim ClassStrain As String
ClassStrain = My.Computer.FileSystem.ReadAllText(fileName)
TempLabel3.Text = ClassStrain
Timer1.Start()
--- EDIT --
My bad. I Found the issue to be that there is a skip in text where it goes to a new line. As if i had added vbNewline to it. Is there any way to edit the text file and take away the last character so there isnt a new line.
Use the Trim() function to get rid of white space. Also use Path.Combine() to make sure the path is correctly separated from the filename with the correct number of backslashes:
zoot1s = My.Computer.FileSystem.ReadAllText("zoot1s.txt").Trim()
Dim fileName As String = System.IO.Path.Combine(zoot1s, "c.txt")
I think you just missed the most important thing, which is the whole specific path of your file that will be reading data from, specifically after the ReadAllText() method, so instead of this line:
zoom1s=My.computer.FileSystem.ReadAllText('zoot1s.txt")
You should edit it like this:
zoom1s=My.computer.FileSystem.ReadAllTex(My.Computer.FileSystem.CurrentDirectory & \zoot1s.txt")
I hope this can solve your problem.
^_^

VBA Hyperlinks.Add method prepending local folder to Address

I've been lurking here for a while but this is my first post so let me know if I need to change something. Anyways, here goes:
I'm trying to create a macro that will add hyperlinks to cells in a worksheet. The problem is that after running the macro, I notice that the folder location of my spreadsheet has been prepended to the address that I specified. Is there something I need to do in order to indicate that this is a webpage and not a local file? Excerpt from the macro is below.
Dim IGQ As Range
Dim IGQno As String
Dim IGQno1 As String
For Each IGQ In Range("A2:A10") 'Actual range is much larger
IGQno = IGQ.Value
IGQno1 = Left(IGQ, 1)
Sheets("Cameron DCDA").Hyperlinks.Add Anchor:=IGQ, _
Address:="""http://xxxx""&IGQno1&""xxx""&IGQno&""xxxxx""" 'It's a company website so they probably don't want me to share it
Next
The result is that a hyperlink is created for each cell but it links to file:///C:\Users\John.Doe\Documents\"http://xxxx"&IGQno1&"xxx"&IGQno&"xxxxx"
I've tried using fewer quotation marks in the address since it seems like overkill but I get the compile error "Expected: end of statement"
Do you guys have any suggestions?
Too many quotes
Address:="http://xxxx " & IGQno1 & "xxx" & IGQno & "xxxxx"
Also - be sure to leave a space before your & otherwise it will be interpreted as a variable type suffix:
What are possible suffixes after variable name in VBA?

JavaScript Eval to replace empty spaces with <SP>, literal

I am using iMacros & VBA. I created a string in VBA that replaces empty spaces with <SP> so I can pass it to an iMacros script. The problem is iMacros takes the <SP> as a space and not as a string. I tried to tweak VBA but when its passed to iMacros as a variableits just empty spaces.
i figured I try to do an eval in iMacros directly but am stuck with the proper syntax.
Set Name "Chris Tony James"
Set newName EVAL("\"{{Name}}\".replace(/ //g, \"<SP>\";")
i have tried a million varieties, even using split.join. I just cant get it.
thanks for any help
Before: Chris Tony James
After: Chris<SP>Tony<SP>James
It must be something like that:
Set newName EVAL("\"{{Name}}\".replace(/\s+/g, \"<SP>\";")
Set Name Chris<SP>Tony<SP>James
Is all that is required to use <SP> in place of spaces in a variable in iMacros. Omit the " marks.

How to specify a path with spaces for StreamWriter

In VB.Net, how do I provide the StreamWriter constructor with a path that includes spaces? StreamWriter("""C:\Users\Public\Public Users\file.txt""") does not work.
Here is a working code example:
Dim fs As New System.IO.StreamWriter("e:\test 123.txt")
fs.Write("hello")
fs.Close()
UPDATE:
The new example for folder with space(s):
'this is your filename
Dim Filename As String = "e:\folder with space\test 123.txt"
'this is your folder
Dim Folderpath As String = System.IO.Path.GetDirectoryName(Filename)
'now do checking if the folder exists, if not create the folder
If System.IO.Directory.Exists(Folderpath) = False Then
System.IO.Directory.CreateDirectory(Folderpath)
End If
'now create the file as usual
Dim fs As New System.IO.StreamWriter("e:\folder with space\test 123.txt")
fs.Write("hello")
fs.Close()
The reason for your code didn't compile because you have not create the folder before creating the file, ie that folder must be existed before you can create your file.
You don't put quotes around the string you pass to the StreamReader constructor. Quotes are only used when you use, say, the command line. Or anything else that uses spaces as separators between arguments. The program requires those double quotes to recognize an argument with an embedded space.
Not necessary here, there's no ambiguity since the argument only takes the path to a single file. The only exception to that rule that I know of is the ProcessStartInfo.Arguments property.
So, just put single double quotes around the string, the syntax that the compiler requires. Your real problem is the name of the folder. Windows Explorer shows a different name for the folders in c:\users\public. For example c:\users\public\videos is displayed in the Explorer window as "Public Videos". It's trying to be helpful by expanding the abbreviated name. Your program however has to use the real folder name. Which is probably "users", not "Public Users". To find out for sure, use the command line (cmd.exe). Use cd \users\public and dir /a.
Last but not least, that folder has a different name on different versions of Windows. You should use Environment.GetFolderPath(). "Public Users" isn't a standard folder name however, not sure why you are using it.