VBA: how to read text file

This is just example, how to read text file in Visual Basic for Applications (for example, in Excel macro).

It’s just a stub: there is no error processing here, the source file is really text file, no problems with permissions etc.


Sub read_from_text_file()

txtFile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If txtFile = False Then
MsgBox "No file to process"
Exit Sub
End If

Open txtFile For Input As #1
Input #1, myString
MsgBox myString
Close #1

End Sub

Using the clipboard in WSH

How to get the text from the clipboard


set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
textFromClipboard = objIE.document.parentwindow.clipboardData.GetData("text")
objIE.Quit
WScript.Echo textFromClipboard

How to put the text into clipboard


textIntoClipboard = "Some text" & VbCrLf & "Some more text"

Set objIE = WScript.CreateObject("InternetExplorer.Application")
objIE.Navigate "about:blank"
Do Until objIE.ReadyState = 4
WScript.Sleep 100
Loop

objIE.document.ParentWindow.ClipboardData.SetData "text", textIntoClipboard
objIE.Quit

The detailed explanation could be found here.

WMI: list of the methods and properties


strComputer = "."
Set objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

For Each objclass in objWMIService.SubclassesOf()
intCounter=0
If Left(objClass.Path_.Class,5) = "Win32" Then
For Each Qualifier in objClass.Qualifiers_
If UCase(Trim(Qualifier.Name)) = "ASSOCIATION" Then
intCounter = 1
End If
Next
If x = 0 Then
strComputer = "."
Set objWMIService = GetObject _
("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set strClass = objWMIService.Get(objClass.Path_.Class)
Wscript.Echo "PROPERTIES:"
For each strItem in strClass.properties_
Wscript.Echo objClass.Path_.Class & vbTab & strItem.name
Next
Wscript.Echo "METHODS:"
For Each strItem in strClass.methods_
Wscript.Echo objClass.Path_.Class & vbTab & strItem.name
Next
End If
End If
Next

Other scripts could be found here

WMI: get the information about the system


Option Explicit
On Error Resume Next

Dim strComputer, objWMIService
Dim colItems, objItem

strComputer = "."

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")

For Each objItem in colItems
WScript.Echo "Machine Name: " & objItem.CSName & VbCr & _
"===================================" & vbCr & _
"Description: " & objItem.Description & VbCr & _
"Manufacturer: " & objItem.Manufacturer & VbCr & _
"Operating System: " & objItem.Caption & VbCr & _
"Version: " & objItem.Version & VbCr & _
"Service Pack: " & objItem.CSDVersion & VbCr & _
"CodeSet: " & objItem.CodeSet & VbCr & _
"CountryCode: " & objItem.CountryCode & VbCr & _
"OS Language: " & objItem.OSLanguage & VbCr & _
"CurrentTimeZone: " & objItem.CurrentTimeZone & VbCr & _
"Locale: " & objItem.Locale & VbCr & _
"SerialNumber: " & objItem.SerialNumber & VbCr & _
"SystemDrive: " & objItem.SystemDrive & VbCr & _
"WindowsDirectory: " & objItem.WindowsDirectory & VbCr & _
""
Next

WMI+VBS: how to get the domain name


Option Explicit
On Error Resume Next

Dim strComputer, objNetwork, objWMIService
Dim colItems, objItem

Set objNetwork = WScript.CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
WScript.Echo "Computer = " & strComputer

Set objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&strComputer&"\root\cimv2")

Set colItems = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objItem in colItems
Wscript.Echo "Domain = " & objItem.domain
Next

There is also WMI FAQ on Microsoft site.

MS Word: replace spaces to non-breaking

To insert it manually, use Ctrl+Shift+Space


Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Normal")
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = "^s"
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll