PDA

View Full Version : Solved: Inserting HTML Tags Into Word Document



Saintcools
06-29-2006, 06:27 AM
Hi
Can any one tell me how to insert HTML Tags into word document. I am giving an example of my problem.

"THIS IS A SAMPLE TEXT"

PROGRAM IN HTML

<HTML>
<HEAD>
<TITLE>TEST</TITLE>
</HEAD>
<BODY ALIGN=CENTER link="#0000FF" vlink="#800080" alink="#FF0000">
<P>THIS IS A SAMPLE TEXT</P>
</BODY>
</HTML>

Can anyone tell me how this tags can be inserted into the word document.

lucas
06-29-2006, 07:26 AM
Why not use front page or some other html editor that is set up for it?

Saintcools
06-29-2006, 07:56 AM
Hi Lucas
I want to insert HTML Tags into word document b'coz it meets my requirement and not something like Frontpage which i know that this is possible.I want ideas and help regarding inserting HTML tags in word thru VBA Coding.Thanks for reply.Any help in this regard will be of great help to me to complete my program.

Thanks
SaintCools

lucas
06-29-2006, 08:47 AM
Hey SaintCools,
I found a couple of old posts that address this issue.
http://www.vbaexpress.com/forum/showthread.php?t=3639

http://www.vbaexpress.com/forum/showthread.php?t=7314
attached is a file from Joost Verdaasdonk that inserts tags....
hope this gets you started in the right direction as I cannot help much with this.

Saintcools
06-30-2006, 01:01 AM
Hi Lucas,
Thanks for your help but after going thru all the post in this forum alone i posted my requirement.If someone can tell me how to insert HTML Tags into word and throw some light on this i will be more than happy and greatful.

fumei
06-30-2006, 04:32 AM
If you have gone through the posts then you know that your requirements are way out of line. Period.

You can not "insert HTML tags into Word". Let me repeat that. You can not insert HTML tags into Word. Word can generate HTML - but very very very very badly. However, generating HTML is NOT the same as being able to insert tags. HTML editors insert tags. If you really want to be able to inserts tags - use an HTML editor. Not Word. Word is a word processor, not an HTML editor.

You can pretend all you want, but to Word:

<HTML>
<HEAD>
<TITLE>TEST</TITLE>
</HEAD>
<BODY ALIGN=CENTER link="#0000FF" vlink="#800080" alink="#FF0000">
<P>THIS IS A SAMPLE TEXT</P>

is text. Text. Text. Text. Not tags. There is absolutely no way around this.

Yes, you can fudge and do sophisticated ways of inserting text that - when viewed by a browser will be seen as tags. But to Word...they are not tags. They are text.
Can anyone tell me how this tags can be inserted into the word document.No - No - to Word this is text. Not tags.

Use an HTML editor. There are lots around. Use Word for what it is good at. Text. If you want to use Word to put in text like <HTML>, or <BODY ALIGN=CENTER link="#0000FF" vlink="#800080" alink="#FF0000"> that is fine, this can be done. But they are NOT tags. A browser will interpret them as tags. Word can insert them as text. Just letters. Not "things" like tags.

Use an HTML editor. You will be greatful [sic] you did.

You say using Word meets your requirement. You do NOT state what this requirement is. I would suggest you rethink your requirement, because this subject comes up, and yes there are complex solutions...but does your requiremnt take into consideration the amount of work it will take to do it in Word? I suspect not.

You can NOT insert tags - as Tags - into a Word document. You can mess around to kind of, sort of, do it, but IMHO it is not anywhere near worth the effort, when there are proper tools to do the proper job.

Just to be clear: <TITLE>TEST</TITLE> to Word is:

< followed by a
T followed by a
I followed by a
T followed by a
L followed by a
E followed by a
> followed by a
T followed by a
E followed by a
S followed by a
T followed by a
< followed by a
/ followed by a
T followed by a
I followed by a
T followed by a
L followed by a
E followed by a
>

Characters. One after the other.....NOT tags. You can work out a way to put those characters into your document. Absolutely. But you will be putting them in as charcters...not tags.

Use an HTML editor. Word and HTML suck.

Saintcools
06-30-2006, 05:40 AM
Hi Fumei,
Thanks for your response. I Understand word is noware related to HTML but i think i have not made my self clear.I just need for example <HTML> is a tag i dont care whether word treats it as a Text or a Tags.All i need is you can say inserting of the word like
<HTML>
<HEAD>
<TITLE>...........</TITLE>
<BODY>
...........
..........
</BODY>
</HTML>
Just to be inserted i am not very particular about the output i can get the html output by converting word to text format and then to HTML. so how to insert these word <HTML>,<TITLE> etc at the respective postions.

fumei
06-30-2006, 08:25 PM
OK, I guess I was not clear myself. Here is how you insert <HTML> at a respective position.

1. Go to the position.
2. Find the < key.
3. Hold down the Shift key and press the < key.
4. Hold down the Shift key and press the "H" key.
5. Hold down the Shift key and press the "T" key.
6. Hold down the Shift key and press the "M" key.
7. Hold down the Shift key and press the "L" key.
8. Hold down the Shift key and press the ">" key.

Done. <HTML> is inserted at that position.

Sorry. I know that is sarcastic, but you are persisting in thinking of <HTML> as ONE thing. A thing that can be inserted as a thing. It is not. It is six individual characters.

Look, you COULD make a toolbar with gobs of macros on it. A macro that will type <HTML>, another macro that will type </HTML>, another macros that will type <TITLE> etc etc. Then you could click on those macro buttons and the specified text will be typed in.

What do you have if you have a toolbar with a bunch of buttons that insert TEXT (like <HTML>)? You have a sort of HTML editor. I repeat this. <HTML> (for eample) is not a thing. It is not something you can insert, as a thing. It is text.

So to answer the question
so how to insert these word <HTML>,<TITLE> etc at the respective postions - just like any other word (beauty, dog, extraordinary, Bob....). You type them in.

They are "words" just like any other words. There is nothing special about them, and they do NOT exist as special things to be inserted.

Again, you CAN make a bunch of macros that will insert - whatever text you want. <HTML>, California, <TITLE>, galaxies, <BODY>, wheat, <HEAD>, Wankel Rotary engine, <BACKGROUND>, pizza.....

To Word, these are ALL THE SAME. Characters of text.

Saintcools
06-30-2006, 09:06 PM
Hi Fumei
I know the macro and other things but i want to have a program which puts few lines of text before the first para in a document and a few lines after the last para in a document. Hope atleast someone can tell me how to do that i will take care of the rest of the HTML Problem.

fumei
07-01-2006, 07:49 PM
but i want to have a program which puts few lines of text before the first para in a document and a few lines after the last para in a document. You want a program that puts a few lines of text before the first para in a document, and a few lines after the last para? Remember...this is what you are asking for.

OK...since you are really persisting with this. This will do precisely that...put in a few lines at the start, and a few lines at the end. PRECISELY what you are asking for. You hope someone will tell you how to do that? Here you go.

This WILL do that. PRECISELY what you are asking for. Assumption: there is an existing document named StupidHTMLIdea.doc - the code opens this file and
puts few lines of text before the first para in a document and a few lines after the last paraOption Explicit

Sub Sigh()
Dim strStartLines As String
Dim strEndLines As String
strStartLines = "this is some text" & vbCrLf & _
"this is another line of some text" & _
vbCrLf & "and this is ANOTHER line of some text" & _
vbCrLf & vbCrLf

strEndLines = vbCrLf & "<this has a sort of tag character" & _
vbCrLf & _
"and a new line with < characters" & _
vbCrLf & " and gosh and ending character >"

Documents.Open FileName:="StupidHTMLIdea.doc"
With Selection
.TypeText Text:=strStartLines
.EndKey Unit:=wdStory
.TypeText Text:=strEndLines
End With
End SubThis does PRECISELY what you ask for.

Now do you get it? YOU have to put the text in the strings. I used "this is some text" - you will have to use "<HTML>" etc etc etc.

TEXT!

And if this is all you are looking for - a way to type a few lines at the start, and a few lines at the end...I suggest trying the macro recorder.

You may want to also use a template. Build a template with <HTML><HEAD><TITLE>yourBookmark etc etc. already in it. Then when you clone the template, have a userform that asks for the title, any any other elements. When you press OK on the userform, it would close, putting your values (Title etc etc into the bookmark locations.

But again...this is really really a waste of time. Use a HTML editor.

I am done here.

Saintcools
07-04-2006, 06:08 AM
Hi Fumei
Thanks for the help buddy but the same doc can be converted into text format and then to HTML to get the HTML output.This is what i am expecting.but a simple problem in that if u could tell me how to place the <p> tag and </p> tag before and after the start and end of each para then my problem is solved buddy.Expecting your reply in this matter.

fumei
07-04-2006, 08:54 PM
You know...you are treading on thin stuff here.

You want to put <p> at the start of each paragraph, and </p> and the end of each paragraph?

Hmmm. That is VERY easy. It takes...let's see, let me do this...9 lines of code, and all done, without ever selecting anything. The text would just change.

But let me ask you this. That will put <p> in front of EACH and EVERY parargraph. Are you sure you want that?

You need to get some basic of VBA here. Have you even TRIED using the macro recorder? It is true the macro recorder does not do things the most efficient way. But if you should that you were even trying to do this yourself, you may get better help. What you are asking for is very simple, and you CAN do it with no problem yourself.

EricFletcher
08-05-2006, 07:35 PM
Gee Gerry, I almost hesitate to jump in here... But what the heck!

First, I agree that an HTML editor is usually the best way to do HTML. However, there are times when Word can generate decent HTML code -- and I don't mean the garbage that gets created by saving as HTML! I've quite successfully used mail merge to "wrap" code around the content coming in from a merge. In fact, for an application like a catalogue, Mail merge can do a fine job. Just set up your merge document with the applicable HTML code and have the data flow in via fields.

Mind you, this is plain Word: no VBA required!

Eric

fumei
08-06-2006, 02:22 AM
Good one Eric. Can you supply a sample? I'd like to see it. Frankly I am not a mail merge guy, although I can picture the application described.

However, I do not believe this is what the OP was trying to get at. I, um, suggest looking at a few other posts.

fumei
08-06-2006, 05:16 AM
Besides, would you not be stuck with the structure of the merge document? I think...don't know...but I think the intention was to be able to take ANY text and wrap tags.