PDA

View Full Version : [SOLVED:] Global Dim for Form in Word



LoneReaper
11-23-2016, 03:47 PM
Good Morning

First time poster, been lurking around for a while to assist with some of my coding needs here and there. If I miss anything in my question please let me know.

What I'm trying to achieve at the moment is a set of Dim parameters that can be used in multiple Private Subs in my form.

For example:
Private Sub ListBox4_Click()

Dim ALB, ALB1F, ALB1L, ALB1E, ALB2F, ALB2L, ALB2E As String
ALB = "CompanyName"
ALB1F = "FirstName"
ALB1L = "LastName"
ALB1E = "Email"
ALB2F = "FirstName"
ALB2L = "LastName"
ALB2E = "Email"

If ListBox4 = ALB Then
Me.ListBox3.Clear
With ListBox3
.AddItem ALB1F & " " & ALB1L
.AddItem ALB2F & " " & ALB2L
End With

I would like this Dim information to be accessible within another sub class such as the following:

Private Sub CommandButton2_Click()

If ListBox4.Text = ALB Then
ActiveDocument.Tables(1).Cell(14, 2).Select

Selection.TypeText Text:=ALB1F & " " & ALB1L & " ("
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=ALB1E, SubAddress:="", ScreenTip:="", _
TextToDisplay:=ALB1E
Selection.TypeText Text:=")"

Set myRange = ActiveDocument.Tables(1).Cell(14, 2).Range
With myRange.Font
.Name = "Verdana"
.Size = 8
End With


I've done some online digging and cannot seem to find the exact solution for the above, or I'm missing some code or incorrectly inputting my code which is all very possible (I'm self teaching myself to do VBA).

Any help would be much appreciated.

gmaxey
11-23-2016, 04:10 PM
Option Explicit
Private ALB, ALB1F, ALB1L, ALB1E, ALB2F, ALB2L, ALB2E As String
'What you have done above is create 1 string variable and six variant variables. If you
'want six 7 strings then dim like ALB as String, ALB1F as String, etc.
Private Sub UserForm_Initialize()
ALB = "CompanyName"
ALB1F = "FirstName"
ALB1L = "LastName"
ALB1E = "Email"
ALB2F = "FirstName"
ALB2L = "LastName"
ALB2E = "Email"
End Sub

LoneReaper
11-23-2016, 04:28 PM
Thank you gmaxey :). Needed this at the top of my form code silly me. This will assist with me cleaning up my code a bit more now.