Results 1 to 2 of 2

Thread: How to share variables between two forms?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2019

    How to share variables between two forms?

    This is the code under one form (in word) which creates tables and populates info in them. I then want to proceed to a second form and want to work on the same document, but the variables are not carried over. I did try declaring as public in the module, but that did not work.

    Private Sub NextButton_Click()

    Set objword = CreateObject("Word.Application")
    objword.Visible = True
    Set objdoc = objword.Documents.Add
    objword.Selection.PageSetup.Orientation = wdOrientLandscape
    Set objrange = objdoc.Range

    'Create Title Table
    objdoc.Tables.Add objrange, 2, 1
    Set TitleTable = objdoc.Tables(1)
    TitleTable.Borders.Enable = True
    Set myrange = objdoc.Content
    myrange.Collapse Direction:=wdCollapseEnd

    'Create table for info
    objdoc.Tables.Add Range:=myrange, NumRows:=4, _
    Set InfoTable = objdoc.Tables(2)
    InfoTable.Borders.Enable = True
    Set myrange = objdoc.Content
    myrange.Collapse Direction:=wdCollapseEnd

    objdoc.Tables.Add Range:=myrange, NumRows:=6, _
    Set objtable2 = objdoc.Tables(3)

    objtable2.Borders.Enable = True

    With TitleTable
    .Cell(1, 1).Range.InsertAfter "Installation Qualification for" & " " & EquipType.Value & " " & ModelName.Value & " " & SerialNum.Value
    .Cell(2, 1).Range.InsertAfter DocName.Value

    End With

    With InfoTable
    .Cell(1, 1).Range.InsertAfter "Company"
    .Cell(2, 1).Range.InsertAfter "Equipment Type"
    .Cell(3, 1).Range.InsertAfter "Location"
    .Cell(4, 1).Range.InsertAfter "Approver(s)"

    .Cell(1, 2).Range.InsertAfter CompName.Value
    .Cell(2, 2).Range.InsertAfter EquipType.Value
    .Cell(3, 2).Range.InsertAfter RoomNum.Value
    .Cell(4, 2).Range.InsertAfter ApproverNames.Value
    End With

    end sub

    The code in the second form:

    Private Sub AddMore_Click()

    Set myrange = objdoc.Content
    myrange.Collapse Direction:=wdCollapseEnd

    With myrange
    .Collapse Direction:=wdCollapseEnd
    .InsertBreak Type:=wdPageBreak
    End With
    End Sub

    Any ideas how i can get the two forms to share variables? Thanks!

  2. #2
    Join Date
    Dec 2004

    Re: How to share variables between two forms?

    declare the shared variables as public in a std module
    Not in either form code module

    public myvar as string, mynum as integer
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed

Posting Permissions

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


Click Here to Expand Forum to Full Width