Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 09-05-2015, 09:43 AM
cyraxote cyraxote is offline userform help (having trouble passing info from form to macro) Windows 7 64bit userform help (having trouble passing info from form to macro) Office 2013
Novice
userform help (having trouble passing info from form to macro)
 
Join Date: Sep 2015
Location: Essex, MD
Posts: 24
cyraxote is on a distinguished road
Question userform help (having trouble passing info from form to macro)

The purpose of the form and macro is to allow the user to search for text highlighted in one color and change it to another color or no color.

This is the form:



This is the code:
Code:
Option Explicit

Private Sub cmdOK_Click()

    Dim SearchColor As String
    Dim ReplaceColor As String

    If optYellow1 = True Then SearchColor = wdYellow
    If optBrightGreen1 = True Then SearchColor = wdBrightGreen
    If optTurquoise1 = True Then SearchColor = wdTurquoise
    If optPink1 = True Then SearchColor = wdPink
    If optBlue1 = True Then SearchColor = wdBlue
    If optRed1 = True Then SearchColor = wdRed
    If optDarkBlue1 = True Then SearchColor = wdDarkBlue
    If optTeal1 = True Then SearchColor = wdTeal
    If optGreen1 = True Then SearchColor = wdGreen
    If optViolet1 = True Then SearchColor = wdViolet
    If optDarkRed1 = True Then SearchColor = wdDarkRed
    If optDarkYellow1 = True Then SearchColor = wdDarkYellow
    If optGray501 = True Then SearchColor = wdGray50
    If optGray251 = True Then SearchColor = wdGray25
    If optBlack1 = True Then SearchColor = wdBlack
    
    If optYellow2 = True Then ReplaceColor = wdYellow
    If optBrightGreen2 = True Then ReplaceColor = wdBrightGreen
    If optTurquoise2 = True Then ReplaceColor = wdTurquoise
    If optPink2 = True Then ReplaceColor = wdPink
    If optBlue2 = True Then ReplaceColor = wdBlue
    If optRed2 = True Then ReplaceColor = wdRed
    If optDarkBlue2 = True Then ReplaceColor = wdDarkBlue
    If optTeal2 = True Then ReplaceColor = wdTeal
    If optGreen2 = True Then ReplaceColor = wdGreen
    If optViolet2 = True Then ReplaceColor = wdViolet
    If optDarkRed2 = True Then ReplaceColor = wdDarkRed
    If optDarkYellow2 = True Then ReplaceColor = wdDarkYellow
    If optGray502 = True Then ReplaceColor = wdGray50
    If optGray252 = True Then ReplaceColor = wdGray25
    If optBlack2 = True Then ReplaceColor = wdBlack
    If optNoColor = True Then ReplaceColor = wdNoHighlight
    
    DoColorChange

End Sub

Private Sub cmdCancel_Click()
    Unload Me
End Sub

Private Sub DoColorChange()

Dim oDoc As Document
Dim oRng As Range
Dim intPosition As Long
'Dim SearchColor As String
'Dim ReplaceColor As String

'SearchColor = wdYellow
'ReplaceColor = wdDarkBlue

Set oDoc = ActiveDocument
Set oRng = oDoc.Range

With oRng.Find
    .Highlight = True
    .Forward = True
    Do While oRng.Find.Execute
        If oRng.HighlightColorIndex = SearchColor Then
           oRng.HighlightColorIndex = ReplaceColor
        End If
        intPosition = oRng.End
        oRng.Start = intPosition
    Loop
End With

End Sub
The DoColorChange macro works fine when it's not in the form and I feed it the info with the red statements above (commented out for the form code). When I use it in the form, I get one of two errors:



1) If the Dim statements for SearchColor and ReplaceColor in DoColorChange are commented out (as above), I get "Compile error: Variable not defined."

2) If those Dim statements are active, I get "Run-time error '13': Type mismatch.

I'm not really sure where to define those variables (OK button code? macro?), what to define them as (since there's no type mismatch outside the form), and how to get the information from the form to the macro.

If I put a breakpoint at
If optNoColor = True Then ReplaceColor = wdNoHighlight
I can hover the mouse and see that the variables for the colors I select in the form are true, and the others are false, so I'm fairly certain that code is OK.

I've been fiddling with this for hours, so I think I'm officially stumped.

Any help would be greatly appreciated.
Reply With Quote
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to copy cell info to multiple documents Patrick Innes Word VBA 2 02-18-2015 08:38 PM
userform help (having trouble passing info from form to macro) Trouble automatically running user form Lostinvba Word VBA 3 11-23-2013 07:22 PM
userform help (having trouble passing info from form to macro) I'm having trouble creating a Merge using label form and placing a shape in the text blockie Mail Merge 8 11-13-2013 11:28 PM
userform help (having trouble passing info from form to macro) Macro trouble Zack Excel 2 10-14-2010 12:07 PM
Trouble updating a form TomCaesar Outlook 0 11-29-2006 10:52 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:35 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft