# insert html tags into word document



## Norto23 (May 18, 2012)

I've got several hundred word documents that I need to do some formatting on. I need to go through them and insert html tags into the document as per below...

a short *bold text* line

becomes

a short < b >bold text< / b > line

Minus the spaces of course.

At the moment I really only need bold, italic, underline, and unordered lists.

Does anyone know of the easiest and most efficient way of doing this? (Or some macro code?)

I don't have much experience in Word, but currently looking at writing a macro.


----------



## macropod (Apr 11, 2008)

Hi,

You could use a macro like the following. It processes all file in the selected folder.

```
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With wdDoc.Range.Find
    .Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .ClearFormatting
    .Font.Bold = True
    With .Replacement
      .ClearFormatting
      .Text = "<b>^&</b>"
      .Font.Bold = False
    End With
    .Execute Replace:=wdReplaceAll
    .ClearFormatting
    .Font.Italic = True
    With .Replacement
      .ClearFormatting
      .Text = "<i>^&</i>"
      .Font.Italic = False
    End With
    .Execute Replace:=wdReplaceAll
    .ClearFormatting
    .Font.Underline = True
    With .Replacement
      .ClearFormatting
      .Text = "<u>^&</u>"
      .Font.Underline = False
    End With
    .Execute Replace:=wdReplaceAll
  End With
  wdDoc.Close SaveChanges:=True
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
```
I don't know what you mean by 'unordered lists', so I haven't coded for that.

Note that the code deletes the bold/italic/underline formatting, which means it can be re-run on the same files if you decide to change the formatting (you'll still have to delete andy existing unwanted tags, though). You can delete/comment out the '= False' lines that do that if you prefer, to retain the formatting, but that will affect the tags too.


----------



## macropod (Apr 11, 2008)

Cross-posted at: insert html tags into word document | Windows Secrets Lounge
For cross-posting etiquette, please read: Excelguru Help Site - A message to forum cross posters


----------



## Norto23 (May 18, 2012)

Thanks for the response. I'm now getting the below error, I'm running Mac Word 2011. Do you know why this is?

Run-time error '429'
ActiveX component can't create object

This occurs on the Set oFolder line


----------



## macropod (Apr 11, 2008)

As you didn't mention using a Mac, the code was written for a Windows environment. Since the Mac platform doesn't support how the GetFolder function works (there's probably an equivalent, but I don't know it), try the follwing:

```
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
strFolder = InputBox("What is the path to the folder to be processed?")
If Dir(strFolder, vbDirectory) = "" Then
  MsgBox "Invalid folder"
  Exit Sub
End If
strFile = Dir(strFolder & ":*.doc", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strFolder & ":" & strFile, AddToRecentFiles:=False, Visible:=False)
  With wdDoc.Range.Find
    .Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .ClearFormatting
    .Font.Bold = True
    With .Replacement
      .ClearFormatting
      .Text = "<b>^&</b>"
      .Font.Bold = False
    End With
    .Execute Replace:=wdReplaceAll
    .ClearFormatting
    .Font.Italic = True
    With .Replacement
      .ClearFormatting
      .Text = "<i>^&</i>"
      .Font.Italic = False
    End With
    .Execute Replace:=wdReplaceAll
    .ClearFormatting
    .Font.Underline = True
    With .Replacement
      .ClearFormatting
      .Text = "<u>^&</u>"
      .Font.Underline = False
    End With
    .Execute Replace:=wdReplaceAll
  End With
  wdDoc.Close SaveChanges:=True
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
```


----------



## Norto23 (May 18, 2012)

Thanks Paul, do you know why I would be getting a run-time error '68' on the below line?

strFile = Dir(strFolder & ":*.doc", vbNormal)


----------



## macropod (Apr 11, 2008)

As you've probably already guesed, I'm not that familiar with vba on the Mac. You may need to seek help on a specialist Mac forum.


----------



## macropod (Apr 11, 2008)

Hi Norton,

Did you manage to sort out the mac-specific issues for looping through a collection of files in a folder? If so, I'd be grateful if you could post the code.

If not, you could delete all of:

```
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
```
and

```
Wend
Set wdDoc = Nothing
```
then simply open a given document and run the code. You won't have the benefit of the multi-file processing, but at least you won't have to manually do all the Find/Replace operations.


----------



## Norto23 (May 18, 2012)

Hey there, yeah i did the top part of the code is posted below...

*** note this is a hack and loops through all files in the same folder as the active document

*** also i had to hack the strFolder variable as per below because the ActiveDocument.path is buggy and posts the file name also!

Sub AddHtmlTagsMAC()
'
' mac based version of the macro
'
'
Application.ScreenUpdating = False

Dim strFolder As String, strFile As String, wdDoc As Document, Response As VbMsgBoxResult

Response = MsgBox("This will HTML-ify all files in the active folder. Continue?", vbYesNo Or vbDefaultButton2)
If Response = vbYes Then

strFolder = Replace(ActiveDocument.Path, ActiveDocument.Name, "")
strFile = Dir(strFolder)

If strFolder = "" Then Exit Sub

While strFile <> ""


----------

