Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Lounger
    Join Date
    Jan 2002
    Location
    Israel
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    using WordDialogs in VBA (2000 SR-1)

    To all the Word VBA experts

    after calling a built-in Word dialog box from within a macro, how does one
    set a variable to be one of the values entered?

    For example
    Dialogs(wdDialogFormatFont).Display
    FontSize = ?

  2. #2
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: using WordDialogs in VBA (2000 SR-1)

    <pre>With Dialogs(wdDialogFormatFont)
    .Display
    FontSize = .Points
    End With
    </pre>

    Highlight Dialogs and press F1 you'll find a link to help on displaying built-in and to a list of arguments for built-in commands that can be displayed with Dialogs().

  3. #3
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: using WordDialogs in VBA (2000 SR-1)

    The online help lists the available arguments; in this case:
    <table border=1><td>wdDialogFormatFont</td><td>Points, Underline, Color, StrikeThrough, Superscript, Subscript, Hidden, SmallCaps, AllCaps, Spacing, Position, Kerning, KerningMin, Default, Tab, Font, Bold, Italic, DoubleStrikeThrough, Shadow, Outline, Emboss, Engrave, Scale, Animations, CharAccent, FontMajor, FontLowAnsi, FontHighAnsi, CharacterWidthGrid, ColorRGB, UnderlineColor, PointsBi, ColorBi, FontNameBi, BoldBi, ItalicBi, DiacColor</td></table>
    Sometimes, it's nice to have the old Word95 help file (which you can download from Microsoft), because it shows the type (string, integer ...) of the arguments, and allowed values.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  4. #4
    Lounger
    Join Date
    Jan 2002
    Location
    Israel
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: using WordDialogs in VBA (2000 SR-1)

    Thanks, but still problems with this short macro:
    Sub FormatTextBoxes()
    '
    ' FormatTextBoxes Macro
    ' Macro created 11/6/02 by Israel Dalven
    '
    Dim aShape As Shape
    Dim FSize As Long
    Dim FColor As Long
    With Dialogs(wdDialogFormatFont)
    .Display
    FSize = .Points
    FColor = .Color
    End With ' Dialogs...
    For Each aShape In Selection.ShapeRange
    With aShape.TextFrame.TextRange
    .Style = ActiveDocument.Styles("Footer")
    .Font.Size = FSize
    .Font.Color = FColor
    End With 'aShape...
    Next aShape
    End Sub

    Problem one: when run after selecting single text box, the color remains black (FColor=2)
    Problem two: when run with two text boxes selected, the dialog box won't open.
    For a novice like me, this is very confusing! What is going on here ?

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Need to use .Font.ColorIndex

    Wish I'd worked this out before sending the first post!:

    <pre>Sub FormatTextBoxes()
    '
    ' FormatTextBoxes Macro
    ' Macro created 11/6/02 by Israel Dalven
    '
    Dim aShape As Shape
    Dim FSize As Long
    Dim FColor As Long

    If Selection.ShapeRange.Count = 1 Then

    With Dialogs(wdDialogFormatFont)
    .Display
    FSize = .Points
    FColor = .Color
    End With ' Dialogs...

    For Each aShape In Selection.ShapeRange
    With aShape.TextFrame.TextRange
    .Style = ActiveDocument.Styles("Footer")
    .Font.Size = FSize
    .Font.ColorIndex = FColor
    End With 'aShape...
    Next 'aShape

    End If

    End Sub
    </pre>


  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: using WordDialogs in VBA (2000 SR-1)

    Israel,

    Dealing with problem two first: If you select two textboxes in Word interactively, and then click on the Format menu, you'll see that "Font" is grayed out. So it's safe to say that if Word isn't designed to allow the font dialog to show while two textboxes are selected, we're probably not going to have success forcing the dialog to display programatically in that situation.

    It turns out that this code will also cause an error if there are no textboxes in the selection.
    Something like:

    If Selection.ShapeRange.Count = 1 Then

    would enforce that the rest of the code can only run if one textbox is selected.

    Problem one: that one is a mystery since the style change and font size changes do get applied.

    [See other post for solution]

    Gary

  7. #7
    Lounger
    Join Date
    Jan 2002
    Location
    Israel
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need to use .Font.ColorIndex

    Thanks Gary,

    You solved the color problem. Is it not strange that "color" is the property in the dialog box, but Color does not work as the corresponding Font property?
    However, the second issue still is not resolved. The code just does not work. Count is not 1. If you can't think of something right away, I will try something after I finish the work i get paid to do.

    Israel

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

    Re: Need to use .Font.ColorIndex

    Does this work for you? It selects the first shape in the selection to display the dialog, then applies the settings to all shapes in the original selection.

    Sub FormatTextBoxes()
    Dim aShape As Shape
    Dim FSize As Long
    Dim FColor As Long
    Dim rng As Range
    Set rng = Selection.Range
    rng.ShapeRange(1).Select
    With Dialogs(wdDialogFormatFont)
    .Display
    FSize = .Points
    FColor = .Color
    End With
    For Each aShape In rng.ShapeRange
    With aShape.TextFrame.TextRange
    .Style = ActiveDocument.Styles("Footer")
    .Font.Size = FSize
    .Font.ColorIndex = FColor
    End With
    Next
    End Sub

  9. #9
    Lounger
    Join Date
    Jan 2002
    Location
    Israel
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need to use .Font.ColorIndex

    Thanks, It works fine. I will study this to try and understand what the problem was and how to do it right the next time. I get the feeling it is just a matter of experience. The structure is so complex that it is hard to know how to begin.

  10. #10
    Lounger
    Join Date
    Jan 2002
    Location
    Israel
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need to use .Font.ColorIndex

    Hello HansV
    I spoke too soon, it seems. The attached file has a Word Drawing with two visually different types of text boxes. I cannot find any other difference between them. With the larger boxes, the macro works, but not with the smaller. All it does when several are selected is to change the first one and then gives an error. Strange?
    Attached Files Attached Files

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

    Re: Need to use .Font.ColorIndex

    You document also contains line objects (arrows). These don't have a TextFrame property, so they cause an error. What you can do is put

    On Error Resume Next

    at the beginning of the macro. Of course, you won't know if another type of error occurred.

    As an alternative, you might put the On Error Resume Next inside the loop, and only set font color etc. if no error occurred:

    For Each aShape In rng.ShapeRange
    On Error Resume Next
    With aShape.TextFrame.TextRange
    If Err = 0 Then
    .Style = ActiveDocument.Styles("Footer")
    .Font.Size = FSize
    .Font.ColorIndex = FColor
    End If
    End With
    Next

    Hope it works now!

    Regards,
    Hans

  12. #12
    Lounger
    Join Date
    Jan 2002
    Location
    Israel
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need to use .Font.ColorIndex

    Thanks, I just tried it and it does work, but why did it give an error if the arrows were not selected and are therefore not part of the Range and ShapeRange collections?

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

    Re: Need to use .Font.ColorIndex

    All drawing objects in the document you attached to your previous post are anchored to the second (and last) paragraph mark in the document. So as soon as you select the second paragraph, you have effectively selected all drawing objects.

    If you want to select several text boxes by Click and Shift Click, the code needs to be adapted a little bit. At the moment it uses a Range; for shapes selected by Click and Shift Click it should be a ShapeRange:

    Dim aShape As Shape
    Dim FSize As Long
    Dim FColor As Long
    Dim rng As ShapeRange
    Set rng = Selection.ShapeRange
    rng(1).Select
    With Dialogs(wdDialogFormatFont)
    .Display
    FSize = .Points
    FColor = .Color
    End With
    For Each aShape In rng
    On Error Resume Next
    With aShape.TextFrame.TextRange
    If Err = 0 Then
    .Style = ActiveDocument.Styles("Footer")
    .Font.Size = FSize
    .Font.ColorIndex = FColor
    End If
    End With
    Next

  14. #14
    Lounger
    Join Date
    Jan 2002
    Location
    Israel
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need to use .Font.ColorIndex

    Yes, after reading carefully the Help on Range and ShapeRange, I suspected that was the problem, but between my suspicion and your solution lies years of experience!

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

    Re: Need to use .Font.ColorIndex

    No, just trial and error <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

Page 1 of 2 12 LastLast

Posting Permissions

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