I wrote a Word VBA program to do this. The program is open-source, under the MIT License.
The program is in a Word-doc on Github at:
https://github.com/jimyuill/word-web-nav/blob/main/tools/generate_word_html.docm
The Word-doc’s text describes how to use the program.
The code is commented to explain how it works (alt+F11 opens the IDE).
The program optionally adds a table-of-contents to the beginning of the document. That part of the code can be ignored, for the OP.
As mentioned by @Cindy Meister, the OP is «much too broad». The program is too big to post all of the code here. In summary:
Prompt for input Word-docs:
There is code to prompt the user for the Word-docs to be converted, from a particular directory. Most of that code is adapted from the example program here:
https://learn.microsoft.com/en-us/office/vba/api/office.filedialog.initialview
One of two APIs is used to get the list of Word-docs. Each API presents a GUI for file-system browsing.
One API allows the user to select particular files in a directory. The API is: Application.FileDialog(msoFileDialogFilePicker)
https://learn.microsoft.com/en-us/office/vba/api/office.filedialog
The other API allows the user to just select a directory. All Word-docs in it are converted. The API is: Application.FileDialog(msoFileDialogFolderPicker)
https://learn.microsoft.com/en-us/office/vba/api/office.msofiledialogtype
The program checks if there are two input Word-docs with the same root-name, and the extensions .doc, .docx, or .docm, e.g., «foo.doc» and «foo.docx». This isn’t allowed for input Word-docs because, for each Word-doc, a Word HTML-file is created using the Word-doc’s root-name:
<root-name>.html
Save each Word-doc in HTML format:
Within the chosen directory, the program creates a subdirectory that will be used to hold the Word-docs’ HTML files and directories.
The program loops to process each input Word-doc. Below are highlights of the code for each doc (some code is omitted). The code is based on the answer by @ASH
' Open the Word doc
Set wordDocObj = Application.Documents.Open(fileObj.Path)
' Change the current directory to the output-directory
ChangeFileOpenDirectory outputFolderObj
' Save the Word-doc in the format "filtered" HTML
fileBaseName = fileSystemObj.GetBaseName(fileName)
outputFileName = fileBaseName & ".html"
ActiveDocument.SaveAs fileName:=outputFileName, FileFormat:=wdFormatFilteredHTML
' Close the Word doc
wordDocObj.Close
' Change the current directory to the input-directory
ChangeFileOpenDirectory sourceFolderObj
Формулировка задачи:
Добрый вечер!
Помогите осуществить конвертацию текста из Worda в код HTML по средствам VBA.
Необходимо, чтобы признаки курсива, полужирного текста, выравнивания, гипперссылки, цвета слова или предложения менялись на html теги.
Например,
текст курсива
будет в html выглядеть так: <i>текст курсива</i>. Все строки из файла .doc должны помещаться в .txt файл. Первое предложение всегда должно обрамляться тегами <h1> и </h1>Примеры во вложении. В основном занимаюсь с VBA в Excel , а вот с Word впервые встречаюсь.
Буду признателен любой помощи.
Код к задаче: «Обработка текста из Word в код HTML»
textual
Sub BoldBB() With Selection.Find .Font.Bold = True .Replacement.Text = "[b]^&[/b]" 'это теги bold-выделений ' .Replacement.Font.Bold = False 'это убор bold-выделений из Word .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With End Sub
Полезно ли:
15 голосов , оценка 3.867 из 5
View Full Version : Convert text file (.txt) to html file
Shanmugam
01-17-2009, 03:29 AM
Hi,
I am having bunch of text files (*.txt) which i need to convert to html files (.htm). How to do this?
For e.g. There were nearly about 1,000 files in a specified path c:text*.txt, which i need to convert all the files into html file with a same file name.
Please help.
Thanks,
Shanmugam
hardlife
01-17-2009, 04:50 PM
Hi,
I am having bunch of text files (*.txt) which i need to convert to html files (.htm). How to do this?
For e.g. There were nearly about 1,000 files in a specified path c:text*.txt, which i need to convert all the files into html file with a same file name.
Please help.
Thanks,
Shanmugam
Try this Shanmugam
create empty word document, store VBA code (ALT+F11)
and prepare input and output directories directory, run code (ALT+F8)
C:WordTestinput (for *.txt files)
C:WordTestoutput (for *.html files)
happy and sunny day
Om Prakash
Pavel Humenuk
Sub LoopThroughFilesInFolder()
Dim i As Integer
Dim docDoc As Document
Dim myVar As String
myVar = «C:WordTestoutput»
With Application.FileSearch
.LookIn = «c:WordTestinput»
.FileName = «*.txt»
.Execute
For i = 1 To .FoundFiles.Count
‘ Message box for informational purposes only
MsgBox «File #» & i & » is » & .FoundFiles(i)
‘opens files individually
Set docDoc = Documents.Open(FileName:=.FoundFiles(i))
‘change the doc and save it
‘docDoc.Range.InsertAfter «test»
‘docDoc.SaveAs FileName = test1a.htm, FileFormat:=wdFormatHTML
‘docDoc.SaveAs FileName:=myVar & docDoc, FileFormat:=wdFormatRTF
docDoc.SaveAs FileName:=myVar & docDoc, FileFormat:=wdFormatHTML
‘docDoc.SaveAs FileName:=myVar & docDoc, FileFormat:=wdFormatWebArchive ‘ OR TRY THIS
‘docDoc.SaveAs FileName = test1a.htm, FileFormat:=wdFormatRTF
docDoc.Close (wdSaveChanges)
Next i
End With
End Sub
hardlife
01-17-2009, 05:08 PM
Hi,
I am having bunch of text files (*.txt) which i need to convert to html files (.htm). How to do this?
For e.g. There were nearly about 1,000 files in a specified path c:text*.txt, which i need to convert all the files into html file with a same file name.
Please help.
Thanks,
Shanmugam
Try this Shanmugam
create empty word document, store VBA code (ALT+F11)
and prepare input and output directories directory, run code (ALT+F8)
C:WordTestinput (for *.txt files)
C:WordTestoutput (for *.html files)
sorry, me forget new file extension
happy and sunny day and good night
Om Prakash
Pavel Humenuk
Sub LoopThroughFilesInFolder()
Dim i As Integer
Dim docDoc As Document
Dim myVar As String
myVar = «C:WordTestoutput»
With Application.FileSearch
.LookIn = «c:WordTestinput»
.FileName = «*.txt»
.Execute
For i = 1 To .FoundFiles.Count
‘ Message box for informational purposes only
MsgBox «File #» & i & » is » & .FoundFiles(i)
‘opens files individually
Set docDoc = Documents.Open(FileName:=.FoundFiles(i))
FileName = Mid(docDoc, 1, Len(docDoc) — 4) & «.html»
‘change the doc and save it
‘docDoc.Range.InsertAfter «test»
‘docDoc.SaveAs FileName = test1a.htm, FileFormat:=wdFormatHTML
‘docDoc.SaveAs FileName:=myVar & FileName, FileFormat:=wdFormatRTF
docDoc.SaveAs FileName:=myVar & FileName, FileFormat:=wdFormatHTML
‘docDoc.SaveAs FileName:=myVar & FileName, FileFormat:=wdFormatWebArchive ‘ OR TRY THIS
‘docDoc.SaveAs FileName = test1a.htm, FileFormat:=wdFormatRTF
docDoc.Close (wdSaveChanges)
Next i
End With
End Sub
orange
01-18-2009, 06:20 PM
Try this Shanmugam
create empty word document, store VBA code (ALT+F11)
and prepare input and output directories directory, run code (ALT+F8)
C:WordTestinput (for *.txt files)
C:WordTestoutput (for *.html files)
sorry, me forget new file extension
happy and sunny day and good night
Om Prakash
Pavel Humenuk
Sub LoopThroughFilesInFolder()
Dim i As Integer
Dim docDoc As Document
Dim myVar As String
myVar = «C:WordTestoutput»
With Application.FileSearch
.LookIn = «c:WordTestinput»
.FileName = «*.txt»
.Execute
For i = 1 To .FoundFiles.Count
‘ Message box for informational purposes only
MsgBox «File #» & i & » is » & .FoundFiles(i)
‘opens files individually
Set docDoc = Documents.Open(FileName:=.FoundFiles(i))
FileName = Mid(docDoc, 1, Len(docDoc) — 4) & «.html»
‘change the doc and save it
‘docDoc.Range.InsertAfter «test»
‘docDoc.SaveAs FileName = test1a.htm, FileFormat:=wdFormatHTML
‘docDoc.SaveAs FileName:=myVar & FileName, FileFormat:=wdFormatRTF
docDoc.SaveAs FileName:=myVar & FileName, FileFormat:=wdFormatHTML
‘docDoc.SaveAs FileName:=myVar & FileName, FileFormat:=wdFormatWebArchive ‘ OR TRY THIS
‘docDoc.SaveAs FileName = test1a.htm, FileFormat:=wdFormatRTF
docDoc.Close (wdSaveChanges)
Next i
End With
End Sub
I tried this approach, but get an run time error 13 on this line:
Set docDoc = Documents.Open(FileName:=.FoundFiles(i))
I set a reference to the Microsoft Word Object library.
Is there some other reference required?????
hardlife
01-19-2009, 10:38 AM
I tried this approach, but get an run time error 13 on this line:
Set docDoc = Documents.Open(FileName:=.FoundFiles(i))
I set a reference to the Microsoft Word Object library.
Is there some other reference required?????
May be you are not running code directly from word.
create empty WORD document, store VBA code (ALT+F11)
and prepare input and output directories directory, run code (ALT+F8)
hope this helps
HAPPY WINTER DAY
Pavel Humenuk
orange
01-19-2009, 12:18 PM
May be you are not running code directly from word.
create empty WORD document, store VBA code (ALT+F11)
and prepare input and output directories directory, run code (ALT+F8)
hope this helps
HAPPY WINTER DAY
Pavel Humenuk
Pavel,
I am trying to run the code from Access 2003.
I am now getting an error on the File Save
It says I’m tring to save a file with the name of a file that is already open????
hardlife
01-19-2009, 12:24 PM
Pavel,
I am trying to run the code from Access 2003.
I am now getting an error on the File Save
It says I’m tring to save a file with the name of a file that is already open????
Try run this code directly from word,
because I am not skilled enough to
run it from Access.
Me is wishing you good luck, Pavel
orange
01-19-2009, 01:02 PM
Try run this code directly from word,
because I am not skilled enough to
run it from Access.
Me is wishing you good luck, Pavel
Thanks Pavel.
I did get it to work in Access 2003, I had to Create the word.application
and I had to adjust the filename variable. It may not be an issue but I don’t know enough about Word to know for sure.
I did get errors that said I couldn’t save a file with the name of an already open file??
I’m not sure if any of this helps the person who started this thread.
Here is the code for using with Access 2003.
orange
‘—————————————————————————————
‘ Procedure : LoopThroughFilesInFolder
‘ Author : user
‘ Date : 1/18/2009
‘ Purpose : From VBA EXpress — Pavel Humenuk
‘
‘ Convert a series of txt files into html as a batch process.
‘ Open a series of X.txt documents with Word, then save each document
‘ in html format.
‘—————————————————————————————
‘
Sub LoopThroughFilesInFolder()
Dim i As Integer
Dim myFileName As String
Dim myVar As String
Dim oApp As Object
Set oApp = CreateObject(«Word.Application»)
oApp.Visible = False
Dim docDoc As Word.Document
myVar = «I:WordTestoutput»
With Application.FileSearch
.LookIn = «I:WordTestinput»
.FileName = «*.txt»
.Execute
For i = 1 To .FoundFiles.Count
‘ Message box for informational purposes only
MsgBox «File #» & i & » is » & .FoundFiles(i)
‘opens files individually
Set docDoc = Documents.Open(.FoundFiles(i))
Debug.Print Documents.Open(.FoundFiles(i)).Name
myFileName = Mid(docDoc, 1, Len(docDoc) — 4) & «.html»
‘change the doc and save it
‘docDoc.Range.InsertAfter «test»
‘docDoc.SaveAs FileName = test1a.htm, FileFormat:=wdFormatHTML
‘docDoc.SaveAs FileName:=myVar & FileName, FileFormat:=wdFormatRTF
docDoc.SaveAs myVar & myFileName, wdFormatHTML
MsgBox «File saved in Output Folder as » & myFileName
‘docDoc.SaveAs FileName:=myVar & FileName, FileFormat:=wdFormatWebArchive ‘ OR TRY THIS
‘docDoc.SaveAs FileName = test1a.htm, FileFormat:=wdFormatRTF
docDoc.Close (wdSaveChanges)
Next i
End With
End Sub
hardlife
01-19-2009, 01:32 PM
Thanks Pavel.
I did get it to work in Access 2003, I had to Create the word.application
and I had to adjust the filename variable. It may not be an issue but I don’t know enough about Word to know for sure.
I did get errors that said I couldn’t save a file with the name of an already open file??
I’m not sure if any of this helps the person who started this thread.
Here is the code for using with Access 2003.
orange
Hi it is unbelievable, at least, me did not know how to use object,
it is good example form me, thank You, happy winter day, Pavel
hardlife
01-19-2009, 01:33 PM
Hi it is unbelievable, at least, me did not know how to use object,
it is good example for me, thank You, happy winter day, Pavel
orange
01-19-2009, 01:35 PM
Hi it is unbelievable, at least, me did not know how to use object,
it is good example form me, thank You, happy winter day, Pavel
You are very welcome.
orange
Shanmugam
01-20-2009, 04:07 AM
Hi,
Its converting through word document. If we open the source code from html, its showing various codes.
But i need direct convert from .txt to .html and if i open html it should show only text.
For e.g. here is the command i type in .txt file:
<START>
<TITLE>New</TITLE>
<BODY>
<P>
This is test
</P>
</BODY>
<END>
If open html it should show “This is test” and if we do ‘view source’ it should show only the above html tag starts with <START> and ends with <END> and not any other codes.
Thanks,
Shanmugam
orange
01-20-2009, 06:48 AM
Hi,
Its converting through word document. If we open the source code from html, its showing various codes.
But i need direct convert from .txt to .html and if i open html it should show only text.
For e.g. here is the command i type in .txt file:
<START>
<TITLE>New</TITLE>
<BODY>
<P>
This is test
</P>
</BODY>
<END>
If open html it should show ?This is test? and if we do ‘view source’ it should show only the above html tag starts with <START> and ends with <END> and not any other codes.
Thanks,
Shanmugam
Shanmugam,
You have been shown a simple way to auto-convert (in a batch manner) from text to html by means of Word. This method inserts some comments describing the incoming document but these are only comments. The comments do not affect the HTML. They could be used by Word if it was to convert the document to RTF or some other format, I think.
Most HTML documents have some meta data beyond what you are suggesting. What is your concern with the embedded comments?
If you really want to do get rid of the comments then I think you should be looking at Regular Expressions. This technique could be automated to parse code.
orange
Shanmugam
01-20-2009, 07:00 AM
Yes, i need HTML output without meta data. Is there any way to convert without interpreting MS-Word? I hope this would help to convert without meta data.
Shanmugam
orange
01-20-2009, 07:54 AM
Yes, i need HTML output without meta data. Is there any way to convert without interpreting MS-Word? I hope this would help to convert without meta data.
Shanmugam Can you please tell us why you don’t want the embedded comments and meta tags?
If you want the most basic HTML, then in your Access script
Create and populate variables, eg,
Dim myTop as string
Dim myBottom as string
Dim txtRead as string
Dim newFileBody as string
MyTop = «<html><body>»
MyBottom = «</body></html>»
For each file in my list
txtRead = «»
txtRead = ** Read the text file into a variable
newFileBody = myTop & txtRead & myBottom ‘the minimal data for HTML
Write newFileBody as your File.html ‘adjust to use your existing file name ‘not the extension
Next
This would give you NO formatting, but it would be an HTML file.
(But I don’t think this is really what you want/need)
For more info on
**Read the text into a variable
see http://msdn.microsoft.com/en-us/library/aa155438(office.10).aspx (http://msdn.microsoft.com/en-us/library/aa155438%28office.10%29.aspx)
and http://www.techbookreport.com/tutorials/fso1.html
Shanmugam
01-20-2009, 10:05 AM
i am doing a project work where i need to convert all the text files into html file. For which currently i am doing this manually (i.e. opening a notepad, then file save as *.html and then save the file). If you open that converted html file it will not have any embedded codes, hence i am looking for the same.
Will be great, if you could prove me the same. The above codes (2 sets of codes) confusing me lot.
Please help!!
Thanks,
Shanmugam
orange
01-20-2009, 10:11 AM
i am doing a project work where i need to convert all the text files into html file. For which currently i am doing this manually (i.e. opening a notepad, then file save as *.html and then save the file). If you open that converted html file it will not have any embedded codes, hence i am looking for the same.
Will be great, if you could prove me the same. The above codes (2 sets of codes) confusing me lot.
Please help!!
Thanks,
Shanmugam
I do not understand why the method that was given is not satisfactory.
If you look at the resultiing HTML files in any browser, the HTML is rendered as expected. The comments within the file are irrelevant to the browser.
Shanmugam
01-20-2009, 10:26 AM
This code:
Dim myTop as string
Dim myBottom as string
Dim txtRead as string
Dim newFileBody as string
MyTop = «<html><body>»
MyBottom = «</body></html>»
For each file in my list
txtRead = «»
txtRead = ** Read the text file into a variable
newFileBody = myTop & txtRead & myBottom ‘the minimal data for HTML
Write newFileBody as your File.html ‘adjust to use your existing file name ‘not the extension
Next
is not working for me. i dont know where to paste this code. do i need to append code with the previous main code or this is something different code where i should not append with anything?
orange
01-20-2009, 10:47 AM
This code:
Dim myTop as string
Dim myBottom as string
Dim txtRead as string
Dim newFileBody as string
MyTop = «<html><body>»
MyBottom = «</body></html>»
For each file in my list
txtRead = «»
txtRead = ** Read the text file into a variable
newFileBody = myTop & txtRead & myBottom ‘the minimal data for HTML
Write newFileBody as your File.html ‘adjust to use your existing file name ‘not the extension
Next
is not working for me. i dont know where to paste this code. do i need to append code with the previous main code or this is something different code where i should not append with anything?
What environment are you working in?
Your question was in an Access forum. One of the responses was based on a Word environment. My responses apply to Access 2003 environment.
Could you please respond to my previous post regarding the comments and meta tags within the HTML file?
Shanmugam
01-20-2009, 10:49 AM
i dont know where to apply your previous code. is this a separate code from the rest?
Shanmugam
01-20-2009, 10:54 AM
your above code is not working fo rme. throwing some error.
orange
01-20-2009, 11:01 AM
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
Shanmugam
01-20-2009, 11:09 AM
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
orange
01-20-2009, 11:18 AM
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.
Shanmugam
01-20-2009, 11:25 AM
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
orange
01-20-2009, 11:50 AM
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?
orange
01-20-2009, 01:20 PM
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:WordTestoutput»
With Application.FileSearch
.LookIn = «I:WordTestinput»
.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.
Shanmugam
01-21-2009, 05:03 AM
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
CreganTur
01-21-2009, 06:21 AM
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.
orange
01-21-2009, 06:31 AM
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.
CreganTur
01-21-2009, 06:39 AM
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:dunno
Shanmugam
01-21-2009, 06:41 AM
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
orange
01-21-2009, 06:45 AM
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:dunno
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.
orange
01-21-2009, 07:03 AM
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
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
This was not tested, so may need some work.
Good luck
orange
01-21-2009, 06:00 PM
Here is revised code to
a) convert text file to minimal HTML, and
b) Call a Logger routine to create log records.
‘—————————————————————————————
‘ 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:WordTestoutput»
‘find all qualifying files
With Application.FileSearch
.LookIn = «I:WordTestinput»
.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:wordtestoutputResults.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
Here is the Logger sub routine
‘—————————————————————————————
‘ 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:wordtestoutputResults.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
Powered by vBulletin® Version 4.2.5 Copyright © 2023 vBulletin Solutions Inc. All rights reserved.
Недавно обнаружил небольшой макрос для Word, позволяющий преобразовать форматированный текст в документе тегами HTML.
В частности, макрос преобразовывает текст, оформленный такими стилями, как Полужирный, Курсив, Подчеркнутый, а также добавляет вместо знака абзаца тег <BR>, а вместо двух идущих подряд знаков абзаца — тег <P>.
Вид текста до преобразования:
и после преобразования:
Как видите, очень неплохо. Теперь можно скопировать получившийся код и вставить на свою страничку в сети Интернет.
Ссылка на страницу с макросом (Word HTML 1.0): http://www.robinupton.com/software/.