Closed RaymondWise closed 8 years ago
Just some documentation.
Code
Sub GetControls()
Dim targetForm As Object
Set targetForm = 'each form
Dim targetControl As control
For Each targetControl In targetForm.Controls
Debug.Print targetControl.name
Next
End Sub
Results
Label2 txt_cols txt_height Label3 txt_width Label4 btn_grid txt_vOff Label5 txt_hOff Label6 chk_down chk_zoom
list_series btn_setXRange txt_xrange btn_xrangedown txt_yrange btn_yrange btn_ydown
Label1 CommandButton1 CommandButton13 CommandButton15 CommandButton16 CommandButton17 CommandButton18 CommandButton19 CommandButton20 CommandButton21 CommandButton22 CommandButton23 CommandButton25 CommandButton26 CommandButton27 CommandButton28 CommandButton29 CommandButton30 CommandButton31 CommandButton32 CommandButton33 CommandButton34 CommandButton35 CommandButton36 CommandButton37
note: CommandButtons(i) for
i = 1, 13, 15 to 23, 25 to 37
### Replacing control names on the form and in the code
This code will replace the names of the controls in the design of the form and in the code behind the form. Right now I was just testing it by appending NEW
to the old names as the new names, that way I could easily reverse it with len()-3
.
Anyhow, this was kind of fun. You can use it to do your control renaming however you'd like and then just merge that into the project. I figured I'd leave that up to you given they are your forms. I only did the testing on chtSeries
hence the length of the arrays being fixed.
```
Public Sub FindReplaceInModule()
Const CHTGRID As String = "form_chtGrid"
Const CHTSERIES As String = "form_chtSeries"
Dim targetProject As VBIDE.VBProject
Dim targetModule As VBIDE.VBComponent
Dim targetCode As VBIDE.CodeModule
Dim controlIndex As Long
Set targetProject = Application.VBE.VBProjects("bUTLAddIn")
Set targetModule = targetProject.VBComponents(CHTSERIES)
Dim targetForm As Object
Set targetForm = targetModule.Designer
Dim newControlNames() As String
ReDim newControlNames(0 To 6)
Dim oldControlNames() As String
ReDim oldControlNames(0 To 6)
For controlIndex = 0 To 6
oldControlNames(controlIndex) = targetForm.Controls(controlIndex).name
newControlNames(controlIndex) = oldControlNames(controlIndex) & "NEW"
targetForm.Controls(controlIndex).name = newControlNames(controlIndex)
Next
Set targetCode = targetModule.CodeModule
Dim findWhat As String
Dim replaceWith As String
Dim numberOfLines As Long
numberOfLines = targetCode.CountOfLines
Dim lineNumber As Long
Dim lineText As String
For controlIndex = 0 To 6
findWhat = oldControlNames(controlIndex)
replaceWith = newControlNames(controlIndex)
For lineNumber = 1 To numberOfLines
lineText = targetCode.Lines(lineNumber, 1)
If InStr(1, lineText, findWhat, vbTextCompare) > 0 Then
targetCode.ReplaceLine lineNumber, Replace(lineText, findWhat, replaceWith, , , vbTextCompare)
End If
Next
Next
End Sub
```
Nevermind. http://codereview.stackexchange.com/questions/145953/renaming-form-controls-and-underlying-code
In the forms, control object names need to be clarified and adjusted before code variables can be analyzed.