VBA Write Very large string - vba

So I have a large vba string I am trying to write as binary to a file. I have a function which does so beautifully. I have a string which is very long, about 80k lets say. This string reads in from a file correctly, passes through functions correctly, prints to screen correctly. But no matter what I do, in any way shape or form can i get more than 39k into the file. I have split the string into smaller bits figuring this is a VBA limitation. Then passing it to my fuction 42 characters at a time. Seek(LOF) +1 put is what im doing after open bin but... It doesn't seem to matter how I call it. Binary, ASCI, writing it all at once. I only get the last 39k. Here is the function im using to write to file. This is a memory limitation not a grammar error as far as I can tell and probably has little to do with the form of my code as much as some thing I seem not to get.
sub IBorrowedThis(hex_val as string)
dim output() as string
dim handle as long
output = Split(hex_val, "|")
handle = FreeFile
open "fp" for binary as #handle
For i = LBound(output) to Unbound(output)
seek #handle,LOF(handle) + 1
put #handle, , cbyte("&H" & output(i))
next i
close #handle
end sub
So I have tested this all in pieces and everything works. appending to the end included. just not for large files.

Another more simple way to write a binary string to a file:
SUB WriteBinaryStringToFile (hex_val AS STRING)
DIM handle AS LONG
handle = FREEFILE
OPEN "binary2.tst" FOR BINARY AS #handle
SEEK #handle, LOF(handle) + 1
PUT #handle, , hex_val
CLOSE #handle
END SUB

Simple method to write binary string to file:
SUB WriteBinaryStringToFile (hex_val AS STRING)
DIM c AS STRING * 1
DIM I AS LONG
DIM handle AS LONG
handle = FREEFILE
OPEN "fp" FOR BINARY AS #handle
FOR I = 1 TO LEN(hex_val)
c = MID$(hex_val, I, 1)
SEEK #handle, LOF(handle) + 1
PUT #handle, , c
NEXT I
CLOSE #handle
END SUB

Related

VBA LoadFromFile crashes on large files

I try to load chunks of a (really) large file in VBA:
Set dataStream = CreateObject("ADODB.Stream")
dataStream.Type = adTypeBinary
dataStream.Open
dataStream.LoadFromFile localDirectory & "\" & objFile.Name
byteBuffer = dataStream.Read(bufferSize)
If I understand correctly, the only amount of memory needed at a given time is bufferSize. Still, Access crashes at the LoadFromFile statement.
Is there a more robust way to read chunks from large files in VBA than ADODB.Stream?
I already tried How to Transfer Large File from MS Word Add-In (VBA) to Web Server? (but that has problems with large files, too. Get fails unpredictably with Error 63).
Ok, here's how I solved it
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(localDirectory)
Set sourceFile = objFSO.OpenTextFile(localDirectory & "\" & objFile.Name)
strChunk = sourceFile.Read(bufferSize)
and then convert the string to a byte array:
Function StringToByteArray(str As String) As Byte()
Dim i As Long
Dim b() As Byte
ReDim b(Len(str) - 1) As Byte
For i = 1 To Len(str)
b(i - 1) = Asc(Mid(str, i, 1))
Next i
StringToByteArray = b
End Function
The usual StrConv method does not work correctly! Therefore the method.
Command dataStream.LoadFromFile tries to load the entire file into RAM. You can check it yourself through the Task Manager.
Simple methods give more opportunities, so I suggest it be simpler. Below is the code that I applied to a 50GB file – and my computer didn't experience any problems, except that processing such a file will take a lot of time. But nothing freezes, and the process can be controlled as you like.
Dim fname As String
Dim fNo As Integer
Const bufferSize = 1024
Dim nextBytes(bufferSize) As Byte
Dim offset As Variant
fname = "C:\Path\BinaryFile.vdi"
fNo = FreeFile()
Open fname For Binary Access Read As #fNo
offset = 1
Do
Get #fNo, offset, nextBytes
offset = offset + bufferSize
' Do Something
If EOF(fNo) Then Exit Do
Loop
Close #fNo

Writing/Reading from text files in VB.net

I am a student in computer science and for a project I need to be able to read from a text file in a way that each line is assigned to a space within an array. This should happen so that each line of text file is read in the order that it appears in the text file. I would also appreciate any methods of writing to a text file as well.
If this question is already explained, could you please direct me to the existing answer.
Things to note:
1) I am coding in a console application in VB.NET
2) I am relatively new at coding
You can do it like this:
Dim sFile As String = "D:\File.txt"
Dim aLines As String() = System.IO.File.ReadAllLines(sFile)
System.IO.File.WriteAllLines(sFile, aLines)
Here's a sample from the official documentation:
Imports System.IO
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim sw As StreamWriter
' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
Dim createText() As String = {"Hello", "And", "Welcome"}
File.WriteAllLines(path, createText)
End If
' This text is always added, making the file longer over time
' if it is not deleted.
Dim appendText As String = "This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText)
' Open the file to read from.
Dim readText() As String = File.ReadAllLines(path)
Dim s As String
For Each s In readText
Console.WriteLine(s)
Next
End Sub
End Class
Remarks
This method opens a file, reads each line of the file, then adds each line as an element of a string array. It then closes the file. A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed. The resulting string does not contain the terminating carriage return and/or line feed.
Module Module1
Sub Main()
'Declare four variables
Dim oReader As New System.IO.StreamReader(".\archive01.txt") 'This file has to exist in the aplication current directory.
Dim oWriter As New System.IO.StreamWriter(".\archive02.txt") 'This file will be created by the software.
Dim oArray() As String = {}
Dim oString As String = Nothing
'For reading from .\archive01.txt and to load in oArray().
oString = oReader.ReadLine
While Not oString Is Nothing
If UBound(oArray) = -1 Then 'Ubound = Upper Bound, also exist LBound = Lower Bound.
ReDim oArray(UBound(oArray) + 1)
Else
ReDim Preserve oArray(UBound(oArray) + 1)
End If
oArray(UBound(oArray)) = New String(oString)
oString = oReader.ReadLine
End While
oReader.Close()
'For writing from oArray() to .\archive02.txt.
For i = 0 To oArray.Count - 1 Step 1
oWriter.WriteLine(oArray(i))
Next
oWriter.Close()
End Sub
End Module
Hi, try with this code. It works well. I hope that this helps to you to learn how to do this kind of things. Thank you very much. And happy codding!. :)

VB trouble loading data from a text file to list box

I am trying to get my application to show data from a text file to a list box with visual basics. I have it working for small sized text files no problem, but it will not work for text files the size of say 20mb. Is there any reason why it wouldn't load such or maybe some kind of limitations?
I forgot to ask, could it possibly just a matter of having to wait a long time? The program just sits there and I can't interact with it while it's getting the data...I think anyway....
Sub OpenFiles()
Dim myName As String = Dir(downloadTo + "*.TXT")
Do While myName <> ""
lstFiles.Items.Add(myName)
myName = Dir()
Loop
End Sub
Sub ReadFiles(textFile As String)
Dim logReader As New System.IO.StreamReader(textFile)
lstSrchTxt.Items.Clear()
While logReader.EndOfStream <> True
Dim stringx As String = logReader.ReadLine
If stringx.Contains(searchText) Then
lstSrchTxt.Items.Add(stringx)
End If
End While
logReader.Close()
End Sub
The issue was that I was entering too much data and it needed time to process all of the characters.

I am trying to use VBA code to save inkpicture contents, can only use vb.net or C#

I found this code that is missing the function call in the last line, any ideas on what the save to file command would be?, I'll just kludge it in.
'CODE to SAVE InkPicture to FILE
Dim objInk As MSINKAUTLib.InkPicture
Dim bytArr() As Byte
Dim File1 As String
File1 = "C:\" & TrainerSig & ".gif"
Set objInk = Me.InkPicture2.Object
If objInk.Ink.Strokes.Count > 0 Then
bytArr = objInk.Ink.Save(2)
fSaveFile bytArr, File1
End If
Here is a kludgy version of saving .InkPicture with VBA code in Access 2007 to a .isf file.
Private Sub Command283_Click()
'CODE to SAVE InkPicture to FILE
Dim objInk As MSINKAUTLib.InkPicture
Dim bytArr() As Byte
Dim File1 As String
File1 = "C:\test.isf"
Set objInk = Me.InkPicture2.Object
If objInk.Ink.Strokes.Count > 0 Then
bytArr = objInk.Ink.Save(2)
Open File1 For Binary As #1
Put #1, , bytArr
Close #1
End If
End Sub
I tried zaphod23's solution and it did not work for me. I also thought it quite strange that the solution would save in a .isf format, normally people want to save inkPicture contents to an image file (jpg, gif, etc.). It took me a while to hunt down the pieces of this code and put it together, so I'll post it here for others who might find it useful.
This takes an inkPicture object used for a signature panel in a Microsoft Access form, saves the contents as a gif image, then puts the image in an image object on the form (which is useful because the inkPicture contents will not show up when you go to print the form).
On Error Resume Next
Dim imgBytes() As Byte
Dim sFilePathAndName As String
imgBytes = Me.signaturePanel.Ink.Save(IPF_GIF)
If (UBound(imgBytes) = 0) Then
MsgBox ("Please enter your signature")
Else
sFilePathAndName = (Application.CurrentProject.Path & "\images\system\signatures\" & "signature")
Open sFilePathAndName For Binary Access Write As #1
Put #1, 1, imgBytes
Close #1
Me.imgSignature.Picture = sFilePathAndName
End If

Unicode string to flat file from vba

I want to store a unicode string in a flat file on a windows box from an excel/vba macro. The macro converts normal string to unicode representation, need to store it in a file and retrieve later.
As mentioned, you can use the Microsoft Scripting Runtime (scrrun.dll). I have posted some examples below. Some people also like the native file IO features. There is an extensive (and fairly comprehensive thread) thread here: http://www.xtremevbtalk.com/showthread.php?t=123814
However for Unicode files it's probably the least painful to use Textstreams:)
Public Sub StringToTextFile(ByVal path As String, ByVal value As String)
'Requires reference to scrrun.dll
Dim fso As Scripting.FileSystemObject
Dim ts As Scripting.TextStream
Set fso = New Scripting.FileSystemObject
Set ts = fso.CreateTextFile(path, False, True)
ts.Write value
ts.Close
End Sub
Public Sub LazyMansWay(ByVal path As String, ByVal value As String)
'Reference counting will cause the objects to be destroyed. The termination
'events of the classes will cause the connections to be closed.
CreateObject("Scripting.FileSystemObject").CreateTextFile(path, False, True).Write value
End Sub
Add a reference to "Microsoft Scripting Runtime" COM component (scrrun.dll).
It has all the classes (specifically FileSystemObject/TextStream) to create/read/write files.
The best solution I could figure is read the string in to a byte array and write each byte to a binary file
Private Function WriteBinaryFile(ByRef szData As String)
Dim bytData() As Byte
Dim lCount As Long
bytData = szData
Open PwdFileName For Binary As #1
For lCount = LBound(bytData) To UBound(bytData)
Put #1, , bytData(lCount)
Next lCount
Close #1
End Function
Read it back by opening the file in binary mode and reading each byte into a byte array and then converting it to a string.
Sub ReadBinaryFile(ByRef gszData As String)
Dim aryBytes() As Byte
Dim bytInput As Byte
Dim intFileNumber
Dim intFilePos
intFileNumber = FreeFile
Open PwdFileName For Binary As #intFileNumber
intFilePos = 1
Do
Get #intFileNumber, intFilePos, bytInput
If EOF(intFileNumber) = True Then Exit Do
ReDim Preserve aryBytes(intFilePos - 1)
aryBytes(UBound(aryBytes)) = bytInput
intFilePos = intFilePos + 1
Loop While EOF(intFileNumber) = False
Close #intFileNumber
gszData = aryBytes
End Sub