Hello everyone,
I am really new to VBA programming. I am attempting to write a code to check if a workbook is already opened, then make it active and then assign a variable name to it before extracting data from the cells and placing in cells of MS Word table. I found a function online which I am attempting to use but I am having trouble in activating the workbook if it's opened.
I have attached the code I am attempting to use below. Thanks for your help.
Function IsFileOpen(FileName As String)
Dim iFilenum As Long
Dim iErr As Long
On Error Resume Next
iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
On Error GoTo 0
Select Case iErr
Case 0: IsFileOpen = False
Case 70: IsFileOpen = True
Case Else: Error iErr
End Select
End Function
Sub ExtractNumbers()
'Declare Excel variables
Dim xlsApp As New Excel.Application ------ For this to work I must set as "New Excel.Application"
Dim xlsDoc As Workbook
Dim xlsSht As Excel.Worksheet
Dim LastRow As Long
'Declare Word variables
Dim wrdApp As Object
Dim wrdDoc As Word.Document
Dim wrdTbl As Object
'Open Excel Workbook and Make "Project Summary" active worksheet
If Not IsFileOpen("F:\Copy of Orders Required.xlsx") Then
xlsApp.Visible = True
Set xlsDoc = Workbooks.Open("F:\Orders.xlsx", UpdateLinks, ReadOnly, False)
Else
Set xlsDoc = Workbooks("Orders.xlsx")------ I would like to do something like this
but Workbooks("Orders.xlsx").Activate does not work either. I am getting Subscript Out of Range Error (Run Time: Error 9)
End If
xlsDoc.Activate
Worksheets("Project Summary").Activate
Set xlsSht = ActiveSheet
I am really new to VBA programming. I am attempting to write a code to check if a workbook is already opened, then make it active and then assign a variable name to it before extracting data from the cells and placing in cells of MS Word table. I found a function online which I am attempting to use but I am having trouble in activating the workbook if it's opened.
I have attached the code I am attempting to use below. Thanks for your help.
Function IsFileOpen(FileName As String)
Dim iFilenum As Long
Dim iErr As Long
On Error Resume Next
iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
On Error GoTo 0
Select Case iErr
Case 0: IsFileOpen = False
Case 70: IsFileOpen = True
Case Else: Error iErr
End Select
End Function
Sub ExtractNumbers()
'Declare Excel variables
Dim xlsApp As New Excel.Application ------ For this to work I must set as "New Excel.Application"
Dim xlsDoc As Workbook
Dim xlsSht As Excel.Worksheet
Dim LastRow As Long
'Declare Word variables
Dim wrdApp As Object
Dim wrdDoc As Word.Document
Dim wrdTbl As Object
'Open Excel Workbook and Make "Project Summary" active worksheet
If Not IsFileOpen("F:\Copy of Orders Required.xlsx") Then
xlsApp.Visible = True
Set xlsDoc = Workbooks.Open("F:\Orders.xlsx", UpdateLinks, ReadOnly, False)
Else
Set xlsDoc = Workbooks("Orders.xlsx")------ I would like to do something like this
but Workbooks("Orders.xlsx").Activate does not work either. I am getting Subscript Out of Range Error (Run Time: Error 9)
End If
xlsDoc.Activate
Worksheets("Project Summary").Activate
Set xlsSht = ActiveSheet