Doug:
I really appreciate your recommendations. The one for controlling Excel
works great and so does the filedialog...
I updated the code utilizing the above and added some code for the meat of
what I need to do. Problem is my code is not working now... Specifically if
I retrieve data from Excel and attempt to paste into Word it doesn't work..
An example of what I mean is the following code:
'retrieve value of cell D2 in chosen workbook and place in active word
document at bookmark NovStudyCode_D2
ActiveDocument.Bookmarks("NovStudyCode_D2").Range = Sheets("Novartis IRT
Decision Grid").Range("D2").Value
This exact code worked before....
If I go to the immediate window (while pausing the code) and hover over ?
Range("D2").value I get an error: Method Range of object _ global Failed
Where am I going wrong? Thanks in advance for all your assistance I really
appreciate your direction...
My full code is:
Sub CreatecURS()
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 fd As FileDialog
Dim strWorkbook As String
Set fd = Application.FileDialog(msoFileDialogFilePicker) 'set object as a
file dialog
With fd
.Title = "Select the Excel Workbook to use."
.InitialFileName = "*.xls?"
.AllowMultiSelect = False
If .Show = -1 Then
strWorkbook = .SelectedItems(1) 'Store name of workbook selected
Else
MsgBox "No WorkBook Selected."
Exit Sub
End If
End With
'specify the workbook to work on
WorkbookToWorkOn = strWorkbook 'identify workbook to use for data
'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")
'retrieve value of cell D2 in chosen workbook and place in active word
document at bookmark NovStudyCode_D2
ActiveDocument.Bookmarks("NovStudyCode_D2").Range = Sheets("Novartis IRT
Decision Grid").Range("D2").Value
'retrieve value of cell I286 in chosen workbook and place in active word
document at bookmark SystemSetupType_I286
ActiveDocument.Bookmarks("SystemSetupType_I286").Range = Sheets("Novartis
IRT Decision Grid").Range("I286").Value
'check value of F36 and if yes, update checkbox DistrRegHub_F36 value to true
If Sheets("Novartis IRT Decision Grid").Range("F36").Value = "yes" Then
ActiveDocument.FormFields("DistrRegHub_F36").CheckBox.Value = True
End If
'check value of F37 and if yes, update checkbox DistrCPO_F37 value to true
If Sheets("Novartis IRT Decision Grid").Range("F37").Value = "yes" Then
ActiveDocument.FormFields("DistrCPO_F37").CheckBox.Value = True
End If
'check value of F37 and if yes, update checkbox DistrBoth_F38 value to true
If Sheets("Novartis IRT Decision Grid").Range("F38").Value = "yes" Then
ActiveDocument.FormFields("DistrBoth_F38").CheckBox.Value = True
End If
If Err Then
ExcelWasNotRunning = True
Set oXL = New Excel.Application
End If
On Error GoTo Err_Handler
If ExcelWasNotRunning Then
oXL.Quit
End If
'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