I have a user that generates several different reports from a non-excel program for multiple departments. These reports are converted and saved into a folder as .xlsb files. I have a macro that is saved inside another folder which formats these reports. I’m building a separate worksheet that will be a control sheet for lack of a better term. From this sheet I trigger a macro which uses the expression “Application.GetOpenFilename” to open a folder containing the excel file and then opens another folder containing the VBA text file.
Currently user navigates to the folder and chooses each of the files that are being used. Then the macro completes its mission by placing the VBA text file inside excel file. The macro works beautifully.
However, what I would like to do is place direct links for the excel and text files into the macro and have this process completed automatically. (The files will always be saves using the same name.)
How can I adjust the existing macro to accomplish this task?
Thank you so much for your help.
------------------------------------------------------------------------------------------------------------------
Sub injectMacro()
Dim vbcomp As Object
Dim wbFn As Variant, txtFn As Variant, wb As Variant
Dim ff As Long
Dim line As String, vbCode As String, fn As String
' -------------------------------------------Excel File----------------------------- I'm wanting to change this to so that it will open a specific file
wbFn = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", , , , True)
If TypeName(wbFn) = "Boolean" Then Exit Sub 'User cancelled
--------------------------------------VBA TXT File------------------------------- I'm wanting to change this to so that it will open a specific file
txtFn = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If TypeName(txtFn) = "Boolean" Then Exit Sub 'User cancelled
With CreateObject("Scripting.FileSystemObject").OpenTextFile(txtFn, 1)
vbCode = .readall
.Close
End With
'Debug.Print vbCode
Application.ScreenUpdating = False
On Error GoTo clean_exit
For Each wb In wbFn
With Workbooks.Open(wb)
Set vbcomp = .VBProject.VBComponents("ThisWorkbook")
vbcomp.CodeModule.AddFromString vbCode
Set vbcomp = Nothing
Select Case UCase(Right(wb, 4))
Case "XLSB", "XLSM", ".XLS"
.Close True
Case Else
'Save as macro enabled workbook
ff = 0
fn = Left(wb, InStrRev(wb, ".")) & "xlsm"
While LenB(Dir(fn))
ff = ff + 1
fn = Left(wb, InStrRev(wb, ".") - 1) & "(" & ff & ").xlsm"
Wend
.SaveAs fn, 52
.Close False
End Select
End With
Next wb
clean_exit:
Application.ScreenUpdating = True
On Error GoTo 0
End Sub
Currently user navigates to the folder and chooses each of the files that are being used. Then the macro completes its mission by placing the VBA text file inside excel file. The macro works beautifully.
However, what I would like to do is place direct links for the excel and text files into the macro and have this process completed automatically. (The files will always be saves using the same name.)
How can I adjust the existing macro to accomplish this task?
Thank you so much for your help.
------------------------------------------------------------------------------------------------------------------
Sub injectMacro()
Dim vbcomp As Object
Dim wbFn As Variant, txtFn As Variant, wb As Variant
Dim ff As Long
Dim line As String, vbCode As String, fn As String
' -------------------------------------------Excel File----------------------------- I'm wanting to change this to so that it will open a specific file
wbFn = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", , , , True)
If TypeName(wbFn) = "Boolean" Then Exit Sub 'User cancelled
--------------------------------------VBA TXT File------------------------------- I'm wanting to change this to so that it will open a specific file
txtFn = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If TypeName(txtFn) = "Boolean" Then Exit Sub 'User cancelled
With CreateObject("Scripting.FileSystemObject").OpenTextFile(txtFn, 1)
vbCode = .readall
.Close
End With
'Debug.Print vbCode
Application.ScreenUpdating = False
On Error GoTo clean_exit
For Each wb In wbFn
With Workbooks.Open(wb)
Set vbcomp = .VBProject.VBComponents("ThisWorkbook")
vbcomp.CodeModule.AddFromString vbCode
Set vbcomp = Nothing
Select Case UCase(Right(wb, 4))
Case "XLSB", "XLSM", ".XLS"
.Close True
Case Else
'Save as macro enabled workbook
ff = 0
fn = Left(wb, InStrRev(wb, ".")) & "xlsm"
While LenB(Dir(fn))
ff = ff + 1
fn = Left(wb, InStrRev(wb, ".") - 1) & "(" & ff & ").xlsm"
Wend
.SaveAs fn, 52
.Close False
End Select
End With
Next wb
clean_exit:
Application.ScreenUpdating = True
On Error GoTo 0
End Sub