Results 1 to 10 of 10
  1. #1
    2 Star Lounger
    Join Date
    Sep 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Saving doc as plain text with markup (2000/XP/2003)

    I need to save a Word document as a plain text and mark up any formatted characters with the same markup used by RTF (don't ask why!). Anybody I know I could loop through all the characters in the document, interrogate their font properties, and map those properties to the proper markup--but this seems inefficient (not to mention a pain in the neck). Anybody have any good ideas on how to do this more simply and efficiently?
    Thanks...Dan

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Saving doc as plain text with markup (2000/XP/2003)

    Why not save the document as .RTF?

  3. #3
    2 Star Lounger
    Join Date
    Sep 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Saving doc as plain text with markup (2000/XP/2003)

    Because it's being uploaded to non-Word document viewer which can't handle RTF.

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Saving doc as plain text with markup (2000/XP/2003)

    If you need RTF tags but the viewer can't handle RTF then what do you expect to happen?

    What about saving the file as RTF and then editing the file with notepad to remove all the RTF definitions at the start of the file. This will give you RTF tagged text which will refer back to non-existent header information so it won't work as RTF anymore but may satisfy you.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Saving doc as plain text with markup (2000/XP/2003)

    If you Save As a Web Page, textual formatting codes will be inserted (that's the understatement of the year <img src=/S/grin.gif border=0 alt=grin width=15 height=15> ) but it won't match RTF. I think we've had some threads on RTF recently, here or on the VB/VBA board. Andrew Savikas (Andrew77) posted a reference to an O'Reilly book on RTF that might be of use.

    In case it's of use, here's some code to convert Word bold formatting to HTML. Maybe it will give you some ideas:

    <pre>Sub BoldToHTML()
    If MsgBox("Convert Word bold to HTML tags?", _
    vbQuestion + vbYesNo) = vbNo Then Exit Sub
    Selection.HomeKey wdStory, wdMove
    With Selection.Find
    .ClearFormatting
    .Font.Bold = True
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    Do
    If .Execute() = False Then Exit Do
    With Selection
    If Len(.Text) > 2 Then
    .InsertBefore ""
    .InsertAfter "
    "
    .Font.Bold = False
    .Collapse wdCollapseEnd
    ElseIf Len(.Text) = 1 Then
    If .Text = Chr(13) Then
    .Font.Bold = False
    .Collapse wdCollapseEnd
    Else
    .InsertBefore ""
    .InsertAfter "
    "
    .Font.Bold = False
    .Collapse wdCollapseEnd
    End If
    End If
    End With
    Loop
    .ClearFormatting
    End With
    End Sub</pre>

    Apologies for the lack of comments; I wrote it just for me. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  6. #6
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Saving doc as plain text with markup (2000/XP/2003)

    If you really want to do this, it's certainly possible, but as you suspect, it'll be s-l-o-w. What, specifically, is the kind of markup you're looking for? Can you provide an example? When you say, "the same markup as RTF," what do you mean? Please provide more details.

  7. #7
    2 Star Lounger
    Join Date
    Sep 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Saving doc as plain text with markup (2000/XP/2003)

    Thanks for the nice code. That does help. I hadn't thought of automating replace with the formatting turned on. Regards, Dan

  8. #8
    2 Star Lounger
    Join Date
    Sep 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Saving doc as plain text with markup (2000/XP/2003)

    Basically, I want to put bold/italic/underline and fontsize markup around text as necessary, with the proper nesting. Anything else is gravy.

  9. #9
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Saving doc as plain text with markup (2000/XP/2003)

    This puts in the [b], <u> and [i] tags, along with markup for the font size. Adjust accordingly if you've got any fonts bigger than 54 points or if you've got half-point sizes or anything. This should at least get you started, I hope.

    <pre>Sub AddMarkup()
    Dim rng As Range
    Set rng = ActiveDocument.Range

    With rng.Find
    .ClearFormatting
    .Text = ""
    .Font.Bold = True
    .Replacement.ClearFormatting
    .Replacement.Text = "" & "^&" & ""
    .Execute Replace:=wdReplaceAll

    .ClearFormatting
    .Text = ""
    .Font.Italic = True
    .Replacement.ClearFormatting
    .Replacement.Text = "" & "^&" & ""
    .Execute Replace:=wdReplaceAll

    .ClearFormatting
    .Text = ""
    .Font.Underline = True
    .Replacement.ClearFormatting
    .Replacement.Text = "<u>" & "^&" & "</u>"
    .Execute Replace:=wdReplaceAll


    Dim k As Long
    For k = 1 To 54
    .ClearFormatting
    .Text = ""
    .Font.Size = k
    .Replacement.ClearFormatting
    .Replacement.Text = "<" & k & ">" & "^&" & "</" & k & ">"
    .Execute Replace:=wdReplaceAll
    Next k
    End With
    End Sub
    </pre>


  10. #10
    2 Star Lounger
    Join Date
    Sep 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Saving doc as plain text with markup (2000/XP/2003)

    Thanks very much Andrew. This looks great. I'll let you know if I run into any problems.

    Dan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •