Consulting

Results 1 to 3 of 3

Thread: Global Dim for Form in Word

  1. #1

    Global Dim for Form in Word

    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.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    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.

Posting Permissions

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