VBA, send command from excel to find certain text in created word doc and send backspace

spriag

New Member
Joined
Jan 6, 2015
Messages
2
Hi,

I've got the code below, which creates a word document from a template and pastes certain cell values to text values in the template.

How would I go about the following - Sometimes the cell value for Telephone (in D2) would be "No telephone". How do I get the macro to instead of writing "No telephone" in the word document, to delete that line (i.e. send a 1 or 2 backspaces)?

Code:
[COLOR=#000000][FONT=Arial][SIZE=2]Sub SuitabilityLetter()[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]Worksheets("Fields").Activate[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]Dim wApp As Word.Application[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]Dim wDoc As Word.Document[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]Set wApp = CreateObject("Word.Application")[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]wApp.DisplayAlerts = False[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]wApp.Visible = True[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]Set wDoc = wApp.Documents.Open("M:\...\template.doc", ReadOnly:=True)[/SIZE][/FONT][/COLOR]

[COLOR=#000000][FONT=Arial][SIZE=2]With wDoc[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.Find.Text = "[[Telephone]]"[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.Find.Execute[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.Font.Size = 9[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection = Range("D2")[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.EndOf[/SIZE][/FONT][/COLOR]

[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.Find.Text = "[[Email]]"[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.Find.Execute[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.Font.Size = 9[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection = Range("E2")[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.EndOf[/SIZE][/FONT][/COLOR]

[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.Find.Text = "[[Date]]"[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.Find.Execute[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.Font.Size = 9[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection = Range("AD2")[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.EndOf[/SIZE][/FONT][/COLOR]

[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.Find.Text = "[[Salutation]]"[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.Find.Execute[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.Font.Size = 9[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection = Range("A2")[/SIZE][/FONT][/COLOR]
[COLOR=#000000][FONT=Arial][SIZE=2]    .Application.Selection.EndOf[/SIZE][/FONT][/COLOR]

[COLOR=#000000][FONT=Arial][SIZE=2]    End With
 [/SIZE][/FONT][/COLOR]

Thanks!
 
Last edited:

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,226,695
Messages
6,192,478
Members
453,727
Latest member
tuong_ng89

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