Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Feb 2001
    Thanked 1 Time in 1 Post

    Idiot word VBA question (2000 SP3)

    I have written a macro to change any text in black to automatic, but am once again struggling with the Word object model (I just don't use it often enough). Would anyone be kind enough to correct my code. Currently it stops at the Diming of wd as Word - claiming it was expecting a user defined type, not a project.

    Dim para As Paragraph, wd As Word, char As Characters
    For Each para In ActiveDocument.Paragraphs
    If para.Font.Color = 9999999 Then
    For Each wd In para.Words
    If wd.Font.Color = 9999999 Then
    For Each char In wd.Characters
    If char.Font.Color = wdColorBlack Then char.Font.Color = wdColorAutomatic
    Next char
    ElseIf wd.Font.Color = wdColorBlack Then
    wd.Font.Color = wdColorAutomatic
    End If
    Next wd
    ElseIf para.Font.Color = wdColorBlack Then
    para.Font.Color = wdColorAutomatic
    End If
    Next para


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Thanked 31 Times in 31 Posts

    Re: Word VBA question (2000 SP3)

    In the Word object model, Word is the "root" object. The elements of the Words collection are Range objects, so the declaration should be

    ..., wd As Range, ...

    But why don't you use a Replace All:

    Sub Black2Auto()
    With ActiveDocument.Content.Find
    .Text = ""
    .Font.Color = wdColorBlack
    .Replacement.Text = ""
    .Replacement.Font.Color = wdColorAutomatic
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With
    End Sub

    Since you don't have to loop, this should be much faster.

  3. #3
    3 Star Lounger
    Join Date
    Feb 2001
    Thanked 1 Time in 1 Post

    Re: Word VBA question (2000 SP3)


    Thanks Hans.

    I said it was an idiot question <img src=/S/stupidme.gif border=0 alt=stupidme width=30 height=30>

Posting Permissions

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