Results 1 to 14 of 14
  1. #1
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Working Code doesn't work (Word 2000/VBA)

    I have some code that used to work just fine in Word 2000, but has stopped functioning recently. I'm just at a loss to explain why:


    Rem ******Converting Bold to CapSmallCaps in TXT*******
    Home
    If BoldToCSC = 1 Then
    BText "Converting bold to cap and small caps"
    With Selection.Find
    .ClearFormatting
    .Font.Bold = True
    .Text = ""
    With .Replacement
    .ClearFormatting
    .Font.Bold = False
    .Font.SmallCaps = True
    .Text = ""
    End With
    .Execute Format:=True, Replace:=wdReplaceAll
    End With
    End If
    ActiveDocument.UndoClear
    End Sub

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

    Re: Working Code doesn't work (Word 2000/VBA)

    Do you get an error message? Or does the code simply do nothing?

    Your code calls procedures or functions Home and BText that are not standard VBA, so although I can guess what they should do, I have no idea whether they are connected with the problem.

    Also, you use a variable BoldToCSC that is not declared or specified in the code snippet you posted. Perhaps it never becomes 1. Apart from that, the code seems to work OK on my PC.

  3. #3
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Re: Working Code doesn't work (Word 2000/VBA)

    It simply does nothing. It gets skipped over.

    It's part of a larger macro that processes text. It was originally written in WordBasic and updated/rewritten over the years:

    The variable is called for as:

    Dim BoldToCSC As Integer

    Here is the dialog box that calls it:

    Rem ******Convert Bold to CSC or Underline to Italics(Y/N)?******
    WordBasic.BeginDialog 294, 212, "Formatting Options"
    WordBasic.GroupBox 16, 20, 270, 65, "Bold"
    WordBasic.OptionGroup "BtoC"
    WordBasic.OptionButton 26, 40, 125, 20, "Leave as is", "OptionButton1"
    WordBasic.OptionButton 26, 60, 253, 20, "Change to Cap and Small Cap", "OptionButton2"
    WordBasic.GroupBox 16, 100, 270, 65, "Underline", "utoi"
    WordBasic.OptionGroup "UtoI"
    WordBasic.OptionButton 26, 120, 125, 20, "Leave as is", "OptionButton3"
    WordBasic.OptionButton 26, 140, 230, 20, "Change to Italics", "OptionButton4"
    WordBasic.OKButton 96, 180, 88, 21
    WordBasic.EndDialog
    Set dlg = WordBasic.CurValues.UserDialog
    Response = WordBasic.Dialog.UserDialog(dlg, 1)
    BoldToCSC = dlg.btoc
    UndToItal = dlg.utoi
    Home
    Set dlg = Nothing

    Thanks for any help you can give!

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

    Re: Working Code doesn't work (Word 2000/VBA)

    I would set a breakpoint on the line

    BoldToCSC = dlg.btoc

    (click in the line and press F9). When you run the code, it will pause here. You can then single-step through the code by pressing F8, and inspect the value of variables by hovering the mouse over them in the code. My suspicion is that somehow, BoldToCSC does not get the value 1, so the code to change Bold to SmallCaps is skipped.

  5. #5
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Re: Working Code doesn't work (Word 2000/VBA)

    Sorry, I gave you the wrong version of Word it doesn't work in. It's actually 2002. VBA version 6.3.

    I think it's the old WordBasic code that is the issue.

    Thanks for the replies.

  6. #6
    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: Working Code doesn't work (Word 2000/VBA)

    I assume the dialog is being displayed... If you insert Stop statements before critical lines, can diagnose this better. First, can you tell whether the value assigned in the dialog is set correctly when called for in the first snippet you posted? You Find using the Selection object. Is the insertion point where you expect it to be at that point in the code, or has it drifted off somewhere else? Have you or the user inadvertently set any other sticky options in the Find dialog that could be causing issues with the Find? Wildcards?

  7. #7
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Working Code doesn't work (Word 2000/VBA)

    I just tested the WordBasic.Dialog code on Word 2002, VBA 6.3

    I had to add the following line to the beginning to stop VBA displaying a WordBasic "Argument is not optional" error
    <font face="Georgia">Dim dlg As Object</font face=georgia>
    it then correctly set the variables to a value of 0 or 1 depending on selections in the dialog box.

    I then commented out the Btext and Home lines and ran the following code, I started by putting the cursor at the beginning of a document that had some bold text and it correctly converted it all to small caps when requested.

    StuartR

    <font face="Georgia">
    Public Sub testit()
    Dim dlg As Object

    Rem ******Convert Bold to CSC or Underline to Italics(Y/N)?******
    WordBasic.BeginDialog 294, 212, "Formatting Options"
    WordBasic.GroupBox 16, 20, 270, 65, "Bold"
    WordBasic.OptionGroup "BtoC"
    WordBasic.OptionButton 26, 40, 125, 20, "Leave as is", "OptionButton1"
    WordBasic.OptionButton 26, 60, 253, 20, "Change to Cap and Small Cap", "OptionButton2"
    WordBasic.GroupBox 16, 100, 270, 65, "Underline", "utoi"
    WordBasic.OptionGroup "UtoI"
    WordBasic.OptionButton 26, 120, 125, 20, "Leave as is", "OptionButton3"
    WordBasic.OptionButton 26, 140, 230, 20, "Change to Italics", "OptionButton4"
    WordBasic.OKButton 96, 180, 88, 21
    WordBasic.EndDialog
    Set dlg = WordBasic.CurValues.UserDialog
    response = WordBasic.Dialog.UserDialog(dlg, 1)
    BoldToCSC = dlg.btoc
    UndToItal = dlg.utoi

    If BoldToCSC = 1 Then
    <font color=448800> 'BText "Converting bold to cap and small caps"</font color=448800>
    With Selection.Find
    .ClearFormatting
    .Font.Bold = True
    .Text = ""
    With .Replacement
    .ClearFormatting
    .Font.Bold = False
    .Font.SmallCaps = True
    .Text = ""
    End With
    .Execute Format:=True, Replace:=wdReplaceAll
    End With
    End If
    ActiveDocument.UndoClear
    Set dlg = Nothing
    End Sub
    </font face=georgia>

  8. #8
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Re: Working Code doesn't work (Word 2000/VBA)

    Thanks so much for the help guys!!!!! I got it!

    It was not dealing with the variables correctly.

  9. #9
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Re: Working Code doesn't work (Word 2000/VBA)

    Drat! I tried using it in 2003 and it barfed on:

    Private Sub Replace(LookFor, ReplaceWith, wrp)

    On Error Resume Next
    Home
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = LookFor
    .Replacement.Text = ReplaceWith
    .Forward = True
    If wrp = 0 Then .wrap = wdFindStop
    If wrp = 1 Then .wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    End Sub

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

    Re: Working Code doesn't work (Word 2000/VBA)

    If you comment out the line "On Error Resume Next", where exactly does it barf?

  11. #11
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Re: Working Code doesn't work (Word 2000/VBA)

    It just closes Word down and asks if I want to send an error report.

    (They should get someone who knows something besides WordBasic to do this).

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

    Re: Working Code doesn't work (Word 2000/VBA)

    I don't have Word 2003, so I can't offer specific help.

    You might put a breakpoint at the beginning of the code (click in a line and press F9.) When you run the code, it will pause at the breakpoint. You can then execute the code line by line by pressing F8. Go slowly, and note at which line Word crashes. Perhaps someone with Word 2003 will have an idea. You might also post a stripped down version of the document/template (remove sensitive information), so that someome with Word 2003 can test it directly.

  13. #13
    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: Working Code doesn't work (Word 2000/VBA)

    Another thing to try is explicit variable type declarations:

    Private Sub Replace(LookFor As String, ReplaceWith As String, wrp As Integer)

    Not sure that would really help, but you never know. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  14. #14
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Re: Working Code doesn't work (Word 2000/VBA)

    Here is the fulltext of the code. It all works in XP now. Now I have to get it to work in 2003:

Posting Permissions

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