I'm trying to create a new excel workbook where I'll put in data from an array I generated.
I keep getting this error, "Run-time error '424': Object Required"
I'm using code that I found online at this link.
Set NewBook = app.Workbooks.Add
With NewBook
.Title = "Cable Numbers"
.Subject = "Documentation"
'parentFld is a file path where a previous document is being used
.SaveAs FileName:=parentFld & "CableNumbers.xlsx"
End With
The workbook isn't even being created. How can I create a new workbook if my macro is in a word document?
Here's some more of my code for reference:
Function SelectFile()
With Application.FileDialog(msoFileDialogFilePicker)
' show the file picker dialog box
If .Show <> 0 Then
SelectFile = .SelectedItems(1)
End If
End With
End Function
Function PrintValue(PrintRow As Long, PrintCol As Long, ArrayName As ArrayList, ArrayIndex As Long)
'Probably doesn't work. I want to print the value from the array into a spreadsheet's first column
app.ActiveSheet.Cells(PrintRow, PrintCol) = ArrayName(ArrayIndex)
End Function
Sub getFirstColumn()
' Gets the first column of all tables in a given file
'Declare variables
Dim wbName As String
Dim TableCount, tableNum As Integer 'This is the number of tables in thedocument minus 1 since loop starts at 0'
Dim dataCell As Variant
Dim printRowCounter, indexCounter As Long
Dim docO As Document
Dim app As Object
Dim fileObj, fldObj As New FileSystemObject
Set fileObj = CreateObject("Scripting.FileSystemObject")
Set fldObj = CreateObject("Scripting.FileSystemObject")
Set app = CreateObject("Excel.Application")
printRowCounter = 1
indexCounter = 0
'Choose a file to get data from. This is a ms Word doc
MsgBox "Choose a workbook file. This file must have a table in it."
wbPath = SelectFile()
wbName = CStr(fileObj.GetFileName(wbPath))
Documents(wbName).Activate
parentFld = fldObj.GetParentFolderName(wbPath)
TableCount = ActiveDocument.Tables.Count
Dim firstColArray As ArrayList
Set firstColArray = New ArrayList
For tableNum = 1 To TableCount
RowCount = ActiveDocument.Tables(tableNum).Range.Rows.Count
For rowNum = 1 To RowCount
ActiveDocument.Tables(tableNum).Cell(Row:=rowNum, Column:=1).Select 'For testing purposes
cellContent = ActiveDocument.Tables(tableNum).Cell(Row:=rowNum, Column:=1)
If cellContent = "" Then
GoTo Skip
Else
dataCell = ActiveDocument.Tables(tableNum).Cell(Row:=rowNum, Column:=1)
firstColArray.Add dataCell
End If
Skip:
Next rowNum
Next tableNum
MsgBox "All data has been collected." & vbNewLine & "Creating an excel file..."
'Creating an excel workbook and inputing data
Set NewBook = app.Workbooks.Add
With NewBook
.Title = "Numbers"
.Subject = "Documentation"
.SaveAs.Close FileName:=parentFld & "\Numbers.xlsx"
End With
app.Workbooks("Numbers.xlsx").Activate
For Each thing In firstColArray
app.ActiveSheet.Cells(printRowCounter, 1) = firstColArray(indexCounter)
printRowCounter = printRowCounter + 1
indexCounter = indexCounter + 1
Next thing
End Sub
Function FileExists(FName As String) As Boolean
' Returns True if the file FName exists, else False
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
FileExists = fs.FileExists(FName)
Set fs = Nothing
End Function
Set oxlApp = CreateObject("Excel.Application")
excelStatus = FileExists(CStr(newExcelPath))
If excelStatus = True Then 'If the file exists, append to it
excelStatus = "append"
ElseIf excelStatus = False Then 'If the file doesn't exist, create a new file
excelStatus = "new"
Set NewBook = oxlApp.Workbooks.Add
With NewBook
.Title = "Cable Numbers"
.Subject = "Documentation"
.SaveAs fileName:=newExcelPath
.Close
End With
End If
Set oxlWbk = oxlApp.Workbooks.Open(fileName:=newExcelPath) 'Open
If excelStatus = "new" Then 'If the excel is new, select the first sheet
'oxlWbk.ActiveWorkbook.Sheets(1).Select
ElseIf excelStatus = "append" Then 'If the excel is old, add a sheet to the end and select it
oxlApp.ActiveWorkbook.Sheets.Add After:=oxlWbk.Sheets(oxlWbk.Sheets.Count)
oxlApp.ActiveWorkbook.Sheets(oxlWbk.Sheets.Count).Select
End If
Related
The below mention code can successfully count the required tags in an XML files and also provides name of file and tag count in excel sheet. I have just one query that currently it only reads the folder individually. However if there are 300 folders in a parent folder i need to select each folder every time. Is there anyway if anyone can amend the code so that if there are 300 folders in a parent folder in read each and every file (XML) in all subfolders. This will be very helpful for me.
I have tried to do it my own but this is beyond my capacity.
Option Explicit
Sub process_folder()
Dim iRow As Long, wb As Workbook, ws As Worksheet, ar
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
ws.UsedRange.Clear
ws.Range("A1:C1") = Array("Source", "<Headline> Tag Count")
iRow = 1
' create FSO and regular expression pattern
Dim FSO As Object, ts As Object, regEx As Object, txt As String
Set FSO = CreateObject("Scripting.FileSystemObject")
Set regEx = CreateObject("VBScript.RegExp")
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = True
.pattern = "<Headline>(.*)</Headline>"
End With
'Opens the folder picker dialog to allow user selection
Dim myfolder As String, myfile As String, n As Long
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
Exit Sub
End If
myfolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
End With
'Loop through all files in a folder until DIR cannot find anymore
Application.ScreenUpdating = False
myfile = Dir(myfolder & "*.xml")
Do While myfile <> ""
iRow = iRow + 1
ws.Cells(iRow, 1) = myfile
' open file and read all lines
Set ts = FSO.OpenTextFile(myfolder & myfile)
txt = ts.ReadAll
ts.Close
' count pattern matches
Dim m As Object
If regEx.test(txt) Then
Set m = regEx.Execute(txt)
ws.Cells(iRow, 2) = m(0).SubMatches(0) ' get date from first match
ws.Cells(iRow, 3) = m.Count
Else
ws.Cells(iRow, 2) = "No tags"
ws.Cells(iRow, 3) = 0
End If
myfile = Dir 'DIR gets the next file in the folder
Loop
' results
ws.UsedRange.Columns.AutoFit
Application.ScreenUpdating = True
End Sub
Use Subfolders property of the parent folder object.
Option Explicit
Sub process_folder()
Dim iRow As Long, wb As Workbook, ws As Worksheet, ar
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
ws.UsedRange.Clear
ws.Range("A1:B1") = Array("Source", "<Headline> Tag Count")
iRow = 1
' create FSO and regular expression pattern
Dim fso As Object, ts As Object, regEx As Object, txt As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set regEx = CreateObject("VBScript.RegExp")
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = "<Headline>(.*)</Headline>"
End With
'Opens the folder picker dialog to allow user selection
Dim myfolder, myfile As String, n As Long
Dim parentfolder As String, oParent
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
Exit Sub
End If
parentfolder = .SelectedItems(1) 'Assign selected folder to ParentFolder
End With
Set oParent = fso.getFolder(parentfolder)
' build collection or files
Dim colFiles As Collection
Set colFiles = New Collection
Call GetFiles(oParent, "xml", colFiles)
'Loop through all files in collection
Application.ScreenUpdating = False
For n = 1 To colFiles.Count
myfile = colFiles(n)
iRow = iRow + 1
ws.Cells(iRow, 1) = myfile
' open file and read all lines
Set ts = fso.OpenTextFile(myfile)
txt = ts.ReadAll
ts.Close
' count pattern matches
Dim m As Object
If regEx.test(txt) Then
Set m = regEx.Execute(txt)
ws.Cells(iRow, 2) = m(0).SubMatches(0) ' get date from first match
ws.Cells(iRow, 3) = m.Count
Else
ws.Cells(iRow, 2) = "No tags"
ws.Cells(iRow, 3) = 0
End If
' results
ws.UsedRange.Columns.AutoFit
Next
Application.ScreenUpdating = True
MsgBox colFiles.Count & " Files process", vbInformation
End Sub
Sub GetFiles(oFolder, ext, ByRef colFiles)
Dim f As Object
For Each f In oFolder.Files
If f.Name Like "*." & ext Then
colFiles.Add oFolder.Path & "\" & f.Name
End If
Next
' call recursively
For Each f In oFolder.subfolders
Call GetFiles(f, ext, colFiles)
Next
End Sub
Loop Through All Folders and Subfolders
In this post under the title Subfolder Paths to Collection, you can find the CollSubfolderPaths function, which will return the paths of all folders and their subfolders in a collection.
In your code, you could utilize it in the following way.
Sub process_folder()
' Preceding code...
Application.ScreenUpdating = False
' Return the paths of all folders and subfolders in a collection.
Dim MyFolders As Collection: Set MyFolders = CollSubfolderPaths(myfolder)
Dim Item As Variant
' Loop through the items in the collection.
For Each Item In MyFolders
' Get the first file.
myfile = Dir(Item & "\" & "*.xml")
'Loop through all files in a folder until DIR cannot find anymore
Do While myfile <> ""
' The same code...
Loop
Next Item
' results
ws.UsedRange.Columns.AutoFit
Application.ScreenUpdating = True
End Sub
I have 2 problems regarding FileDialog.
The below code is to copy a file from another folder into another. But if it couldn't locate that file, it would open the FileDialog to select the file.
Problems:
When the FileDialog is opened, it would instead default to Documents and not the AltPath.
Is it possible to select 2 or more files with FileDialog without resorting to loop?
Dim fso As Object
Dim ws As Worksheet
Dim targetFile As Object
Dim S_Line As Long
Dim BasePath As String
Dim AltPath As String
Dim AltPath2 As String
Dim MainPath As String
Dim NewPath As String
Dim Position As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = ActiveSheet
BasePath = "Z:\Test\Folder\"
AltPath = "B:\Test\Folder\"
MainPath = BasePath & "File.xlsm"
NewPath = "D:\Folder\"
S_Line = 0
Position = UCase(Trim(ws.Cells(R_Line, 8).Value2))
If Position = "OK" Then
If Right(MainPath, 1) = "\" Then
MainPath = Left(MainPath, Len(MainPath) - 1)
End If
If fso.FileExists(MainPath) = True Then
fso.CopyFile Source:=MainPath, Destination:=NewPath
Else
Do While S_Line < 2
Set targetFile = Application.FileDialog(msoFileDialogFilePicker)
With targetFile
.Title = "Select a File"
.AllowMultiSelect = True
.InitialFolderName = AltPath
If .Show <> -1 Then
MsgBox "You didn't select anything"
Exit Sub
End If
AltPath2 = .SelectedItems(1)
End With
fso.CopyFile Source:=AltPath2, Destination:=NewPath
S_Line = S_Line + 1
Loop
End If
You did not answer my clarification question and your question is not so clear. Please, test the next code. It will open the dialog in the folder you need, and copy the selected items in the folder you want. I commented the lines being strictly connected to your environment (Position, S_Line), since I cannot deduce which are they and how to be used:
Sub copyFileSourceDest()
Dim fso As Object
Dim ws As Worksheet
Dim AltPath2 As String
Dim MainPath As String
Dim NewPath As String
Dim Position As String
Const AltPath As String = "B:\Test\Folder\"
Const BasePath As String = "Z:\Test\Folder\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = ActiveSheet
MainPath = BasePath & "File.xlsm"
NewPath = ThisWorkbook.path & "\NewFold\" ' "D:\Folder\"
'Position = UCase(Trim(ws.cells(R_Line, 8).Value2))
'If Position = "OK" Then
'the following sequence looks useless, since it is a FILE path:
'If Right(MainPath, 1) = "\" Then
' MainPath = left(MainPath, Len(MainPath) - 1)
'End If
If fso.FileExists(MainPath) = True Then
fso.CopyFile Source:=MainPath, Destination:=NewPath
Else
Dim item As Variant
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select a File"
.AllowMultiSelect = True
'.InitialFolderName = AltPath 'it does not exist in this Dialog type
.InitialFileName = AltPath
If .Show <> -1 Then
MsgBox "You didn't select anything"
Exit Sub
End If
For Each item In .SelectedItems
AltPath2 = item
fso.CopyFile Source:=AltPath2, Destination:=NewPath
Next
End With
End If
'End If
End Sub
It will simple copy (all) files you select in the Dialog. Not understanding why necessary a loop as your code tried...
I have a folder containing about 500-600 excel files from a script I have made where the file names end up like this
101a12345.xlsx
101a67899.xlsx
102a12345.xlsx
102a78999.xlsx
The file names follow that patern, 101a, 102a etc. What i want to do is merge those based on that paternt into 1 excel file. Therefore, the 101a12345.xlsx and 101a67899.xlsx should merge into an 101aMaster.xlsx. All excel files are single sheet.
I have found a sample code here which i am trying to implement: How to merge multiple workbooks into one based on workbooks names
Taken from the link above:
Sub test(sourceFolder As String, destinationFolder As String)
Const TO_DELETE_SHEET_NAME As String = "toBeDeleted"
'------------------------------------------------------------------
Dim settingSheetsNumber As Integer
Dim settingDisplayAlerts As Boolean
Dim dict As Object
Dim wkbSource As Excel.Workbook
Dim wks As Excel.Worksheet
Dim filepath As String
Dim code As String * 4
Dim wkbDestination As Excel.Workbook
Dim varKey As Variant
'------------------------------------------------------------------
'Change [SheetsInNewWorkbook] setting of Excel.Application object to
'create new workbooks with a single sheet only.
With Excel.Application
settingDisplayAlerts = .DisplayAlerts
settingSheetsNumber = .SheetsInNewWorkbook
.SheetsInNewWorkbook = 1
.DisplayAlerts = False
End With
Set dict = VBA.CreateObject("Scripting.Dictionary")
filepath = Dir(sourceFolder)
'Loop through each Excel file in folder
Do While filepath <> ""
If VBA.Right$(filepath, 5) = ".xlsx" Then
Set wkbSource = Excel.Workbooks.Open(sourceFolder & filepath)
Set wks = wkbSource.Worksheets(1)
code = VBA.Left$(wkbSource.Name, 4)
'If this code doesn't exist in the dictionary yet, add it.
If Not dict.exists(code) Then
Set wkbDestination = Excel.Workbooks.Add
wkbDestination.Worksheets(1).Name = TO_DELETE_SHEET_NAME
Call dict.Add(code, wkbDestination)
Else
Set wkbDestination = dict.Item(code)
End If
Call wks.Copy(Before:=wkbDestination.Worksheets(1))
wkbDestination.Worksheets(1).Name = VBA.Mid$(filepath, 6)
Call wkbSource.Close(False)
End If
filepath = Dir
Loop
'Save newly created files.
For Each varKey In dict.keys
Set wkbDestination = dict.Item(varKey)
'Remove empty sheet.
Set wks = Nothing
On Error Resume Next
Set wks = wkbDestination.Worksheets(TO_DELETE_SHEET_NAME)
On Error GoTo 0
If Not wks Is Nothing Then wks.Delete
Call wkbDestination.SaveAs(Filename:=destinationFolder & varKey & ".xlsx")
Next varKey
'Restore Excel.Application settings.
With Excel.Application
.DisplayAlerts = settingDisplayAlerts
.SheetsInNewWorkbook = settingSheetsNumber
End With
End Sub
However, this code opens all workbooks and at about 60-70 open excel files i receive an error: Run-time Error '1004' - Method 'Open' of object 'Workbooks' failed.
is there a way to make this code work?
Excel version is pro plus 2016.
Merge Workbooks
It will open the first of each files starting with the unique first four characters, and copy the first worksheet of each next opened file to the first opened file and finally save it as a new file.
There need not be only 2 files (starting with the same four characters) and there can only be one.
Adjust the values in the constants section.
Option Explicit
Sub mergeWorkbooks()
Const sPath As String = "F:\Test\2021\67077087\"
Const sPattern As String = "*.xlsx"
Const dPath As String = "F:\Test\2021\67077087\Destination\"
Const dName As String = "Master.xlsx"
Const KeyLen As Long = 4
Dim PatLen As Long: PatLen = Len(sPattern)
Dim fName As String: fName = Dir(sPath & sPattern)
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Do While Len(fName) > 0
dict(Left(fName, KeyLen)) = Empty
fName = Dir
Loop
Application.ScreenUpdating = False
On Error Resume Next
MkDir dPath
On Error GoTo 0
Dim wb As Workbook
Dim Key As Variant
Dim wsLen As Long
For Each Key In dict.Keys
Set wb = Nothing
fName = Dir(sPath & Key & sPattern)
Do While Len(fName) > 0
wsLen = Len(fName) - PatLen - KeyLen + 2
If wb Is Nothing Then
Set wb = Workbooks.Open(sPath & fName)
wb.Worksheets(1).Name = Mid(fName, KeyLen, wsLen)
'Debug.Print wb.Name
Else
With Workbooks.Open(sPath & fName)
'Debug.Print .Name
.Worksheets(1).Name = Mid(fName, KeyLen, wsLen)
.Worksheets(1).Copy After:=wb.Sheets(wb.Sheets.Count)
.Close False
End With
End If
fName = Dir
Loop
Application.DisplayAlerts = False
wb.SaveAs dPath & Key & dName ', xlOpenXMLWorkbook
Application.DisplayAlerts = True
wb.Close False
Next Key
Application.ScreenUpdating = True
End Sub
Test for Names
Use the following to print all names in the active workbook to the VBE Immediate window (CTRL+G).
Sub listNames()
Dim nm As Name
For Each nm In ActiveWorkbook.Names
Debug.Print nm.Name
Next nm
End Sub
First, check if the names (if any) are used in some formulas.
Use the following to delete all names in the active workbook.
Sub deleteNames()
Dim nm As Name
For Each nm In ActiveWorkbook.Names
nm.Delete
Next nm
End Sub
Untested but here's one approach where you don't have multiple files open at the same time:
Sub test(sourceFolder As String, destinationFolder As String)
Dim dict As Object, code As String
Dim colFiles As Collection, f, k, wbNew As Workbook, wb As Workbook
Set dict = VBA.CreateObject("Scripting.Dictionary")
'ensure trailing "\"
EnsureSlash sourceFolder
EnsureSlash destinationFolder
'get a collection of all xlsx files in the source folder
Set colFiles = allFiles(sourceFolder, "*.xlsx")
If colFiles.Count = 0 Then Exit Sub 'no files
'organize the files into groups according to first four characters of the filename
For Each f In colFiles
code = Left(f.Name, 4)
If Not dict.exists(code) Then Set dict(code) = New Collection 'need new group?
dict(code).Add f 'add the file to the collection for this code
Next f
'loop over the groups
For Each k In dict
Set colFiles = dict(k) 'the files for this code
Set wbNew = Workbooks.Add(Template:=xlWBATWorksheet) 'one sheet
For Each f In colFiles
With Workbooks.Open(f.Path)
.Worksheets(1).Copy after:=wbNew.Sheets(wbNew.Sheets.Count)
wbNew.Sheets(wbNew.Sheets.Count).Name = Replace(f.Name, ".xlsx", "")
.Close False
End With
Next f
Application.DisplayAlerts = False
wbNew.Sheets(1).Delete 'remove the empty sheet
Application.DisplayAlerts = True
wbNew.SaveAs destinationFolder & k & ".xlsx"
wbNew.Close
Next k
End Sub
'Return all files in `sourceFolder` which match `pattern`
' as a collection of file objects
Function allFiles(sourceFolder As String, pattern As String) As Collection
Dim col As New Collection, f
For Each f In CreateObject("scripting.filesystemobject").getfolder(sourceFolder).Files
If f.Name Like pattern Then col.Add f
Next f
Set allFiles = col
End Function
'Utility - check a path ends in a backslash
' use Application.PathSeparator if needs to be cross-platform
Sub EnsureSlash(ByRef f As String)
If Right(f, 1) <> "\" Then f = f & "\"
End Sub
Please consider code below. There open topic here -> "Open Sharepoint Excel Files With VBA". With some advises. My technical task is
1) Open file, 2) Update all links. 3) Save & Close file 4) Next file
Code renters error message enter image description here
Can some one correct me in my code, please?
Sub update_files()
Dim FolderPath As String
Dim wb_master As Workbook
Dim ws_master As Worksheet
Dim StrFile As String
Dim TempPath As String
Dim source_wb As Workbook
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.AskToUpdateLinks = False
If wb_master Is Nothing Then Set wb_master = ThisWorkbook
If ws_master Is Nothing Then Set ws_master = ThisWorkbook.Sheets("setup")
FolderPath = ws_master.Range("A5") 'cell "A5" = https://customer_name.sharepoint.com/teams/BG003C9/FY20/08/Actuals/01/Preliminary/Actuals/
'customer_name
TempPath = Replace(Replace(FolderPath, "https://customer_name.sharepoint.com", "\\customer_name.sharepoint.com#SSL\DavWWWRoot\"), "/", "\")
If ws_master.AutoFilterMode = True Then ws_master.AutoFilterMode = False
SourceRow = 5
Do While Cells(SourceRow, "B").Value <> "" 'cell B5 ... Bn+1 until cell = "", stored file name on customer SharePoint
StrFile = ws_master.Range("B" & SourceRow).Value
source_file = TempPath & StrFile & ".xlsx"
Set source_wb = Workbooks.Open(source_file)
source_wb.LockServerFile 'locke file on server for next changes
source_wb.Activate 'active opend file
source_wb.UpdateLink Name:=ActiveWorkbook.LinkSources 'update all links
source_wb.Save 'save workbook
source_wb.Close 'close workbook
SourceRow = SourceRow + 1 ' Move down 1 row for source sheet
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.AskToUpdateLinks = True
End Sub
I have about 100 Word documents and from each I want to copy data and paste it all in one Excel workbook.
I came up with this code which opens one Word document, copies data, pastes it to Excel and closes the Word document:
Sub WordDataToExcel()
Dim myObj
Dim myWB
Dim mySh
Dim txt As String, Lgth As Long, Strt As Long
Dim i As Long
Dim oRng As Range
Dim Tgt As String
Dim TgtFile As String
Dim arr()
Dim ArrSize As Long
Dim ArrIncrement As Long
ArrIncrement = 1000
ArrSize = ArrIncrement
ReDim arr(ArrSize)
Dim wrdDoc As Object
Documents.Open ("D:\ekr5_i.doc")
TgtFile = "result.xlsx"
Tgt = "D:\" & TgtFile
'finds the text string of Lgth lenght
txt = "thetext"
Lgth = 85
Strt = Len(txt)
'Return data to array
With Selection
.HomeKey unit:=wdStory
With .Find
.ClearFormatting
.Forward = True
.Text = txt
.Execute
While .Found
i = i + 1
Set oRng = ActiveDocument.Range _
(Start:=Selection.Range.Start + Strt, _
End:=Selection.Range.End + Lgth)
arr(i) = oRng.Text
oRng.Start = oRng.End
.Execute
If i = ArrSize - 20 Then
ArrSize = ArrSize + ArrIncrement
ReDim Preserve arr(ArrSize)
End If
Wend
End With
End With
ReDim Preserve arr(i)
'Set target and write data
Set myObj = CreateObject("Excel.Application")
Set myWB = myObj.Workbooks.Open(Tgt)
Set mySh = myWB.Sheets(1)
With mySh
.Range(.Cells(1, 1), .Cells(i, 1)) = myObj.Transpose(arr)
End With
'Tidy up
myWB.Close True
myObj.Quit
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Set mySh = Nothing
Set myWB = Nothing
Set myObj = Nothing
End Sub
I need to loop through all the documents in the folder.
I have implemented the same with Excel workbooks, but I don't know how for Word documents.
Here is the code for Excel workbooks:
Sub combine_into_one()
Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
Dim strPath$, Pivot$, sUserName$, sFolderName$, sSourceName$, x&
Dim oFldialog As FileDialog
Dim oFolder
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set oFldialog = Application.FileDialog(msoFileDialogFolderPicker)
With oFldialog
If .Show = -1 Then
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
sFolderName = .SelectedItems(1)
End If
End With
Set oFolder = FSO.GetFolder(sFolderName)
Workbooks.Add: Pivot = ActiveWorkbook.Name 'Destination workbook
For Each oFile In oFolder.Files
Workbooks(Pivot).Activate
x = Workbooks(Pivot).Sheets(1).Cells.SpecialCells(xlCellTypeLastCell).Row + 1
Workbooks.Open Filename:=oFile: sSourceName = ActiveWorkbook.Name
Workbooks(sSourceName).Activate
Workbooks(sSourceName).Sheets(1).[A80:Q94].copy
Workbooks(Pivot).Activate
Workbooks(Pivot).Sheets(1).Cells(x + 1, 1).PasteSpecial xlPasteAll
Workbooks(sSourceName).Close False
Next
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
There are so, so, so many things you can do between Excel & Word. I'm not sure I totally understand your question. The script below may help you; it has definitely served me well over time. If you need something different, please describe your issue more, to better clarify the issue you are facing.
Sub OpenAndReadWordDoc()
Rows("2:1000000").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A1").Select
' assumes that the previous procedure has been executed
Dim oWordApp As Word.Application
Dim oWordDoc As Word.Document
Dim blnStart As Boolean
Dim r As Long
Dim sFolder As String
Dim strFilePattern As String
Dim strFileName As String
Dim sFileName As String
Dim ws As Worksheet
Dim c As Long
Dim n As Long
'~~> Establish an Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")
If Err Then
Set oWordApp = CreateObject("Word.Application")
' We started Word for this macro
blnStart = True
End If
On Error GoTo ErrHandler
Set ws = ActiveSheet
r = 1 ' startrow for the copied text from the Word document
' Last column
n = ws.Range("A1").End(xlToRight).Column
sFolder = "C:\Users\Excel\Desktop\Coding\Microsoft Excel\PWC\Resumes\"
'~~> This is the extension you want to go in for
strFilePattern = "*.doc*"
'~~> Loop through the folder to get the word files
strFileName = Dir(sFolder & strFilePattern)
Do Until strFileName = ""
sFileName = sFolder & strFileName
'~~> Open the word doc
Set oWordDoc = oWordApp.Documents.Open(sFileName)
' Increase row number
r = r + 1
' Enter file name in column A
ws.Cells(r, 1).Value = sFileName
ActiveCell.Offset(1, 0).Select
ActiveSheet.Hyperlinks.Add Anchor:=Sheets("Sheet1").Range("A" & r), Address:=sFileName, _
SubAddress:="A" & r, TextToDisplay:=sFileName
' Loop through the columns
For c = 2 To n
If oWordDoc.Content.Find.Execute(FindText:=Trim(ws.Cells(1, c).Value), _
MatchWholeWord:=True, MatchCase:=False) Then
' If text found, enter Yes in column number c
ws.Cells(r, c).Value = "Yes"
End If
Next c
oWordDoc.Close SaveChanges:=False
'~~> Find next file
strFileName = Dir
Loop
ExitHandler:
On Error Resume Next
' close the Word application
Set oWordDoc = Nothing
If blnStart Then
' We started Word, so we close it
oWordApp.Quit
End If
Set oWordApp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
Function GetDirectory(path)
GetDirectory = Left(path, InStrRev(path, "\"))
End Function
In this scenario, whatever you put in the headers of B1:K1 (or more to the right) is searched for, each word document in a folder is opened, scanned, and if the string in B1:K1 is found, an 'x' is placed in the same x-y coordinate.
Again, if this doesn't help, please describe your issue better, and I'll post back with alternative solutions. Thanks!!