Read、ReadText、Write、WriteText メソッドの例 (VB)

この例では、テキスト ボックスの内容をテキスト Stream とバイナリ Stream の両方に読み込む方法を示します。PositionSizeCharsetSetEOS など、ほかのプロパティおよびメソッドも示されています。

Public Sub ReadX()
    'Declare variables
    Dim objStream As Stream
    Dim varA As Variant
    Dim bytA() As Byte
    Dim i As Integer
    Dim strBytes As String
    
    'Instantiate and Open Stream
    Set objStream = New Stream
    objStream.Open
    
    'Write the text content of a textbox to the stream
    objStream.WriteText Text1.Text
    
    'Display the text contents and size of the stream
    objStream.Position = 0
    Debug.Print "Default text:"
    Debug.Print objStream.ReadText
    Debug.Print objStream.Size
    
    'Switch character set and display
    objStream.Position = 0
    objStream.Charset = "Windows-1252"
    Debug.Print "New Charset text:"
    Debug.Print objStream.ReadText
    Debug.Print objStream.Size
    
    'Switch to a binary stream and display
    objStream.Position = 0
    objStream.Type = adTypeBinary
    Debug.Print "Binary:"
    Debug.Print objStream.Read
    Debug.Print objStream.Size
    
    'Load an array of bytes with the text box text
    ReDim bytA(Len(Text1.Text))
    For i = 1 To Len(Text1.Text)
        bytA(i - 1) = CByte(Asc(Mid(Text1.Text, i, 1)))
    Next
    
    'Write the buffer to the binary stream and display
    objStream.Position = 0
    objStream.Write bytA()
    objStream.SetEOS
    objStream.Position = 0
    Debug.Print "Binary after Write:"
    Debug.Print objStream.Read
    Debug.Print objStream.Size
    
    'Switch back to a text stream and display
    Debug.Print "Translated back:"
    objStream.Position = 0
    objStream.Type = adTypeText
    Debug.Print objStream.ReadText
    Debug.Print objStream.Size
End Sub