brent.fraser
06-27-2013, 12:05 PM
Hello there peoples,
I am almost there in creating a master report that pulls information from an Excel spreadsheet and compiles it into Word.
Here's what I have so far:
Public Sub importExcelData()
Dim workBook As workBook
Dim dataInExcel As String
Application.ScreenUpdating = False
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.xlsx", vbNormal)
Set workBook = Workbooks.Open(strFolder & "\Book1.xlsx", 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.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
It's sort-of working. What I would like to do is browse to a specific Excel file since the name of the file is going to change all the time (I have it hard-coded as "Book1.xls" above. The browse function I found only browses to the folder and not a particular file. I found the following code but it seems to only work in Excel:
Sub GetFilePath()
FilePath = Application.GetOpenFilename("Text Files (*.xls*), *.xls*")
If FilePath <> False Then
Dim oCC_filePath As ContentControl
Set oCC_filePath = ActiveDocument.SelectContentControlsByTitle("filePath").Item(1)
End If
End Sub
I am still looking for the right code but it has been difficult to locate so far.
Lastly, in the above code, it is looking for the following range:
dataInExcel = workBook.Worksheets("Sheet1").Range("A1").Formula
As soon as I change the range from A1 to A1:A6, it stops working. I have even tried to name the range in Excel and that doesn't work. Does anyone have a suggestion?
Thanks in advance for your help.
B.
I am almost there in creating a master report that pulls information from an Excel spreadsheet and compiles it into Word.
Here's what I have so far:
Public Sub importExcelData()
Dim workBook As workBook
Dim dataInExcel As String
Application.ScreenUpdating = False
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.xlsx", vbNormal)
Set workBook = Workbooks.Open(strFolder & "\Book1.xlsx", 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.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
It's sort-of working. What I would like to do is browse to a specific Excel file since the name of the file is going to change all the time (I have it hard-coded as "Book1.xls" above. The browse function I found only browses to the folder and not a particular file. I found the following code but it seems to only work in Excel:
Sub GetFilePath()
FilePath = Application.GetOpenFilename("Text Files (*.xls*), *.xls*")
If FilePath <> False Then
Dim oCC_filePath As ContentControl
Set oCC_filePath = ActiveDocument.SelectContentControlsByTitle("filePath").Item(1)
End If
End Sub
I am still looking for the right code but it has been difficult to locate so far.
Lastly, in the above code, it is looking for the following range:
dataInExcel = workBook.Worksheets("Sheet1").Range("A1").Formula
As soon as I change the range from A1 to A1:A6, it stops working. I have even tried to name the range in Excel and that doesn't work. Does anyone have a suggestion?
Thanks in advance for your help.
B.