![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | How to concatenate a literal string and a variable value? Assume I want to concatenate a literal text value and a variable value for a function parameter then the following does NOT work: objWorkbook.SaveAs("D:\work\v1_" + Filename) The variable "Filename" is filled with a valid value. So how do I otherwise concatenate two parts? Tony |
My System Specs![]() |
| | #2 (permalink) |
| | Re: How to concatenate a literal string and a variable value? "Tony Bansten" <tonytony@xxxxxx> wrote in message news:48e51d7f$0$6614$9b4e6d93@xxxxxx-online.net... Quote: > Assume I want to concatenate a literal text value and a variable value for > a function parameter > then the following does NOT work: > > objWorkbook.SaveAs("D:\work\v1_" + Filename) > > The variable "Filename" is filled with a valid value. > > So how do I otherwise concatenate two parts? > > Tony > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: How to concatenate a literal string and a variable value? What is the object type for "FileName" Do you have OPTION EXPLICIT ON OPTION STRICT ON or do you have dim Filename (which means dim Filename as object) ............ or do you have ? dim fileName as string Are you using VB.NET? If you're not, then why are you posting to a VB.NET newsgroup (microsoft.public.dotnet.languages.vb)? "Tony Bansten" <tonytony@xxxxxx> wrote in message news:48e51d7f$0$6614$9b4e6d93@xxxxxx-online.net... Quote: > Assume I want to concatenate a literal text value and a variable value for > a function parameter > then the following does NOT work: > > objWorkbook.SaveAs("D:\work\v1_" + Filename) > > The variable "Filename" is filled with a valid value. > > So how do I otherwise concatenate two parts? > > Tony > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: How to concatenate a literal string and a variable value? "Tony Bansten" <tonytony@xxxxxx> wrote in message news:48e51d7f$0$6614$9b4e6d93@xxxxxx-online.net... Quote: > Assume I want to concatenate a literal text value and a variable value for > a function parameter > then the following does NOT work: > > objWorkbook.SaveAs("D:\work\v1_" + Filename) > > The variable "Filename" is filled with a valid value. > > So how do I otherwise concatenate two parts? > > Tony > work. If fails if the values are numeric, in which case it performs addition, which is why "&" is preferred. It may be the result of the concatenation is invalid. That is, if Filename = "test.txt", the result of the concatenation will be "D:\work\v1_test.txt". I would troubleshoot with code similar to: strFile = "D:\work\v1_" & Filename Wscript.Echo strFile objWorkbook.SaveAs(strFile) -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net -- |
My System Specs![]() |
| | #5 (permalink) |
| | Re: How to concatenate a literal string and a variable value? "Tony Bansten" <tonytony@xxxxxx> schrieb: Quote: > Assume I want to concatenate a literal text value and a variable value for > a function parameter > then the following does NOT work: > > objWorkbook.SaveAs("D:\work\v1_" + Filename) > > The variable "Filename" is filled with a valid value. method should be passed 'ByVal'), but note that VB has its dedicated string concatenation operator '&'. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/> |
My System Specs![]() |
| | #6 (permalink) |
| | Re: How to concatenate a literal string and a variable value? "Tony Bansten" <tonytony@xxxxxx> wrote in message news:48e51d7f$0$6614$9b4e6d93@xxxxxx-online.net... Quote: > Assume I want to concatenate a literal text value and a variable value for > a function parameter > then the following does NOT work: > > objWorkbook.SaveAs("D:\work\v1_" + Filename) concatenate strings would be to use an '&' symbol... ThisStringVar = "D:\work\v1_" & Filename But... sloan's right... if you're using that "thing" called vb.net (even though the ".net" was removed from the name years ago, just adding to the confusion.net), you'll find VBA or VB groups to be far more helpful. |
My System Specs![]() |
| | #7 (permalink) |
| | Re: How to concatenate a literal string and a variable value? "Ken Halter" <Ken_Halter@xxxxxx_Sparingly_Hotmail.com> wrote in message news:%23BvscaPJJHA.4936@xxxxxx Quote: > "Tony Bansten" <tonytony@xxxxxx> wrote in message > news:48e51d7f$0$6614$9b4e6d93@xxxxxx-online.net... Quote: >> Assume I want to concatenate a literal text value and a variable value >> for a function parameter >> then the following does NOT work: >> >> objWorkbook.SaveAs("D:\work\v1_" + Filename) > That looks VBAish... if it were VB Classic, the "preferred" way to > concatenate strings would be to use an '&' symbol... > > ThisStringVar = "D:\work\v1_" & Filename > > But... sloan's right... if you're using that "thing" called vb.net (even > though the ".net" was removed from the name years ago, just adding to the > confusion.net), you'll find VBA or VB groups to be far more helpful. paths instead of manually doing it with string manipulation techniques, i.e.: set fso = createobject("scripting.filesystemobject") ThisStringVar = fso.buildpath( "D:\work\v1_", Filename ) I suspect that this would resolve the OP's problem, as the resulting value (for a filename of "theFile.txt") would be this: "D:\work\v1_\theFile.txt" Instead of the value produced by his original code: "D:\work\v1_theFile.txt" At least, it is my understanding that if "theFile.txt" is the filename, it must be preceded by a backslash to separate it from the parent folder portion of the path. /Al |
My System Specs![]() |
| | #8 (permalink) |
| | Re: How to concatenate a literal string and a variable value? On Oct 3, 12:11*am, "Al Dunbar" <AlanD...@xxxxxx> wrote: Quote: > "Ken Halter" <Ken_Halter@xxxxxx_Sparingly_Hotmail.com> wrote in message > > news:%23BvscaPJJHA.4936@xxxxxx > > > Quote: > > "Tony Bansten" <tonyt...@xxxxxx> wrote in message > >news:48e51d7f$0$6614$9b4e6d93@xxxxxx-online.net... Quote: > >> Assume I want to concatenate a literal text value and a variable value > >> for a function parameter > >> then the following does NOT work: Quote: Quote: > >> objWorkbook.SaveAs("D:\work\v1_" + Filename) Quote: > > That looks VBAish... if it were VB Classic, the "preferred" way to > > concatenate strings would be to use an '&' symbol... Quote: > > ThisStringVar = "D:\work\v1_" & Filename Quote: > > But... *sloan's right... if you're using that "thing" called vb.net (even > > though the ".net" was removed from the name years ago, just adding to the > > confusion.net), you'll find VBA or VB groups to be far more helpful. > IMHO, this is a good argument for using FSO methods to construct valid file > paths instead of manually doing it with string manipulation techniques, > i.e.: > > * * set fso = createobject("scripting.filesystemobject") > * * ThisStringVar = fso.buildpath( "D:\work\v1_", Filename ) > > I suspect that this would resolve the OP's problem, as the resulting value > (for a filename of "theFile.txt") would be this: > > * * "D:\work\v1_\theFile.txt" > > Instead of the value produced by his original code: > > * * "D:\work\v1_theFile.txt" > > At least, it is my understanding that if "theFile.txt" is the filename, it > must be preceded by a backslash to separate it from the parent folder > portion of the path. > > /Al file's original name in the SaveAs process (which is how I read the the need - though the OP is decidedly mute on this issue). It occurs to me that without the content of Filename and the subsequent concatenation to examine, plus the all too common & uninformative 'does NOT work' declaration of the problem, anything could be happening. For example, Filename might not contain the correct file extension. In that case, I believe the SaveAs would throw an error that might be misconstrued as a 'failure to concatenate' when in fact its a file type error. Without more info from the OP, it's just pure speculation on our part to try to say what's wrong (because the original code is NOT the problem). Tom Lavedas =========== http://members.cox.net/tglbatch/wsh/ |
My System Specs![]() |
| | #9 (permalink) |
| | Re: How to concatenate a literal string and a variable value? On Oct 2, 2:14 pm, tonyt...@xxxxxx (Tony Bansten) wrote: Quote: > Assume I want to concatenate a literal text value and a variable value for a function parameter > then the following does NOT work: > > objWorkbook.SaveAs("D:\work\v1_" + Filename) > > The variable "Filename" is filled with a valid value. > > So how do I otherwise concatenate two parts? > > Tony operator. If you use +, VB may try to coerce one or both of the operands, possibly yielding unexpected results. Also, when working with paths, use the Path.Combine method to build paths: objWorkbook.SaveAs(Path.Combine("D:\work", "v1_" & Filename)) Ditto on sloan's questions about Option Strict and Option Explicit. Chris |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Concatenate text string and text in variable with no space between | PowerShell | |||
| Concatenate text string and text in variable with no space between | VB Script | |||
| variable evaluation in a string | PowerShell | |||
| Find a string within a variable string | PowerShell | |||
| Subject: using a Variable with/in a string | PowerShell | |||