Email docs from word doc by individual pages

Holley

Board Regular
Joined
Dec 11, 2019
Messages
155
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello all! I have a manual task that I am sure could be automated - I'm just at a loss of how to do it. I have a text file that I open in a word template with a company logo. There can be up to 70 pages. Each page break will need to be a different email. The email address is in the same location on each page as is the customer #. Would it be possible to run a macro that will copy the text (even if a page at a time), and paste it into an Outlook message? To make it even better is if it could copy the email address and customer # and past in the appropriate fields? Attached is an example of one page. The highlighted fields are static.
1699644463520.png

Thanks as always!
 
🥰 and I am so grateful too!! I will not be able to do that until later tonight. It is blocked here at work and I can do it when I get home from my PC.
We are 20:28 here in the UK so I'll log in tomorrow.

It's worth investigating better ways.
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hopefully, this will work... thank you again for your assistance! Row 34 starts the next document.
TESTSTMT.csv
ABC
1123 Any Street , Any town, ST 1234 123/123-4567
2 Fax 800/12-1234
3
4
5 11/10/23 ABCD0123
6
7COMPANY NAME CUST NBR - 1234567
8123 ANY STREET
9ANY CITY, ST 1234
10
11
12
13DEAR CUSTOMER
14
15ATTENTION A/P: HERE IS A COPY OF THE STATEMENT YOU HAVE REQUESTED
16PLEASE REACH OUT TO US WITH UPDATES
17YOUREMAIL@ISP.COM
18
19SINCERELY
20
21
22AGENT NAME
23
24VENDOR NAME INV NBR INV DATE AMOUNT DUE DATE
251234 VENDOR NAME 1234 01/01/23 123.23 02/01/23
261234 VENDOR NAME 1234 01/01/23 123.23 02/01/23
271234 VENDOR NAME 1234 01/01/23 123.23 02/01/23
28 --------------------
29369.69
30
31
32
33
34123 Any Street , Any town, ST 1234 123/123-4567
35 Fax 800/12-1234
36
37
38 11/10/23 ABCD0123
39
40STORE NAME CUST NBR - 9876543
41123 ANY STREET
42ANY CITY, ST 1234
43
44
45
46DEAR CUSTOMER
47
48ATTENTION A/P: HERE IS A COPY OF THE STATEMENT YOU HAVE REQUESTED
49PLEASE REACH OUT TO US WITH UPDATES
50YOUREMAIL@YOURDOMAIN.COM
51
52SINCERELY
53
54
55AGENT NAME
56
57VENDOR NAME INV NBR INV DATE AMOUNT DUE DATE
581234 VENDOR NAME 4566 01/01/23 123.23 02/01/23
59 --------------------
60123.23
61
62
63
64
TESTSTMT
 
Upvote 0
Hopefully, this will work... thank you again for your assistance! Row 34 starts the next document.
TESTSTMT.csv
ABC
1123 Any Street , Any town, ST 1234 123/123-4567
2 Fax 800/12-1234
3
4
5 11/10/23 ABCD0123
6
7COMPANY NAME CUST NBR - 1234567
8123 ANY STREET
9ANY CITY, ST 1234
10
11
12
13DEAR CUSTOMER
14
15ATTENTION A/P: HERE IS A COPY OF THE STATEMENT YOU HAVE REQUESTED
16PLEASE REACH OUT TO US WITH UPDATES
17YOUREMAIL@ISP.COM
18
19SINCERELY
20
21
22AGENT NAME
23
24VENDOR NAME INV NBR INV DATE AMOUNT DUE DATE
251234 VENDOR NAME 1234 01/01/23 123.23 02/01/23
261234 VENDOR NAME 1234 01/01/23 123.23 02/01/23
271234 VENDOR NAME 1234 01/01/23 123.23 02/01/23
28 --------------------
29369.69
30
31
32
33
34123 Any Street , Any town, ST 1234 123/123-4567
35 Fax 800/12-1234
36
37
38 11/10/23 ABCD0123
39
40STORE NAME CUST NBR - 9876543
41123 ANY STREET
42ANY CITY, ST 1234
43
44
45
46DEAR CUSTOMER
47
48ATTENTION A/P: HERE IS A COPY OF THE STATEMENT YOU HAVE REQUESTED
49PLEASE REACH OUT TO US WITH UPDATES
50YOUREMAIL@YOURDOMAIN.COM
51
52SINCERELY
53
54
55AGENT NAME
56
57VENDOR NAME INV NBR INV DATE AMOUNT DUE DATE
581234 VENDOR NAME 4566 01/01/23 123.23 02/01/23
59 --------------------
60123.23
61
62
63
64
TESTSTMT
Give this a go for starters.

This code will extract the information needed for the statement and split it between two worksheets, 'Customers' and 'StatementLines' so
create worksheets by these names. I can add in checks later.

Save the csv file as an .xlsx workbook.

Rename the worksheet 'Import'.

Place this code in a standard code module and run the subProcessImport procedure.

Once ths data is in a structured format we can go onto the next stage.

VBA Code:
Public Sub subProcessImport()
Dim strCompany As String
Dim i As Long
Dim ii As Long
Dim arrStatement() As Variant
Dim rng As Range
Dim s As String
Dim intRow As Integer
Dim arrString() As String
Dim arrVariant() As Variant
Dim arrLines() As String
Dim WsCustomers As Worksheet
Dim WsStatement As Worksheet
Dim strAccount As String
Dim strVendor As String
Dim lngStatementRow As Long

  ActiveWorkbook.Save

  Set WsCustomers = Worksheets("Customers")
  
  With WsCustomers
    With .Cells
      .Clear
    End With
    .Range("A1:F1").Value = Array("Customer", "Address 1", "Address 2", "Address 3", "Address 4", "Account")
  End With

  Set WsStatement = Worksheets("StatementLines")

  With WsStatement
    With .Cells
      .Clear
    End With
    .Range("A1:E1").Value = Array("Account", "INV NBR", "INV DATE", "AMOUNT", "DUE DATE")
  End With
  
  strCompany = Range("A1").Value
  
  intRow = 2
  lngStatementRow = 2
  
  For Each rng In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    
    ' First line of individual statement.
    If Trim(rng.Value) = strCompany Then
      s = s & vbCrLf & rng.Value
      i = i + 1
    End If
    
    ' Customer number.
    If InStr(1, Trim(rng.Value), "CUST NBR - ", vbTextCompare) > 0 Then
    
      arrString = Split(rng.Value, " ")
     
      With WsCustomers
        .Cells(intRow, 1).Value = Trim(Replace(rng.Value, "CUST NBR - " & arrString(UBound(arrString)), "", 1))
        strAccount = arrString(UBound(arrString))
        .Cells(intRow, 6).Value = arrString(UBound(arrString))
      End With
      
      arrVariant = rng.Offset(1, 0).Resize(6)
      For i = LBound(arrVariant) To UBound(arrVariant)
        If Trim(arrVariant(i, 1)) <> "" Then
          WsCustomers.Cells(intRow, i + 1).Value = arrVariant(i, 1)
        Else
          Exit For
        End If
      Next i
      intRow = intRow + 1
    
    End If
      
    If Left(rng.Value, 11) = "VENDOR NAME" Then
      
      arrVariant = rng.Offset(1, 0).Resize(30)
      
      For i = LBound(arrVariant) To UBound(arrVariant)
        
        If Trim(arrVariant(i, 1)) = "--------------------" Then
          Exit For
        End If
        
        Do While InStr(1, arrVariant(i, 1), "  ", vbTextCompare) > 0
          arrVariant(i, 1) = Replace(arrVariant(i, 1), "  ", " ", 1)
        Loop
        
        arrLines = Split(arrVariant(i, 1), " ")
        
        strVendor = ""
        
        For ii = LBound(arrLines) To UBound(arrLines) - 4
          strVendor = strVendor & " " & arrLines(ii)
        Next ii
     
        s = ""
        
        WsStatement.Cells(lngStatementRow, 1).Value = strAccount
        
        For ii = UBound(arrLines) - 3 To UBound(arrLines)
          s = s & "," & arrLines(ii)
        Next ii
                
        WsStatement.Range("A" & lngStatementRow & ":E" & lngStatementRow).Value = Split(strAccount & s, ",")
        
        lngStatementRow = lngStatementRow + 1
      
      Next i
    
    End If
        
  Next rng
      
  Call subFormatSheet(WsCustomers)
  
  Call subFormatSheet(WsStatement)
  
  MsgBox "Statement trext file has been processed.", vbOKOnly, "Confirmation"
  
End Sub

Public Sub subFormatSheet(Ws As Worksheet)

  With Ws.Range("A1").CurrentRegion
  
    .Font.Size = 14
    .Font.Name = "Arial"
    With .Rows(1)
      .Font.Bold = True
      .Interior.Color = RGB(217, 217, 217)
    End With
    .VerticalAlignment = xlCenter
    .EntireColumn.AutoFit
    With .Borders
      .LineStyle = xlContinuous
      .Weight = xlThin
      .ColorIndex = vbBlack
    End With
 
 End With
  
End Sub
 
Upvote 0
Give this a go for starters.

This code will extract the information needed for the statement and split it between two worksheets, 'Customers' and 'StatementLines' so
create worksheets by these names. I can add in checks later.

Save the csv file as an .xlsx workbook.

Rename the worksheet 'Import'.

Place this code in a standard code module and run the subProcessImport procedure.

Once ths data is in a structured format we can go onto the next stage.

VBA Code:
Public Sub subProcessImport()
Dim strCompany As String
Dim i As Long
Dim ii As Long
Dim arrStatement() As Variant
Dim rng As Range
Dim s As String
Dim intRow As Integer
Dim arrString() As String
Dim arrVariant() As Variant
Dim arrLines() As String
Dim WsCustomers As Worksheet
Dim WsStatement As Worksheet
Dim strAccount As String
Dim strVendor As String
Dim lngStatementRow As Long

  ActiveWorkbook.Save

  Set WsCustomers = Worksheets("Customers")
 
  With WsCustomers
    With .Cells
      .Clear
    End With
    .Range("A1:F1").Value = Array("Customer", "Address 1", "Address 2", "Address 3", "Address 4", "Account")
  End With

  Set WsStatement = Worksheets("StatementLines")

  With WsStatement
    With .Cells
      .Clear
    End With
    .Range("A1:E1").Value = Array("Account", "INV NBR", "INV DATE", "AMOUNT", "DUE DATE")
  End With
 
  strCompany = Range("A1").Value
 
  intRow = 2
  lngStatementRow = 2
 
  For Each rng In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
   
    ' First line of individual statement.
    If Trim(rng.Value) = strCompany Then
      s = s & vbCrLf & rng.Value
      i = i + 1
    End If
   
    ' Customer number.
    If InStr(1, Trim(rng.Value), "CUST NBR - ", vbTextCompare) > 0 Then
   
      arrString = Split(rng.Value, " ")
    
      With WsCustomers
        .Cells(intRow, 1).Value = Trim(Replace(rng.Value, "CUST NBR - " & arrString(UBound(arrString)), "", 1))
        strAccount = arrString(UBound(arrString))
        .Cells(intRow, 6).Value = arrString(UBound(arrString))
      End With
     
      arrVariant = rng.Offset(1, 0).Resize(6)
      For i = LBound(arrVariant) To UBound(arrVariant)
        If Trim(arrVariant(i, 1)) <> "" Then
          WsCustomers.Cells(intRow, i + 1).Value = arrVariant(i, 1)
        Else
          Exit For
        End If
      Next i
      intRow = intRow + 1
   
    End If
     
    If Left(rng.Value, 11) = "VENDOR NAME" Then
     
      arrVariant = rng.Offset(1, 0).Resize(30)
     
      For i = LBound(arrVariant) To UBound(arrVariant)
       
        If Trim(arrVariant(i, 1)) = "--------------------" Then
          Exit For
        End If
       
        Do While InStr(1, arrVariant(i, 1), "  ", vbTextCompare) > 0
          arrVariant(i, 1) = Replace(arrVariant(i, 1), "  ", " ", 1)
        Loop
       
        arrLines = Split(arrVariant(i, 1), " ")
       
        strVendor = ""
       
        For ii = LBound(arrLines) To UBound(arrLines) - 4
          strVendor = strVendor & " " & arrLines(ii)
        Next ii
    
        s = ""
       
        WsStatement.Cells(lngStatementRow, 1).Value = strAccount
       
        For ii = UBound(arrLines) - 3 To UBound(arrLines)
          s = s & "," & arrLines(ii)
        Next ii
               
        WsStatement.Range("A" & lngStatementRow & ":E" & lngStatementRow).Value = Split(strAccount & s, ",")
       
        lngStatementRow = lngStatementRow + 1
     
      Next i
   
    End If
       
  Next rng
     
  Call subFormatSheet(WsCustomers)
 
  Call subFormatSheet(WsStatement)
 
  MsgBox "Statement trext file has been processed.", vbOKOnly, "Confirmation"
 
End Sub

Public Sub subFormatSheet(Ws As Worksheet)

  With Ws.Range("A1").CurrentRegion
 
    .Font.Size = 14
    .Font.Name = "Arial"
    With .Rows(1)
      .Font.Bold = True
      .Interior.Color = RGB(217, 217, 217)
    End With
    .VerticalAlignment = xlCenter
    .EntireColumn.AutoFit
    With .Borders
      .LineStyle = xlContinuous
      .Weight = xlThin
      .ColorIndex = vbBlack
    End With
 
 End With
 
End Sub
So far so good!!!
 
Upvote 0
Have you tried it on a big text file of statements?
Ran this on real file and everything looks good on the Customers tab except the customer #s are off and not showing on the tab StatementLines

1699886447004.png


Nothing carried over to the StatementLines tab.

1699886506609.png
 
Upvote 0
Ran this on real file and everything looks good on the Customers tab except the customer #s are off and not showing on the tab StatementLines

View attachment 101843

Nothing carried over to the StatementLines tab.

View attachment 101844
Is the customer number like this in the text file?

CUST NBR - 1234567

Does it work for some of them and if so what is the difference between those lines?
 
Upvote 0
Is the customer number like this in the text file?

CUST NBR - 1234567

Does it work for some of them and if so what is the difference between those lines?
I had 44 customers and it pulled 17. Each show the customer # at the end of the name. The customer number does show as you have in your example.
1699887678617.png
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top