Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 35 of 35

Thread: Convert text file (.txt) to html file

  1. #21
    your above code is not working fo rme. throwing some error.

  2. #22
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    257
    Location
    Quote Originally Posted by Shanmugam
    your above code is not working fo rme. throwing some error.
    The code was just meant to provide a guide of how you might approach the problem. It was not vba code.

    Did you get my last post?
    Please elaborate on why the HTML comments are such a problem for you.

    orange

  3. #23
    i do not require html embedded codes. i am attaching you sample html file and this is what i am looking for as an output. Environment may be any like ms-word or excel or access, etc. but i need output similar to the attachment (look at html file and go to view source, with has without embedded codes).

    Thanks,
    Shanmugam

  4. #24
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    257
    Location
    Quote Originally Posted by Shanmugam
    i do not require html embedded codes. i am attaching you sample html file and this is what i am looking for as an output. Environment may be any like ms-word or excel or access, etc. but i need output similar to the attachment (look at html file and go to view source, with has without embedded codes).

    Thanks,
    Shanmugam
    Your <START> and </END> are not valid HTML Tags.
    You have embedded <BR> Tags?

    If you don't want the HTML comments but you do want other tags, then you have a parsing problem to solve. My approach with the scripting object would not allow for any internal tags such as <BR>.

    What do you need?

    The Word approach was the simplest solution to txt to html conversion.
    Unfortunately, Word gives embedded comments that, for some reason, is not acceptable to you.

  5. #25
    I require the same output as i attached before without any changes. please let me know whether this is possible.

    Is there any chances by without working with Ms-word and work with some other environment?

    Thanks,
    Shanmugam

  6. #26
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    257
    Location
    Quote Originally Posted by Shanmugam
    I require the same output as i attached before without any changes. please let me know whether this is possible.

    Is there any chances by without working with Ms-word and work with some other environment?

    Thanks,
    Shanmugam

    How did the HTML tags get into your sample?

  7. #27
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    257
    Location
    Quote Originally Posted by orange
    How did the HTML tags get into your sample?
    Here is some code for use in Access 2003, that will convert a series of text files to minimal html. The files are written with the same name base but with the extension .html

    Only the HTML and BODY tag appear within the html source.

    The input directory must be defined. Files have the .txt extension.
    The output directory must be defined. New files have .html extension.

    There must be a reference set to Microsoft Scripting Runtime

    [vba] '---------------------------------------------------------------------------------------
    ' Procedure : LoopThroughFilesInFolder_FSO
    ' Author : user
    ' Date : 1/20/2009
    ' Purpose : VBA EXpress --
    '
    ' Convert a series of txt files into minimal html as a batch process.
    ' Open a series of X.txt documents , append some html tags,
    ' then save each document with a.html extension.
    '
    'This uses Filescripting object
    '
    '*** MUST SET a reference to Microsoft Scripting Runtime ***
    '*** ***
    '---------------------------------------------------------------------------------------
    '
    Sub LoopThroughFilesInFolder_FSO()
    Dim i As Integer
    Dim myFileName As String

    Dim myVar As String


    '==== Using fso ========================

    Dim MyTop As String
    Dim MyBottom As String
    MyTop = "<html><body>"
    MyBottom = "</body></html>"
    Dim myNewFileBody As String
    Dim FileIn As File
    Dim FileOut As File
    Dim fileOutName As String
    Dim xName As String
    Dim x As Integer
    Dim fils As Files
    Dim tsIn As TextStream
    Dim tsOut As TextStream
    Const ForReading = 1, ForWriting = 2

    Dim ofso As Scripting.FileSystemObject
    Set ofso = New Scripting.FileSystemObject

    myVar = "I:\WordTest\output\"

    With Application.FileSearch
    .LookIn = "I:\WordTest\input\"
    .FileName = "*.txt"
    .Execute

    For i = 1 To .FoundFiles.Count
    fileOutName = myVar & "X.html"
    x = InStrRev(.FoundFiles(i), "\")
    xName = Mid(.FoundFiles(i), x + 1, Len(.FoundFiles(i)) - x - 4)
    fileOutName = myVar & xName & ".html"
    Set tsIn = ofso.OpenTextFile(.FoundFiles(i), ForReading)

    Set tsOut = ofso.CreateTextFile(fileOutName, ForWriting)

    'write the HTML header
    tsOut.Write MyTop

    'write the incoming data to output
    While Not tsIn.AtEndOfStream
    tsOut.WriteLine (tsIn.ReadLine)
    Wend
    tsIn.Close

    'write the html footer
    tsOut.Write MyBottom
    tsOut.Close
    Debug.Print "File IN " & i & " is " & .FoundFiles(i)



    Debug.Print "File saved in Output Folder as " & fileOutName

    Next i
    End With

    End Sub

    [\vba]

    I hope this is useful.
    Last edited by orange; 01-21-2009 at 06:35 AM.

  8. #28
    While executing the above program, i am getting error msg for the below codes,
    Dim FileIn as File
    Dim Fileout as File
    Dim fils as Files
    Dim tsIn As TextStream
    Dim tsOut As TextStream
    Dim ofso As Scripting.FileSystemObject

    as, 'Compile-error, user-defined type not defined'

    Please help!!

    Shanmugam

  9. #29
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    While executing the above program, i am getting error msg
    Not to butt in, but did you set the reference to Microsoft Scripting Runtime? Click Tools-> References and select it from the menu that appears.

    And please use VBA tags when posting code- click on the green VBA button. This will format the code according to VBIDE and make it much easier to read.
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  10. #30
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    257
    Location
    Quote Originally Posted by CreganTur
    Not to butt in, but did you set the reference to Microsoft Scripting Runtime? Click Tools-> References and select it from the menu that appears.

    And please use VBA tags when posting code- click on the green VBA button. This will format the code according to VBIDE and make it much easier to read.
    Thanks CreganTur, that's exactly the point I tried to make in my posting.

    There must be a reference set to Microsoft Scripting Runtime as CreganTur suggested.

    I still don't understand why html comments are so unacceptable.

  11. #31
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    I still don't understand why html comments are so unacceptable.
    Well, because he's trying to do a straight conversion from a .txt file to a .html file, the issue could be that he is not allowed to add any new formatting or data to the file. It could be part of a larger process that requires the converted files without any changes to the data.

    Or my theory could be completely wrong
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  12. #32
    Hey.. its working. Thanks a lot. Is it possible to create a text(*.txt) log file to print on each text file how many lines converted into html file?

    For e.g.:

    Logfile.txt

    215b008b.txt - 12 lines printed in 215b008b.html
    215b009b.txt - 12 lines printed in 215b009b.html
    215b010b.txt - 11 lines printed in 215b010b.html

    Thanks,
    Shanmugam

  13. #33
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    257
    Location
    Quote Originally Posted by CreganTur
    Well, because he's trying to do a straight conversion from a .txt file to a .html file, the issue could be that he is not allowed to add any new formatting or data to the file. It could be part of a larger process that requires the converted files without any changes to the data.

    Or my theory could be completely wrong
    I agree with your comments. It could be part of a bigger process. But in my mind, he was manually inserting HTML tags. In fact he inserted <START> and </END>, which are not HTML, and <P> and <BR>. My view is that some tags are allowed, but others aren't.
    I just wanted to hear the current conversion techniques and the real requirements from the poster.

  14. #34
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    257
    Location
    Quote Originally Posted by Shanmugam
    Hey.. its working. Thanks a lot. Is it possible to create a text(*.txt) log file to print on each text file how many lines converted into html file?

    For e.g.:

    Logfile.txt

    215b008b.txt - 12 lines printed in 215b008b.html
    215b009b.txt - 12 lines printed in 215b009b.html
    215b010b.txt - 11 lines printed in 215b010b.html

    Thanks,
    Shanmugam
    Yes it is possible.
    See the lines in this colour
    This is the approach NOT working CODE

    I would just create an empty file with the name you want (eg Results.log), separate from Access.

    Then in Access

    [vba] dim tsLog as textstream 'for the log file
    'use full path for the log file
    Set tsLog = ofso.CreateTextFile("Results.log", ForAppending)


    'write the incoming data to output

    myLinecounter =0

    While Not tsIn.AtEndOfStream
    myLinecounter = myLinecounter +1
    tsOut.WriteLine (tsIn.ReadLine)

    Wend

    tsLog.writeline .foundfiles(i).name &" - " & MyLinecounter & " lines printed in " & fileOutName
    [/vba]

    This was not tested, so may need some work.

    Good luck

  15. #35
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    257
    Location
    Here is revised code to
    a) convert text file to minimal HTML, and
    b) Call a Logger routine to create log records.

    [vba] '---------------------------------------------------------------------------------------
    ' Procedure : LoopThroughFilesInFolder_FSO
    ' Author : user
    ' Date : 1/20/2009
    ' Purpose : VBA EXpress --
    '
    ' Convert a series of txt files into minimal html as a batch process.
    ' Open a series of X.txt documents , append some html tags,
    ' then save each document with a.html extension.
    '
    'This uses Filescripting object
    '
    '*** MUST SET a reference to Microsoft Scripting Runtime ***
    '***
    ' Calls: Logger (LogFileName, LogRecord)
    ' -sub to write a logrec to a specified log file
    '---------------------------------------------------------------------------------------
    '
    Sub LoopThroughFilesInFolder_FSO()
    Dim i As Integer
    Dim myFileName As String
    Dim str As String
    Dim myVar As String

    '==== Using fso ========================

    Dim MyTop As String
    Dim MyBottom As String
    MyTop = "<html><body>"
    MyBottom = "</body></html>"
    Dim myNewFileBody As String

    Dim fileOutName As String
    Dim xName As String
    Dim x As Integer
    Dim MyLineCounter As Integer
    Dim ofso As FileSystemObject

    Dim tsIn As TextStream
    Dim tsOut As TextStream
    Const ForReading = 1, ForWriting = 2, ForAppending = 3

    myVar = "I:\WordTest\output\"

    'find all qualifying files
    With Application.FileSearch
    .LookIn = "I:\WordTest\input\"
    .FileName = "*.txt"
    .Execute

    Set ofso = New FileSystemObject
    fileOutName = myVar & "X.html"

    For i = 1 To .FoundFiles.Count

    x = InStrRev(.FoundFiles(i), "\")
    xName = Mid(.FoundFiles(i), x + 1, Len(.FoundFiles(i)) - x - 4)
    fileOutName = myVar & xName & ".html"

    Set tsIn = ofso.OpenTextFile(.FoundFiles(i), ForReading)
    Set tsOut = ofso.CreateTextFile(fileOutName, ForWriting)

    'write the HTML header
    tsOut.Write MyTop

    MyLineCounter = 0
    'write the incoming data to output
    While Not tsIn.AtEndOfStream
    MyLineCounter = MyLineCounter + 1 'count the input lines
    tsOut.WriteLine (tsIn.ReadLine)
    Wend
    str = (.FoundFiles(i) & " - " & MyLineCounter & " lines printed in " & fileOutName & " ")
    Call Logger("I:\wordtest\output\Results.log", str)
    tsIn.Close

    'write the html footer
    tsOut.Write MyBottom
    tsOut.Close
    Debug.Print "File IN " & i & " is " & .FoundFiles(i)
    Debug.Print "File saved in Output Folder as " & fileOutName

    Next i
    End With

    End Sub[/vba]
    Here is the Logger sub routine
    [vba]'---------------------------------------------------------------------------------------
    ' Procedure : Logger
    ' Author : user
    ' Date : 1/21/2009
    ' Purpose : To write records to a LOG file using FileSystemObject.
    '
    'Parameters
    ' sLogName As String -- full path and file name of the log file
    ' sLogRec As String -- record to be written to the log
    '
    ' NOTE: Each log record has a timestamp appended
    '
    ' Special Note/restriction:
    '***** Must set a reference to MICROSOFT SCRIPTING RUNTIME ***
    '---------------------------------------------------------------------------------------
    '
    Sub Logger(sLogName As String, sLogRec As String)
    Dim tslog As TextStream
    Dim fileLog As File
    Dim i As Integer
    Dim fso As FileSystemObject
    On Error GoTo Logger_Error

    Set fso = New FileSystemObject
    Set fileLog = fso.GetFile(sLogName) '"I:\wordtest\output\Results.log")
    Set tslog = fileLog.OpenAsTextStream(ForAppending)
    tslog.WriteLine sLogRec & Now()
    tslog.Close

    On Error GoTo 0
    Exit Sub

    Logger_Error:

    MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure Logger of Module ADO_Etc"
    End Sub [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •