I'm slowly learning VBA and have achieved some good stuff essentially cutting and pasting from forums, but now I'm stuck.
Using this script here: WordMVP I can open excel files from Word. No problem. However now I have reused this script so many times my modules are starting to get long. Can I split this up into different subs? And call the parts when I need them? e.g: (hacking the linked code to bits)
Put this in one separate sub:
Sub WorkOnAWorkbook(str As String)
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
Dim ExcelWasNotRunning As Boolean
'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
Set oWB = oXL.Workbooks.Open(FileName:=str)
Exit Sub
Err_Handler:
MsgBox WorkbookToWorkOn & " caused a problem. " & Err.Description, vbCritical, _
"Error: " & Err.Number
End Sub
and this in another:
Sub release()
'Make sure you release object references.
Set oRng = Nothing
Set oSheet = Nothing
Set oWB = Nothing
Set oXL = Nothing
End Sub
and use them when I need them, e.g.:
Sub test()
Call WorkOnAWorkbook("somefile.xls")
oWB.Application.Run "Module1.TestMacro", "JasonX"
Call release()
End Sub
I get a 424 Object Required at oWB.Application.Run "Module1.TestMacro", "JasonX".
Is anything like this possible, part of me feels like I'm nearly there, part of me feels like I've totally screwed up, and part of me thinks its not possible...
Can anyone help please??
Thx a million.
Class- heres a good like to start your research - CPearson.com