Solved

Inserting simple word document with hyperlinks into html code -

Posted on 2003-11-20
1
387 Views
Last Modified: 2010-05-01
I have written a stock trading software system that produces a bunch of data that we are sending to our subscribers each week via an html newsletter. I have been able to create the newsletter html for all the data taken from my access database. Now I need to be able to read MS-Word docs, convert them to html and insert the html into specific sections of the newsletter html. Thanks to this forum, I have found code that converts (see below) the very simple Word documents into a fairly clean html file. I have one problem left to solve: How do I test for hyperlinks and how do I create the html code for those hyperlinks. (Confession: I am a pretty good VB programmer; I am a total newbie for html). Any/all help is appreciated.

 Dim docSource As Word.Document
  Dim lCharacterCntr As Long
 
  Dim strChar As String * 1
  Dim strHTMLFile As String
 
  Dim bBold As Boolean
  Dim bItalic As Boolean
  Dim bLink As Boolean
  Dim intIndentLevel As Integer
 
  Set docSource = Word.Documents.Open("C:\test.doc", , True)
 
  bBold = False
  bItalic = False
  bLink = False
  intIndentLevel = 0

  strHTMLFile = "test1.html"
  Open strHTMLFile For Output As #1

  ' create the document with basic header stuff
  Print #1, "<HTML>"
  Print #1, "<HEAD>"
  Print #1, "<TITLE>";
  Print #1, docSource.Name;
  Print #1, "</TITLE>"
  Print #1, "</HEAD>"
  Print #1, ""
  Print #1, "<BODY>"
 
  For lCharacterCntr = 1 To docSource.Characters.Count
    strChar = docSource.Characters(lCharacterCntr)
    If docSource.Characters(lCharacterCntr).Bold Then
      If Not bBold Then
         ' starting a bolded area
         Print #1, "<B>";
         bBold = True
      End If
    Else
      If bBold Then
         ' ending a bolded area
         Print #1, "</B>";
         bBold = False
      End If
    End If
   
    If docSource.Characters(lCharacterCntr).Italic Then
      If Not bItalic Then
         ' starting an Italic area
         Print #1, "<I>";
         bItalic = True
      End If
    Else
      If bItalic Then
         ' ending an Italic area
         Print #1, "</I>";
         bItalic = False
      End If
    End If
   
    If strChar = vbCr Then
      Print #1, "<P>"
    ElseIf strChar = vbLf Then
      Debug.Print "LF";
    Else
      Print #1, strChar;
    End If
  Next lCharacterCntr
  docSource.Close
  Set docSource = Nothing
 
  Print #1, "</BODY>"
  Print #1, "</HTML>"
  Close #1
0
Comment
Question by:turnermike
1 Comment
 
LVL 7

Accepted Solution

by:
wsteegmans earned 500 total points
ID: 9795532
First maybe some remarks ...

> I have found code that converts (see below) the very simple Word documents into a fairly clean html file.
Indeed, but your script can't handle:
- pictures
- bullet lists
- Tables
- Horizontal lines
- Headers and other Styles
- ...

If your word-docs are just plain text, it will work. Otherwise, I should use just the Save As HTML Functionlity. Indeed, the code is not so nice ... but is costs you NO time.

To answer your question:
In HTML, a URL is defined like this:
<A HREF="http://www.experts-exchange.com">IT Solutions Forum</A>

If you wan't to open the URL in a new window, it's like this:
<a href="http://www.hotmail.com" target="_blank">HoTMaiL</a>

Your Document Object (VBA) has a Hyperlinks Collection. In this collection, you find all the URL's defined in the document, with text and URL. Via the Range Object, you know where it is located ...

Maybe you can play with this objects ...

Just open your Word-document in Word, create a new procedure (in the Visual Basic Editor, for example something like this:
Sub Test
  MsgBox ActiveDocument.Hyperlinks.Item(1).TextToDisplay
End Sub
and 'Watch' the ActiveDocument, or just the Hyperlinks collection.

Success!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now