Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 02-12-2024, 11:42 AM
war4d war4d is offline Trim spaces from merge data fields Windows 11 Trim spaces from merge data fields Office 2021
Novice
Trim spaces from merge data fields
 
Join Date: Feb 2024
Posts: 2
war4d is on a distinguished road
Default

If anyone is curious. I ended up creating a new text file and connecting to that as the data source. The below code is what is working.

The TrimAll function was found here in Ovidiu Luca's answer.

Code:
' Go to Tools -> References... and check "Microsoft Scripting Runtime" 
' to be able to use the FileSystemObject

Sub CleanData()

Dim masterDoc As Document, field As String, line As String, lastRecordNum As Long, filePath As String, fso As FileSystemObject, fileStream As TextStream
Set masterDoc = ActiveDocument
filePath = "C:\temp\MyTestFile.txt"
If Dir("C:\temp\") = "" Then MkDir "C:\temp\"
Set fso = New FileSystemObject
Set fileStream = fso.CreateTextFile(filePath)
masterDoc.MailMerge.DataSource.ActiveRecord = wdLastRecord
lastRecordNum = masterDoc.MailMerge.DataSource.ActiveRecord
masterDoc.MailMerge.DataSource.ActiveRecord = wdFirstRecord
If masterDoc.MailMerge.DataSource.Type = _
    wdMergeInfoFromWord Then
    'Create FieldName Line
    With masterDoc.MailMerge
      For Each aname In .DataSource.DataFields
          field = LTrim(RTrim(aname.Name))
            If Len(line) < 1 Then
            line = line & field
            Else
            line = line & "|" & field
            End If
      Next aname
     End With
          fileStream.WriteLine line
' Loop Through Data and trim whitespace from both sides. Space and tab
    Do While lastRecordNum > 0
        With masterDoc.MailMerge
            line = """"
          For Each afield In .DataSource.DataFields
            field = TrimAll(afield.Value)
            If Len(line) < 2 Then
            line = line & field
            Else
            line = line & """|""" & field
            End If
          Next afield
          line = line & """"
         End With
          fileStream.WriteLine line
         
        If masterDoc.MailMerge.DataSource.ActiveRecord >= lastRecordNum Then
            lastRecordNum = 0
        Else
            masterDoc.MailMerge.DataSource.ActiveRecord = wdNextRecord
        End If
    Loop
    fileStream.Close
    'Connect to new file
ary = Split(filePath, "\")
With ActiveDocument.MailMerge
  .OpenDataSource Name:=filePath, ReadOnly:=True, AddToRecentFiles:=False, _
    LinkToSource:=False, Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;" & _
    "Data Source=StrMMSrc;Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
    SQLStatement:="SELECT * FROM " & ary(UBound(ary))
End With
End If

End Sub

'Function to Trim whitespace.
Private Function TrimAll(Text As String) As String

Const toRemove As String = " " & vbTab & vbCr & vbLf 'what to remove

Dim s As Long: s = 1
Dim e As Long: e = Len(Text)
Dim c As String

If e = 0 Then Exit Function 'zero len string

Do 'how many chars to skip on the left side
    c = Mid(Text, s, 1)
    If c = "" Or InStr(1, toRemove, c) = 0 Then Exit Do
    s = s + 1
Loop
Do 'how many chars to skip on the right side
    c = Mid(Text, e, 1)
    If e = 1 Or InStr(1, toRemove, c) = 0 Then Exit Do
    e = e - 1
Loop
TrimAll = Mid(Text, s, (e - s) + 1) 'return remaining text

End Function
Reply With Quote
 

Tags
editdatasource, mailmerge field, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trim spaces from merge data fields Trim spaces from table cells jeffreybrown Word VBA 2 09-04-2018 07:36 PM
Data Entry - Getting spreadsheets back with excess space but the TRIM function doesn't work jhato160 Excel 5 06-17-2016 04:06 AM
Merge fields in Word and source data from Excel SteveVai Mail Merge 3 03-13-2014 05:25 AM
Trim spaces from merge data fields Phantom spaces when updating linked fields freefalladdict Word 3 01-08-2012 02:23 PM
How do I set up the fields in mail merge word 07 from my data base mbcrabber Mail Merge 4 06-06-2010 01:25 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:49 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft