L
LEU
I am having problems populating a table from the form I have created. Doug
Robbins posted that I look at Greg Maxey’s website. I did this but I am still
having trouble. With the following macros I can create my table and open the
form with the info to populate the table. The list boxes in the form are
loaded from a table in a Word document on my C drive. What I want to do is
pick one or more of the choices in the first list box and it would then load
the choice(s) in the first list box into the first column of the table and
the info from the associated second list box into the second column of the
table. If I picked more than one item in the list box then it would load my
second choice in the second row column one and column two and so on.
Here are my macros:
Sub WarningBox()
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:= _
1, DefaultTableBehavior:=wdWord9TableBehavior,
AutoFitBehavior:=wdAutoFitFixed
Selection.Tables(1).Rows.SetLeftIndent LeftIndent:=8, RulerStyle:=wdAdjustNone
Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=432,
RulerStyle:=wdAdjustNone
Selection.Tables(1).Borders.OutsideLineWidth = wdLineWidth150pt
Selection.ParagraphFormat.Style = "Normal"
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Font.Size = 18
Selection.Font.Color = wdColorRed
Selection.Font.Bold = wdToggle
Selection.Font.Underline = wdUnderlineSingle
Selection.TypeText Text:="Warning"
Selection.Font.Underline = wdUnderlineNone
Selection.Font.Bold = wdToggle
Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeBackspace
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 6
.SpaceAfterAuto = False
End With
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Cells.Split NumRows:=1, NumColumns:=2, MergeBeforeSplit:=False
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.MoveRight Unit:=wdCell
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="POTENTIAL HAZARDS"
Selection.MoveRight Unit:=wdCell
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="CONTROLS"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Cells.Split NumRows:=1, NumColumns:=2, MergeBeforeSplit:=False
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Hazard1"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight Unit:=wdCell
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Controls1"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
UserForm1.Show
End Sub
Here are my forms macros:
Option Explicit
Private Sub UserForm_Initialize()
Dim myArray() As Variant
Dim sourcedoc As Document
Dim i As Integer
Dim j As Integer
Dim myitem As Range
Dim m As Long
Dim n As Long
Application.ScreenUpdating = False
Set sourcedoc = Documents.Open(FileName:="C:\Hazard.doc", Visible:=False)
i = sourcedoc.Tables(1).Rows.Count - 1
j = sourcedoc.Tables(1).Columns.Count
ListBox1.ColumnCount = j
'Hide column 2
ListBox1.ColumnWidths = "75;0"
ReDim myArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
myArray(m, n) = myitem.Text
Next m
Next n
'Load data into ListBox1
ListBox1.List() = myArray
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub
Private Sub CommandButton1_Click()
Dim i As Integer, Hazards As String, Controls As String
Hazards = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
If ListBox1.Value <> "" Then
Hazards = Hazards & ListBox1.Value '& vbCr
FillBookmark "Hazard1", Hazards
End If
Next i
FillBookmark "Hazard1", Hazards
Controls = ""
For i = 1 To ListBox2.ColumnCount
ListBox2.BoundColumn = i
If ListBox2.Value <> "" Then
Controls = Controls & ListBox2.Value '& vbCr
FillBookmark "Controls1", Controls
End If
Next i
FillBookmark "Controls1", Controls
Me.Hide
End Sub
Private Sub ListBox1_Change()
Dim myArray As Variant
myArray = Split(ListBox1.List(ListBox1.ListIndex, 1), Chr(13))
ListBox2.List = myArray
End Sub
Private Sub ListBox2_Change()
Dim i As Long
Dim lngCount As Long
lngCount = 0
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
lngCount = lngCount + 1
End If
Next i
End Sub
Private Sub FillBookmark(bkName As String, bkVal As String)
Dim oRg As Range
With ActiveDocument.Bookmarks
If .Exists(bkName) Then
Set oRg = .Item(bkName).Range
oRg.Text = bkVal
..Add Name:=bkName, Range:=oRg
End If
End With
End Sub
Here is an example of my table info from the document on my C drive that I
am loading into the form:
Row One
Column One = Hazard
Column Two = Controls
Row Two
Column One = Chemical Exposure Hazard
Column Two = Avoid contact with eyes.
Avoid breathing vapor or mist.
Wash hands thoroughly after use.
Review MSDS for each chemical
Locate pressurized portable eye wash station near work area.
Use nitrile gloves when applying chemicals
Row Three
Column One = Electrical Hazards
Column Two = Inspect tools/equipment for damage prior to use. Do not use
damaged tools or equipment
Use ground fault circuit interrupters (GFCI).
LEU
Robbins posted that I look at Greg Maxey’s website. I did this but I am still
having trouble. With the following macros I can create my table and open the
form with the info to populate the table. The list boxes in the form are
loaded from a table in a Word document on my C drive. What I want to do is
pick one or more of the choices in the first list box and it would then load
the choice(s) in the first list box into the first column of the table and
the info from the associated second list box into the second column of the
table. If I picked more than one item in the list box then it would load my
second choice in the second row column one and column two and so on.
Here are my macros:
Sub WarningBox()
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:= _
1, DefaultTableBehavior:=wdWord9TableBehavior,
AutoFitBehavior:=wdAutoFitFixed
Selection.Tables(1).Rows.SetLeftIndent LeftIndent:=8, RulerStyle:=wdAdjustNone
Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=432,
RulerStyle:=wdAdjustNone
Selection.Tables(1).Borders.OutsideLineWidth = wdLineWidth150pt
Selection.ParagraphFormat.Style = "Normal"
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Font.Size = 18
Selection.Font.Color = wdColorRed
Selection.Font.Bold = wdToggle
Selection.Font.Underline = wdUnderlineSingle
Selection.TypeText Text:="Warning"
Selection.Font.Underline = wdUnderlineNone
Selection.Font.Bold = wdToggle
Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeBackspace
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 6
.SpaceAfterAuto = False
End With
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Cells.Split NumRows:=1, NumColumns:=2, MergeBeforeSplit:=False
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.MoveRight Unit:=wdCell
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="POTENTIAL HAZARDS"
Selection.MoveRight Unit:=wdCell
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="CONTROLS"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Cells.Split NumRows:=1, NumColumns:=2, MergeBeforeSplit:=False
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Hazard1"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveRight Unit:=wdCell
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Controls1"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
UserForm1.Show
End Sub
Here are my forms macros:
Option Explicit
Private Sub UserForm_Initialize()
Dim myArray() As Variant
Dim sourcedoc As Document
Dim i As Integer
Dim j As Integer
Dim myitem As Range
Dim m As Long
Dim n As Long
Application.ScreenUpdating = False
Set sourcedoc = Documents.Open(FileName:="C:\Hazard.doc", Visible:=False)
i = sourcedoc.Tables(1).Rows.Count - 1
j = sourcedoc.Tables(1).Columns.Count
ListBox1.ColumnCount = j
'Hide column 2
ListBox1.ColumnWidths = "75;0"
ReDim myArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
myArray(m, n) = myitem.Text
Next m
Next n
'Load data into ListBox1
ListBox1.List() = myArray
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub
Private Sub CommandButton1_Click()
Dim i As Integer, Hazards As String, Controls As String
Hazards = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
If ListBox1.Value <> "" Then
Hazards = Hazards & ListBox1.Value '& vbCr
FillBookmark "Hazard1", Hazards
End If
Next i
FillBookmark "Hazard1", Hazards
Controls = ""
For i = 1 To ListBox2.ColumnCount
ListBox2.BoundColumn = i
If ListBox2.Value <> "" Then
Controls = Controls & ListBox2.Value '& vbCr
FillBookmark "Controls1", Controls
End If
Next i
FillBookmark "Controls1", Controls
Me.Hide
End Sub
Private Sub ListBox1_Change()
Dim myArray As Variant
myArray = Split(ListBox1.List(ListBox1.ListIndex, 1), Chr(13))
ListBox2.List = myArray
End Sub
Private Sub ListBox2_Change()
Dim i As Long
Dim lngCount As Long
lngCount = 0
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
lngCount = lngCount + 1
End If
Next i
End Sub
Private Sub FillBookmark(bkName As String, bkVal As String)
Dim oRg As Range
With ActiveDocument.Bookmarks
If .Exists(bkName) Then
Set oRg = .Item(bkName).Range
oRg.Text = bkVal
..Add Name:=bkName, Range:=oRg
End If
End With
End Sub
Here is an example of my table info from the document on my C drive that I
am loading into the form:
Row One
Column One = Hazard
Column Two = Controls
Row Two
Column One = Chemical Exposure Hazard
Column Two = Avoid contact with eyes.
Avoid breathing vapor or mist.
Wash hands thoroughly after use.
Review MSDS for each chemical
Locate pressurized portable eye wash station near work area.
Use nitrile gloves when applying chemicals
Row Three
Column One = Electrical Hazards
Column Two = Inspect tools/equipment for damage prior to use. Do not use
damaged tools or equipment
Use ground fault circuit interrupters (GFCI).
LEU