PDA

View Full Version : How to work with line of text opened with notepad



Dzony
06-14-2016, 03:16 AM
Hi
I have code like this below. I recieve from it first line of text from file "Myfile.docm" but opened with notepad. How I can work with this line? How to get words or characters from it?



Private Sub Test_OpenTxt()

Dim strFilename As String: strFilename = "C:\Myfilepath\Myfile.docm"
Dim strTextLine As String
Dim iFile As Integer: iFile = FreeFile


Open strFilename For Input As #iFile
Do Until EOF(1)
Line Input #1, strTextLine
Debug.Print "strTextLine : " & " " & strTextLine
Loop
Close #iFile




End Sub

gmayor
06-14-2016, 05:45 AM
I take it you understand that DOCM is essentially a zip container for an XML file and thus not a text file? The macro will recover garbage. However, FWIW

Option Explicit

Sub Macro1()
Dim strFilename As String: strFilename = "C:\Myfilepath\Myfile.docm"
Dim sText As String
sText = Test_OpenTxt(strFilename)
'Do something with sText e.g.
MsgBox sText
End Sub


Private Function Test_OpenTxt(strFilename As String) As String
Dim strTextLine As String
Dim iFile As Integer: iFile = FreeFile
Open strFilename For Input As #iFile
Do Until EOF(1)
Line Input #1, strTextLine
Test_OpenTxt = "strTextLine : " & " " & strTextLine
Loop
Close #iFile
End Function

Dzony
06-15-2016, 05:53 AM
Thank you gmayor

jonh
06-17-2016, 04:15 PM
DOCM is essentially a zip container for an XML file and thus not a text file?

I have nothing to add other than XML is text. So presumably you could unzip the file and then apply some sort of XSL transform, or parse out the plain text.