I'm stuck coding a command button for word. The button (in a word document) needs to call a dialog to specify the file name for an excel workbook, then copy a named range and paste it back into word as a picture. the copy and paste part is fairly straightforward, but getting the file name dialog is not working for me.
Pretty much every example I found specifies the excel file name within the code, not from a dialog
My code so far (tried to clean it up as far as possible, there was a lot of trial and error)
Sub CRA_copy()
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
Dim ExcelWasNotRunning As Boolean
Dim WorkbookToWorkOn As String
Dim dlgOpen As FileDialog
Dim crabook As String
oName = ActiveDocument.Name
'If Excel is running, get a handle on it; otherwise start a new instance of Excel
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")
If Err Then
ExcelWasNotRunning = True
Set oXL = New Excel.Application
End If
On Error GoTo Err_Handler
'Open the workbook
crabook = Application.GetOpenFilename( _
filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=False)
'Process each of the spreadsheets in the workbook
oXL.ActiveWorkbook.Range("CRA").Copy
If ExcelWasNotRunning Then
oXL.Quit
End If
oName.Activate
Selection.EndKey Unit:=wdStory
Document.InsertBreak Type:=wdPageBreak
Selection.Paste
'Make sure you release object references.
Set oRng = Nothing
Set oSheet = Nothing
Set oWB = Nothing
Set oXL = Nothing
'quit
Exit Sub
Err_Handler:
MsgBox WorkbookToWorkOn & " caused a problem. " & Err.Description, vbCritical, _
"Error: " & Err.Number
If ExcelWasNotRunning Then
oXL.Quit
End If
End Sub