Vba excel to word format issue

Largey980

New Member
Joined
Jun 28, 2016
Messages
17
Hi,

After some help with excel to word macro, trying to populate letter templates from raw date pasted from another system.

Word templates all

Title First_Name Surname
Address_1
Address_2
Address_3
Town
County
Postcode

<tbody>
</tbody>


----
Code im using


Sub FindReplaceInWord()


Dim Wbk As Workbook: Set Wbk = ThisWorkbook
Dim Wrd As New Word.Application
Dim Dict As Object
Dim RefList As Range, RefElem As Range


Wrd.Visible = True
Dim WDoc As Document
Set WDoc = Wrd.Documents.Open("file path to word doc")


Set Dict = CreateObject("Scripting.Dictionary")
Set RefList = Wbk.Sheets("Sheet1").Range("C2:C16")


With Dict
For Each RefElem In RefList
If Not .Exists(RefElem) And Not IsEmpty(RefElem) Then
.Add RefElem.Value, RefElem.Offset(0, 1).Value
End If
Next RefElem
End With


For Each Key In Dict
With WDoc.Content.Find
.Execute FindText:=Key, ReplaceWith:=Dict(Key)
End With
Next Key


End Sub
----
This works fine, until address 2/3 blank and leaves blank line or adds a 0 to word templates.


I tried using =CONCATENATE(Address_1, CHAR(10), Address_2,CHAR(10),Address_3), changed Town to ref address_2, postcode to address_3, left county blank.. which is ok as blank entries at bottom, when copies to word however it appears all on same line.

I need way to remove blank lines, or get the concatenate to copy to sep lines.

I considered using mail merge but I couldn't get it to work with raw data.

Any suggestions?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
What about this:

Code:
    If Dict(Key) = "" Then
        .Execute FindText:=Key & "^p", ReplaceWith:=Dict(Key)
    Else
        .Execute FindText:=Key, ReplaceWith:=Dict(Key)
    End If

WBD
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,944
Members
449,480
Latest member
yesitisasport

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