*bold, _italic, <>heading in Word/VB

V

vjp2.at

I tend to write a lot of documents in text-only format, with
the following conventions:

This_is_italic This*is*bold
[This is a footnote] <This is a chapter heading>

In the case of bold/italic, I'd want any token that has _ or * in it.
Normally, I don't do documents that are so long as to make manual conversion
feasible. A year ago, when I had a non-profitable emergency, Klaus Linke was
so kind as to write me a macro to do the most cumbersom conversion, for
footnotes. The convention I use for bold and italic, I understand is much
more problematic. The chapter headings I would need so I can have a table of
contents. Right now I have no immediate need for this, but I am considering
returning to that non-profitable undertaking.

Incidentally, I used to use SCRIBE where the conventions were

@i(This is italic) @b(This is bold)
@foot(This is a footnote) @chap(This is chapter heading)
@u(This is underline) @sec(This is a chapter heading)

Furthermore, I've programmed in a number of languages (FORTRAN, ALGOL,
PASCAL, C) so I don;t mind doing a little bit of coding so long as I
understood (how to find) all the MS Word variable names.

I'm attaching whatever VB macros I do have, and comments others have
offered. If I seem a bit confused it is from the short attention span I have
been able to afford to direct towards this project.

.Text = "*\_*"
.Text = "*\**"

\*[!^13]@\*

Sub Foot()

Dim myRange As Range
Dim boolFound As Boolean
Dim myFootnote As Footnote
Set myRange = ActiveDocument.Content
Do
myRange.Find.ClearFormatting
With myRange.Find
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
End With
myRange.Find.Execute
boolFound = myRange.Find.Found
If boolFound Then
myRange.Characters.Last.Delete
myRange.Characters.First.Delete
myRange.Cut
Set myFootnote = ActiveDocument.Content.Footnotes.Add( _
Range:=myRange)
myFootnote.Range.PasteAndFormat (wdPasteDefault)
Set myRange = ActiveDocument.Content
myRange.Collapse (wdCollapseStart)
Else
Exit Sub
End If
Loop
End Sub

Sub jb()
'
' Macro written by Maniac on May 16, 2006
' Reformats text between two strings
'

' Specify search phrases (start / end)
A$ = "abc"
B$ = "def"

' Start at the beginning and reset all
' search formatting options

ActiveDocument.Range(0).Select
Selection.Find.ClearFormatting
' Loop repeats until first phrase not found
While Selection.Find.Execute(A$)
StartReformat = Selection.End
Selection.MoveRight
Selection.Find.Execute (B$)
StopReformat = Selection.Start
Selection.MoveRight
' Add formatting to the following section
' Options include:
' .Bold, .Italic, .Underline, .StrikeThrough (true / false)
' .Size = font size
' .Font.Color = wdColorGreen (Red, Blue, etc... see help)

With ActiveDocument.Range(StartReformat, StopReformat)
.Bold = True
.Font.Color = wdColorBlue
End With
Wend
End Sub



- = -
Vasos Panagiotopoulos, Columbia'81+, Reagan, Mozart, Pindus, BioStrategist
http://ourworld.compuserve.com/homepages/vjp2/vasos.htm
---{Nothing herein constitutes advice. Everything fully disclaimed.}---
[Homeland Security means private firearms not lazy obstructive guards]
[Urb sprawl confounds terror] [Remorse begets zeal] [Windows is for Bimbos]
 
K

Klaus Linke

Hi Vasos,
This_is_italic This*is*bold

In that example, you have missing blanks?
This _is_ italic This *is* bold

The convention I use for bold and italic, I understand is much
more problematic.

Not at all :)
Unless I'm missing something you don't even need a macro to fix this, you
can use Format > AutoFormat.
In the options, disable everything except "*Bold* and _italic_ with real
formatting".
Or if you want to do the AutoFormat with a macro:

With Options
.AutoFormatApplyHeadings = False
.AutoFormatApplyLists = False
.AutoFormatApplyBulletedLists = False
.AutoFormatApplyOtherParas = False
.AutoFormatReplaceQuotes = False
.AutoFormatReplaceSymbols = False
.AutoFormatReplaceOrdinals = False
.AutoFormatReplaceFractions = False
.AutoFormatReplacePlainTextEmphasis = True
.AutoFormatReplaceHyperlinks = False
.AutoFormatPreserveStyles = False
.AutoFormatPlainTextWordMail = False
End With
ActiveDocument.Content.AutoFormat

Do the footnotes after you do this AutoFormat, since AutoFormat only works
on the main document.
Else, you'd miss bold and italic text in the footnotes.

<This is a chapter heading>

For the headings, you probably could adapt your macro jb, but it would be
simpler to do a wildcard replacement.
Find what: \<[!^13]@\>
Replace with: ((leave that empty, and choose Format > Style > Heading 1"))

If you record that, you should get the code:
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles(wdStyleHeading1)
With Selection.Find
.Text = "\<[!^13]@\>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

Regards,
Klaus


I tend to write a lot of documents in text-only format, with
the following conventions:

This_is_italic This*is*bold
[This is a footnote] <This is a chapter heading>

In the case of bold/italic, I'd want any token that has _ or * in it.
Normally, I don't do documents that are so long as to make manual
conversion
feasible. A year ago, when I had a non-profitable emergency, Klaus Linke
was
so kind as to write me a macro to do the most cumbersom conversion, for
footnotes. The convention I use for bold and italic, I understand is much
more problematic. The chapter headings I would need so I can have a table
of
contents. Right now I have no immediate need for this, but I am
considering
returning to that non-profitable undertaking.

Incidentally, I used to use SCRIBE where the conventions were

@i(This is italic) @b(This is bold)
@foot(This is a footnote) @chap(This is chapter heading)
@u(This is underline) @sec(This is a chapter heading)

Furthermore, I've programmed in a number of languages (FORTRAN, ALGOL,
PASCAL, C) so I don;t mind doing a little bit of coding so long as I
understood (how to find) all the MS Word variable names.

I'm attaching whatever VB macros I do have, and comments others have
offered. If I seem a bit confused it is from the short attention span I
have
been able to afford to direct towards this project.

.Text = "*\_*"
.Text = "*\**"

\*[!^13]@\*

Sub Foot()

Dim myRange As Range
Dim boolFound As Boolean
Dim myFootnote As Footnote
Set myRange = ActiveDocument.Content
Do
myRange.Find.ClearFormatting
With myRange.Find
.Text = "\[*\]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
End With
myRange.Find.Execute
boolFound = myRange.Find.Found
If boolFound Then
myRange.Characters.Last.Delete
myRange.Characters.First.Delete
myRange.Cut
Set myFootnote = ActiveDocument.Content.Footnotes.Add( _
Range:=myRange)
myFootnote.Range.PasteAndFormat (wdPasteDefault)
Set myRange = ActiveDocument.Content
myRange.Collapse (wdCollapseStart)
Else
Exit Sub
End If
Loop
End Sub

Sub jb()
'
' Macro written by Maniac on May 16, 2006
' Reformats text between two strings
'

' Specify search phrases (start / end)
A$ = "abc"
B$ = "def"

' Start at the beginning and reset all
' search formatting options

ActiveDocument.Range(0).Select
Selection.Find.ClearFormatting
' Loop repeats until first phrase not found
While Selection.Find.Execute(A$)
StartReformat = Selection.End
Selection.MoveRight
Selection.Find.Execute (B$)
StopReformat = Selection.Start
Selection.MoveRight
' Add formatting to the following section
' Options include:
' .Bold, .Italic, .Underline, .StrikeThrough (true / false)
' .Size = font size
' .Font.Color = wdColorGreen (Red, Blue, etc... see help)

With ActiveDocument.Range(StartReformat, StopReformat)
.Bold = True
.Font.Color = wdColorBlue
End With
Wend
End Sub



- = -
Vasos Panagiotopoulos, Columbia'81+, Reagan, Mozart, Pindus, BioStrategist
http://ourworld.compuserve.com/homepages/vjp2/vasos.htm
---{Nothing herein constitutes advice. Everything fully disclaimed.}---
[Homeland Security means private firearms not lazy obstructive guards]
[Urb sprawl confounds terror] [Remorse begets zeal] [Windows is for
Bimbos]
 

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