Ben_Kerry
10-21-2015, 08:17 AM
Hi all,
FIRST TIME POSTER - please bear with me!!!
I'm trying to combine two subs. The first sub is designed to open, save and close .docx files within a folder. The second sub, which I am calling from within the first sub, is designed to remove empty rows from tables within a document.
If I place the code for Sub2 into a document and run it individually, it works as expected. However, once I call it from within Sub1, it doesn't work. However, once I run the combined code, the folder's "Date Modified" in windows explorer does update. But none of the files within the folder are modified.
The source of the code had a caveat to say: "Note: Do not save the file containing this macro in a folder you want to process; otherwise, it will process itself! When it does that, it'll close its own document and stop processing."
However, out of frustration I tried this, by placing the code in the first document within the folder of documents that I want to modify. It worked for the first document, and then stopped as per the warning.
The first sub is from this thread:msofficeforums.com/word-vba/16209-run-macro-multiple-docx-files
The second sub is from this thread:extendoffice.com/documents/word/721-word-remove-empty-rows-columns-in-table
I have tried a few things, like instead of calling the sub...I inserted the code into the first sub - to no avail. Can anyone see where I am going wrong??? :banghead:
My combined code is:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
'Call your other macro or insert its code here
Call DeleteEmptyTablerowsandcolumns
.Close SaveChanges:=True
End With
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
Sub DeleteEmptyTablerowsandcolumns()
Application.ScreenUpdating = False
Dim Tbl As Table, cel As Cell, i As Long, n As Long, fEmpty As Boolean
With ActiveDocument
For Each Tbl In .Tables
n = Tbl.Columns.Count
For i = n To 1 Step -1
fEmpty = True
For Each cel In Tbl.Columns(i).Cells
If Len(cel.Range.Text) > 2 Then
fEmpty = False
Exit For
End If
Next cel
If fEmpty = True Then Tbl.Columns(i).Delete
Next i
Next Tbl
End With
With ActiveDocument
For Each Tbl In .Tables
n = Tbl.Rows.Count
For i = n To 1 Step -1
fEmpty = True
For Each cel In Tbl.Rows(i).Cells
If Len(cel.Range.Text) > 2 Then
fEmpty = False
Exit For
End If
Next cel
If fEmpty = True Then Tbl.Rows(i).Delete
Next i
Next Tbl
End With
Set cel = Nothing: Set Tbl = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
FIRST TIME POSTER - please bear with me!!!
I'm trying to combine two subs. The first sub is designed to open, save and close .docx files within a folder. The second sub, which I am calling from within the first sub, is designed to remove empty rows from tables within a document.
If I place the code for Sub2 into a document and run it individually, it works as expected. However, once I call it from within Sub1, it doesn't work. However, once I run the combined code, the folder's "Date Modified" in windows explorer does update. But none of the files within the folder are modified.
The source of the code had a caveat to say: "Note: Do not save the file containing this macro in a folder you want to process; otherwise, it will process itself! When it does that, it'll close its own document and stop processing."
However, out of frustration I tried this, by placing the code in the first document within the folder of documents that I want to modify. It worked for the first document, and then stopped as per the warning.
The first sub is from this thread:msofficeforums.com/word-vba/16209-run-macro-multiple-docx-files
The second sub is from this thread:extendoffice.com/documents/word/721-word-remove-empty-rows-columns-in-table
I have tried a few things, like instead of calling the sub...I inserted the code into the first sub - to no avail. Can anyone see where I am going wrong??? :banghead:
My combined code is:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
'Call your other macro or insert its code here
Call DeleteEmptyTablerowsandcolumns
.Close SaveChanges:=True
End With
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
Sub DeleteEmptyTablerowsandcolumns()
Application.ScreenUpdating = False
Dim Tbl As Table, cel As Cell, i As Long, n As Long, fEmpty As Boolean
With ActiveDocument
For Each Tbl In .Tables
n = Tbl.Columns.Count
For i = n To 1 Step -1
fEmpty = True
For Each cel In Tbl.Columns(i).Cells
If Len(cel.Range.Text) > 2 Then
fEmpty = False
Exit For
End If
Next cel
If fEmpty = True Then Tbl.Columns(i).Delete
Next i
Next Tbl
End With
With ActiveDocument
For Each Tbl In .Tables
n = Tbl.Rows.Count
For i = n To 1 Step -1
fEmpty = True
For Each cel In Tbl.Rows(i).Cells
If Len(cel.Range.Text) > 2 Then
fEmpty = False
Exit For
End If
Next cel
If fEmpty = True Then Tbl.Rows(i).Delete
Next i
Next Tbl
End With
Set cel = Nothing: Set Tbl = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function