combine strings containing NULL - vba

I am attempting to use the confluence REST API to attach files to a wiki page. I generate assorted graphs in excel using VBA and save them as images (currently as png/jpeg).
I then use the example here to upload a file to the wiki as an attachment. This method works well for text files, but when I attempt to use images I run into some issues. Specifically as I understand it the code:
sPostData = "--" & STR_BOUNDARY & vbCrLf & _
"Content-Disposition: form-data; name=""uploadfile""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _
"Content-Type: application/octet-stream" & vbCrLf & vbCrLf & _
sPostData & vbCrLf & _
"--" & STR_BOUNDARY & "--"
attempts to add string boundaries to the binary representation of the file by concatenating string. My issue is that the representation for the png contains NUL
‰PNG
SUB
NULNULNUL
IHDRNULNULEOT+NULNULSTXSYNBSETXNULNULSOH>&Ǽ ...etc...
which promptly terminates the string, and so you lose the rest of the file and the final string boundary. I believe this occurs with all string functionality in excel.
My question is how should I go about attaching the string boundaries to my file so as to avoid this issue?
Thanks in advance :)

As Alex K. pointed out I have been working under an incorrect assumption. The truncation is present in my analysis of the issue and not the actual string.
The problem was actually due to a mistake on my part - from the webiste:
Another caveat is the pvToByteArray function. It turns out send
method can not handle “byref” byte arrays, so for instance passing
baBuffer will fail, as VB6 sets up VT_BYREF bit of the type of the
variant parameter.
It was my implementation of this part of the code that was incorrect - this caused the failure and the error message
500: Stream ended unexpectedly
After correcting this the issue appears resolved.
Thanks again Alex.

Related

String Variable inside Single Quotes in Another String Variable Not Expanding - VBA

I'm trying to build quite a complicated URL String in VBA and I'm having troule escaping everything correctly. I managed to get the URL to work, but now I'm trying to "variablize" the importart parts and can't understand the issue (VBA doesn't exactly give detailed errors either).
Here is a working and non-working example URL:
Dim URL As STring, Client As String
Client = "AClientName"
URL = "http://URL?filter={'mode':'cli','client':'AClientName'}" ' This Works
URL = "http://URL?filter={'mode':'cli','client':' & CLIENT & '}" ' This Fails to Open
You're just missing the a close and open quote around the variable.
If you debug.print, you can see that it's interpreting your & and Client as just another part of the string instead of concatenating it as a variable.
In:
Debug.Print "http://URL?filter={'mode':'cli','client':'AClientName'}"
Out:
http://URL?filter={'mode':'cli','client':'AClientName'}
This works because there's no escaping the string or concatenation needed.
You can see here that & CLIENT & are part of the string.
In:
Debug.Print "http://URL?filter={'mode':'cli','client':' & CLIENT & '}"
Out:
http://URL?filter={'mode':'cli','client':' & CLIENT & '}
To get the proper string with the variable use this.
In:
Debug.Print "http://URL?filter={'mode':'cli','client':'" & Client & "'}"
Out:
http://URL?filter={'mode':'cli','client':'AClientName'}

Replace a String containing a "

I have run into a problem trying to get this code to work:
filereader.Replace(Chr(34) & "SomeSetting" & Chr(34) & "=dword:00000000", Chr(34) & "SomeSetting" & Chr(34) & "=dword:00000001")
I want it to replace a string which is !exaclty! looking like this, containing the quotation marks:
"SomeSetting"=dword:00000000
but what it does is looking for this string:
""SomeSetting""=dword:00000000
and i cant get it to work. Even if i tried this:
Dim Test As String = Chr(34)
Test will look like this:
""
instead of "
what am i missing here?
I think I see your problem... This is a quite common, accidental thing for people to do.
Strings are immutable, which means that once you've created them they cannot be changed without creating a new string instance.
The problem is this:
filereader.Replace(Chr(34) & "SomeSetting" & Chr(34) & "=dword:00000000", Chr(34) & "SomeSetting" & Chr(34) & "=dword:00000001")
The Replace() function returns the new string with the replaced value(s) (since it cannot change the original one), but you never use the instance it returns.
You should set your old string variable to the new string returned by Replace(), like this:
filereader = filereader.Replace(Chr(34) & "SomeSetting" & Chr(34) & "=dword:00000000", Chr(34) & "SomeSetting" & Chr(34) & "=dword:00000001")
To avoid (or at least minimize the risk of) things like this happening, make sure you read the information that Visual Studio's IntelliSense shows you when writing the function call.
If you do bump into problems anyway, make sure to check the MSDN documentation to see if you missed anything. They usually also have examples showing how you can use the methods.

vb6 printer.print does not work in vb.net 201

Receive this error in build
Operator '&' is not defined for types 'String' and 'Microsoft.VisualBasic.TabInfo'
This is the line, what do I have to do to make this valid (which does work in vb6)
Printer.Print("ATICTS PROBLEM REPORT: " _
& txtCallId.Text & FileSystem.TAB(70) & "Hours to date:" _
& FileSystem.TAB(90) & txtHours.Text)
I would suggest that you break that up...
Printer.Print("ATICTS PROBLEM REPORT: ")
Printer.Print(txtCallId.Text)
Printer.Print(FileSystem.TAB(70))
Printer.Print("Hours to date:")
Printer.Print(FileSystem.TAB(90))
Printer.Print(txtHours.Text)
As understand it, FileSystem.Tab wasn't a simple tab character or number of spaces. I believe the above has the best chance of producing the same results.

FileSystem.WriteAllText adds non-printable characters

Here are two methods for writing text to a file in VB.Net 2012. The first one prepends the same three non-printable characters to each file: . The second one works as expected and does not add the three characters. objDataReader is an OleDB datareader.
Any idea why?
Greg
My.Computer.FileSystem.WriteAllText(lblLocation.Text & "\" &
objDataReader("MessageControlId").ToString & ".txt", objDataReader("MsgContents").ToString, False)
Using outfile As New StreamWriter(lblLocation.Text & "\" & objDataReader("MessageControlId").ToString & ".txt")
outfile.Write(objDataReader("MsgContents").ToString)
End Using
Thanks. I found the entry below I after Googled BOM, in case anyone wants a more detailed explanation. While the BOM was not visible in a text editor it did cause problems when I passed the file to our HL7 interface engine.
Greg
Write text files without Byte Order Mark (BOM)?

Converting characters to ASCII code

Need help with reading special characters within my VB code. ASCII code Char(34) = " works fine but Char(60) = < and Char(62) = > are not being read.
My Code
node.FirstChild.InnerText = Chr(60) & "httpRuntime executionTimeout=" & Chr(34) & "999999" & Chr(34) & " maxRequestLength=" & Chr(34) & "2097151" & Chr(34) & "/" & Chr(62)
Without ASCII Code
'node.FirstChild.InnerText = "<httpRuntime executionTimeout="999999" maxRequestLength="2097151"/>"
Are you trying to modify a Config file? Try:-
node.FirstChild.InnerXml = "<httpRuntime executionTimeout=""999999"" maxRequestLength=""2097151"" />"
Note all that Chr marlarky is unnecessary, were you trying to avoid < and > being encoded as XML entities?
Maybe this doesn't answer your question, but you could use two double quotes to escape the quotes character in VB.NET:
node.FirstChild.InnerText = _
"<httpRuntime executionTimeout=""999999"" maxRequestLength=""2097151"" />"
I'm just guessing: you could use the String.Format method for your purposes:
node.FirstChild.InnerText = _
String.Format( _
"<httpRuntime executionTimeout=""{0}"" maxRequestLength=""{1}"" />", _
timeoutValue.ToString(), reqLenValue.ToString())
You'll need to give more information about how you're "seeing" the results. In my experience, problems with this are as likely to be about viewing strings in the debugger as getting the right strings in the first place.
I don't really see why you need to use Chr(60) etc at all, other than for the quotes. What happens when you just use < and > in your code?
I strongly suggest you dump the string out to the console rather than using the debugger - the debugger tries to show you how you could represent the string in code, rather than showing you the contents verbatim.
Of course, if this is XML then I'd expect serializing the XML out again to end up escaping the < and > - again, more information about what you're trying to do would be helpful. The absolute ideal (IMO) would be a short but complete program demonstrating the problem - a small console app which does one thing, and a description of what you want it to do instead.