BnashGran
06-16-2014, 09:08 AM
Hello,
I am running Windows 8.1 and Office 2013. I am using mail merge to create product spec sheets which I will convert to PDF. Each sheet contains main fields in tables and in text. Some of the spec sheets will have blank fields due to the nature of the products. Thus I am writing a macro to delete empty rows and tables in the spec sheets. This portion of the macro works perfectly. However, I would like to set the macro to prompt the user to run whenever a mail merge is complete. This way, users who are making these spec sheets in the future don't forget to run the macro. I have created a new word template to house the macros and each spec sheet mail merge form references this template. I have read online that I need to create an application variable and then link that to a sub using the Application_Mailmergeafterrecordmerge Event. However. I do not know how to write the application. Most of my knowledge on this comes from http :// msdn.microsoft. com/ en-us /library /ff198157 (v=office.15). aspx. If someone could provide help declaring an application variable, I would be very appreciative. I have attached the code that I have thus far for my macro. Lastly, I apologize if I have broken any of the forum rules.
Public Sub Application_MailMergeAfterRecordMerge(Document)
If MsgBox("Do you want to clean this specsheet?", vbYesNo, "Clean Document") = vbYes Then
Call DeleteEmptyRows
Else
Exit Sub
End If
End Sub
Public Sub DeleteEmptyRows()
Dim oTable As Table, oRow As Range, oCell As Cell, Counter As Long, _
NumRows As Long, TextInRow As Boolean
For Each oTable In ActiveDocument.Tables
Set oRow = oTable.Rows(1).Range
NumRows = oTable.Rows.Count
Application.ScreenUpdating = False
For Counter = 1 To NumRows
StatusBar = "Row " & Counter
TextInRow = False
For Each oCell In oRow.Rows(1).Cells
If Len(oCell.Range.Text) > 2 Then
TextInRow = True
Exit For
End If
Next oCell
If TextInRow Then
Set oRow = oRow.Next(wdRow)
Else
oRow.Rows(1).Delete
End If
Next Counter
Next oTable
Application.ScreenUpdating = True
Call DeleteTable
End Sub
Public Sub DeleteTable()
Dim oTable As Table
For Each oTable In ActiveDocument.Tables
If oTable.Rows.Count <= 2 Then
oTable.Delete
End If
Next oTable
End Sub
I am running Windows 8.1 and Office 2013. I am using mail merge to create product spec sheets which I will convert to PDF. Each sheet contains main fields in tables and in text. Some of the spec sheets will have blank fields due to the nature of the products. Thus I am writing a macro to delete empty rows and tables in the spec sheets. This portion of the macro works perfectly. However, I would like to set the macro to prompt the user to run whenever a mail merge is complete. This way, users who are making these spec sheets in the future don't forget to run the macro. I have created a new word template to house the macros and each spec sheet mail merge form references this template. I have read online that I need to create an application variable and then link that to a sub using the Application_Mailmergeafterrecordmerge Event. However. I do not know how to write the application. Most of my knowledge on this comes from http :// msdn.microsoft. com/ en-us /library /ff198157 (v=office.15). aspx. If someone could provide help declaring an application variable, I would be very appreciative. I have attached the code that I have thus far for my macro. Lastly, I apologize if I have broken any of the forum rules.
Public Sub Application_MailMergeAfterRecordMerge(Document)
If MsgBox("Do you want to clean this specsheet?", vbYesNo, "Clean Document") = vbYes Then
Call DeleteEmptyRows
Else
Exit Sub
End If
End Sub
Public Sub DeleteEmptyRows()
Dim oTable As Table, oRow As Range, oCell As Cell, Counter As Long, _
NumRows As Long, TextInRow As Boolean
For Each oTable In ActiveDocument.Tables
Set oRow = oTable.Rows(1).Range
NumRows = oTable.Rows.Count
Application.ScreenUpdating = False
For Counter = 1 To NumRows
StatusBar = "Row " & Counter
TextInRow = False
For Each oCell In oRow.Rows(1).Cells
If Len(oCell.Range.Text) > 2 Then
TextInRow = True
Exit For
End If
Next oCell
If TextInRow Then
Set oRow = oRow.Next(wdRow)
Else
oRow.Rows(1).Delete
End If
Next Counter
Next oTable
Application.ScreenUpdating = True
Call DeleteTable
End Sub
Public Sub DeleteTable()
Dim oTable As Table
For Each oTable In ActiveDocument.Tables
If oTable.Rows.Count <= 2 Then
oTable.Delete
End If
Next oTable
End Sub