Using Word in VB to Find & Replace in .html files

B

Barry Watson

Ok, bear with me. Here's what I want to do:

I create a Word application in Visual Basic.
I use it to open a Word document (*.doc).
I save the file as an .htm file.

(So far I can do all of this.)

Now I need to open the .htm file I created
and find and replace the <title>Page Title</title>
tags with values I dynamically create in my
VB app.

I need to do this for a folder of documents...
I know how to use the filesystemobject to get a
collection of files and step through them.

The problem is when I save the .doc file as .htm,
I can't see the html source of the document so I
can't run a find & replace on the <title> tag. I
need to know the VB code to view the source in
my Word object so I can replace the <title> tag.

Any help would be greatly appreciated and oh yeah,
I need the answer like last week... lol...

Barry
 
J

Jezebel

HTM files are plain text, so you can open them directly --

Dim pFileName as string
Dim pFileText as string
Dim pFileNum as long
Const pFolder as string = "C:\.....\"

pFileNum = FREEFILE
pFileName = Dir(pFolder & "*.doc")
Do until len(pFileName) = 0
pFileName = pFolder & pFileName

'Read the file
open pFileName for input as #pFileNum
pFileText = input(LOF(pFileNum), pFileNum)
Close #pFileNum

'Modify it
pFileText = Replace(pFileText, "<title>Page Title</title>", "[ Whatever
you need to insert]")

'Write the file
open pFileName for output as #pFileNum
Print #pFileNum, pFileText
Close #pFileNum

'Get the next file
pFileName = Dir()
Loop
 
J

Jezebel

Sorry, the line should be

pFileName = Dir(pFolder & "*.htm")



Jezebel said:
HTM files are plain text, so you can open them directly --

Dim pFileName as string
Dim pFileText as string
Dim pFileNum as long
Const pFolder as string = "C:\.....\"

pFileNum = FREEFILE
pFileName = Dir(pFolder & "*.doc")
Do until len(pFileName) = 0
pFileName = pFolder & pFileName

'Read the file
open pFileName for input as #pFileNum
pFileText = input(LOF(pFileNum), pFileNum)
Close #pFileNum

'Modify it
pFileText = Replace(pFileText, "<title>Page Title</title>", "[ Whatever
you need to insert]")

'Write the file
open pFileName for output as #pFileNum
Print #pFileNum, pFileText
Close #pFileNum

'Get the next file
pFileName = Dir()
Loop




Barry Watson said:
Ok, bear with me. Here's what I want to do:

I create a Word application in Visual Basic.
I use it to open a Word document (*.doc).
I save the file as an .htm file.

(So far I can do all of this.)

Now I need to open the .htm file I created
and find and replace the <title>Page Title</title>
tags with values I dynamically create in my
VB app.

I need to do this for a folder of documents...
I know how to use the filesystemobject to get a
collection of files and step through them.

The problem is when I save the .doc file as .htm,
I can't see the html source of the document so I
can't run a find & replace on the <title> tag. I
need to know the VB code to view the source in
my Word object so I can replace the <title> tag.

Any help would be greatly appreciated and oh yeah,
I need the answer like last week... lol...

Barry
 
B

Barry Watson

I feel... really stupid... It didn't even occur to me...

Thanks...

I sure am curious about your email address.... lol....

Barry

Jezebel said:
Sorry, the line should be

pFileName = Dir(pFolder & "*.htm")



Jezebel said:
HTM files are plain text, so you can open them directly --

Dim pFileName as string
Dim pFileText as string
Dim pFileNum as long
Const pFolder as string = "C:\.....\"

pFileNum = FREEFILE
pFileName = Dir(pFolder & "*.doc")
Do until len(pFileName) = 0
pFileName = pFolder & pFileName

'Read the file
open pFileName for input as #pFileNum
pFileText = input(LOF(pFileNum), pFileNum)
Close #pFileNum

'Modify it
pFileText = Replace(pFileText, "<title>Page Title</title>", "[
Whatever you need to insert]")

'Write the file
open pFileName for output as #pFileNum
Print #pFileNum, pFileText
Close #pFileNum

'Get the next file
pFileName = Dir()
Loop




Barry Watson said:
Ok, bear with me. Here's what I want to do:

I create a Word application in Visual Basic.
I use it to open a Word document (*.doc).
I save the file as an .htm file.

(So far I can do all of this.)

Now I need to open the .htm file I created
and find and replace the <title>Page Title</title>
tags with values I dynamically create in my
VB app.

I need to do this for a folder of documents...
I know how to use the filesystemobject to get a
collection of files and step through them.

The problem is when I save the .doc file as .htm,
I can't see the html source of the document so I
can't run a find & replace on the <title> tag. I
need to know the VB code to view the source in
my Word object so I can replace the <title> tag.

Any help would be greatly appreciated and oh yeah,
I need the answer like last week... lol...

Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top