brentfraser
Technical User
I am trying to import a range of cells from Excel to a Word content control. Currently I can import a single cell using the following:
As soon as I change "A1" to "A1:A6", it stops working. Is there another convention that I should be using?
The entire code I have is posted below:
Is what I am trying to do even possible? I would be happy to reference a named range of cells in excel too. That seems to make more sense than just rows/columns.....
After doing some reading, I am thinking I have to place the range in an array and then reference that array into the content control I have created. This way all six rows of information in the named range in the excel spreadsheet will be imported into the content control in Word.
Is this the right path to be going down?
I assume this is possible but having a few other sets of eyes on this might get me past this.
Thanks all who have taken the time to look at this.
Brent
Code:
dataInExcel = workBook.Worksheets("Sheet1").Range("A1").Formula
The entire code I have is posted below:
Code:
Public strExcelFile As String
Public Function GetFilePath() As String
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
fd.Title = "Select the file"
fd.Filters.Add "Excel Files", "*.xls; *.xlsx", 1
'fd.Filters.Add "All Files", "*.*"
If fd.Show Then
strExcelFile = fd.SelectedItems(1)
End If
End Function
Public Sub importExcelData()
Dim workBook As workBook
Dim dataInExcel As String
Application.ScreenUpdating = False
GetFilePath
MsgBox strExcelFile
Set workBook = Workbooks.Open(strExcelFile, True, True)
dataInExcel = workBook.Worksheets("Sheet1").Range("A1").Formula
Dim oCC As ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTitle("box1").Item(1)
oCC.Range.Text = dataInExcel
workBook.Close False
Set workBook = Nothing
Application
Is what I am trying to do even possible? I would be happy to reference a named range of cells in excel too. That seems to make more sense than just rows/columns.....
After doing some reading, I am thinking I have to place the range in an array and then reference that array into the content control I have created. This way all six rows of information in the named range in the excel spreadsheet will be imported into the content control in Word.
Is this the right path to be going down?
Code:
Public strExcelFile As String, workbook As String
Public Sub GetFilePath()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
fd.Title = "Select the file"
fd.Filters.Add "Excel Files", "*.xls; *.xlsx", 1
If fd.Show Then
strExcelFile = fd.SelectedItems(1)
End If
End Sub
Public Sub importExcelData()
Dim workbook As workbook
Dim dataInExcel As String
Application.ScreenUpdating = False
GetFilePath
MsgBox strExcelFile
Set workbook = Workbooks.Open(strExcelFile, True, True)
dataInExcel = workbook.Worksheets("Sheet1").Range("A1").Formula
[COLOR=#A40000] Dim Arr() As Variant
Arr = workbook.Worksheets("Sheet1").Range("A1:A6").Value
Dim R As Long
Dim C As Long
For R = 1 To UBound(Arr, 1) ' First array dimension is rows.
For C = 1 To UBound(Arr, 2) ' Second array dimension is columns.
Debug.Print Arr(R, C)
Next C
Next R
Dim oCC_testing As ContentControl
Set oCC_testing = ActiveDocument.SelectContentControlsByTitle("testing").item(1)
oCC_testing.Range.Text = Arr[/color] Dim oCC_box1 As ContentControl
Set oCC_box1 = ActiveDocument.SelectContentControlsByTitle("box1").item(1)
oCC_box1.Range.Text = dataInExcel
workbook.Close False
Set workbook = Nothing
Application.ScreenUpdating = True
End Sub
I assume this is possible but having a few other sets of eyes on this might get me past this.
Thanks all who have taken the time to look at this.
Brent