SpookyAwol
02-05-2014, 03:32 PM
BACKGROUND
Word / VB template system. Userform to define what documents are generated and custom content. Generates up to 20 unique documents.
Currently data (for drop downs) is stored in a word document in tabular format which is then put into an array then multiselect box. A user can select one or multiple records.
I maintain a controlled source for this word document in an excel spreadsheet. I cut and paste any updates from the excel (as a whole new table) into my existing word document.
QUESTION
Can I save myself a step and increase code efficiency/speed by just querying the excel spreadsheet directly?
If worthwhile, tips on code?
CURRENT CODE
Set Sourcedoc = Documents.Open(FileName:=sFileName + "source.doc") i = Sourcedoc.Tables(1).Rows.Count - 1
ListBox1.ColumnCount = 5
ListBox4.ColumnCount = 5
Dim MyArray() As Variant
ReDim MyArray(i, 5)
For n = 0 To 4 ' 0 to 4 columns (i place holder)
For m = 0 To i - 1 ' 0 to i lines
If n = 4 Then
MyArray(m, n) = m
Else
Set myitem = Sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Set myitem = Nothing
End If
Next m
Next n
Sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
Set Sourcedoc = Nothing
Word / VB template system. Userform to define what documents are generated and custom content. Generates up to 20 unique documents.
Currently data (for drop downs) is stored in a word document in tabular format which is then put into an array then multiselect box. A user can select one or multiple records.
I maintain a controlled source for this word document in an excel spreadsheet. I cut and paste any updates from the excel (as a whole new table) into my existing word document.
QUESTION
Can I save myself a step and increase code efficiency/speed by just querying the excel spreadsheet directly?
If worthwhile, tips on code?
CURRENT CODE
Set Sourcedoc = Documents.Open(FileName:=sFileName + "source.doc") i = Sourcedoc.Tables(1).Rows.Count - 1
ListBox1.ColumnCount = 5
ListBox4.ColumnCount = 5
Dim MyArray() As Variant
ReDim MyArray(i, 5)
For n = 0 To 4 ' 0 to 4 columns (i place holder)
For m = 0 To i - 1 ' 0 to i lines
If n = 4 Then
MyArray(m, n) = m
Else
Set myitem = Sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Set myitem = Nothing
End If
Next m
Next n
Sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
Set Sourcedoc = Nothing