Inserting Text Box in Word Doc from excel

danpre

Board Regular
Joined
Aug 29, 2011
Messages
58
Dear Team,

Hope you are doing great.

I have 1 requirement where i need to prepare word doc letter and send to all finance manager through outlook as attachment.

I have written macro code for the same which is working fine but i have additional requirement in this code is where supplier Name and address should come in "Text Box" as shown below and not typed as normal which should come after To Finance Manager .

To Finance Manager,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t202 path="m,l,21600r21600,l21600,xe" o:spt="202" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:path o:connecttype="rect" gradientshapeok="t"></v:path></v:shapetype><v:shape style="Z-INDEX: 251659264; POSITION: absolute; MARGIN-TOP: 2.75pt; WIDTH: 256.5pt; HEIGHT: 116.25pt; VISIBILITY: visible; MARGIN-LEFT: -5.25pt; mso-wrap-style: square; mso-width-percent: 0; mso-height-percent: 0; mso-wrap-distance-left: 9pt; mso-wrap-distance-top: 0; mso-wrap-distance-right: 9pt; mso-wrap-distance-bottom: 0; mso-position-horizontal: absolute; mso-position-horizontal-relative: text; mso-position-vertical: absolute; mso-position-vertical-relative: text; mso-width-relative: margin; mso-height-relative: margin; v-text-anchor: top" id=Text_x0020_Box_x0020_2 stroked="f" o:gfxdata="UEsDBBQABgAIAAAAIQC75UiUBQEAAB4CAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbKSRvU7DMBSFdyTewfKKEqcMCKEmHfgZgaE8wMW+SSwc27JvS/v23KTJgkoXFsu+P+c7Ol5vDoMTe0zZBl/LVVlJgV4HY31Xy4/tS3EvRSbwBlzwWMsjZrlprq/W22PELHjb51r2RPFBqax7HCCXIaLnThvSAMTP1KkI+gs6VLdVdad08ISeCho1ZLN+whZ2jsTzgcsnJwldluLxNDiyagkxOquB2Knae/OLUsyEkjenmdzbmG/YhlRnCWPnb8C898bRJGtQvEOiVxjYhtLOxs8AySiT4JuDystlVV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K0wP/HBAAAAMgEAAAsAAAAAAAAAAAAAAAAANgEAAF9yZWxzLy5yZWxzUEsBAi0AFAAGAAgAAAAhAE33OYnvAgAAGwgAAB8AAAAAAAAAAAAAAAAAIAIAAGNsaXBib2FyZC9kcmF3aW5ncy9kcmF3aW5nMS54bWxQSwECLQAUAAYACAAAACEAnE5eIeIGAAA6HAAAGgAAAAAAAAAAAAAAAABMBQAAY2xpcGJvYXJkL3RoZW1lL3RoZW1lMS54bWxQSwECLQAUAAYACAAAACEAnGZGQbsAAAAkAQAAKgAAAAAAAAAAAAAAAABmDAAAY2xpcGJvYXJkL2RyYXdpbmdzL19yZWxzL2RyYXdpbmcxLnhtbC5yZWxzUEsFBgAAAAAFAAUAZwEAAGkNAAAAAA==" type="#_x0000_t202" o:spid="_x0000_s1026"></v:shape><TABLE cellSpacing=0 cellPadding=0 width="100%"><TBODY><TR><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: transparent; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0">«Supplier_Name»<o:p></o:p>
«Supplier_Address»

</TD></TR></TBODY></TABLE><o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>


<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
Dear Sir/Madam<o:p></o:p>
<o:p> </o:p>
We regret that we are unable to process the below referred invoice for the following reason:<o:p></o:p>
<o:p> </o:p>
«Rejection_Reason»<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Accenture Ref: «URN»<o:p></o:p>
<o:p> </o:p>
Invoice Number: «Invoice_Number»<o:p></o:p>
<o:p></o:p>
Invoice Date: «Invoice_Date»<o:p></o:p>
<o:p> </o:p>
Invoice Amount: «Invoice_Amount» «Currency_code»<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Correct Details: <o:p></o:p>
<o:p> </o:p>
«Last_Comment»<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p></o:p>
<o:p></o:p>
Request you to kindly amend your invoice accordingly and resend it to us at <o:p></o:p>
«DFM_Email_id» as soon as possible.<o:p></o:p>
<o:p> </o:p>
If you have any queries please do not hesitate to contact customer support at <o:p></o:p>
«Customer_care».<o:p></o:p>
<o:p> </o:p>
Yours sincerely<o:p></o:p>


Below is the VBA code i have written which is working fine except the Name and address not coming in TextBox.

Private Sub CommandButton1_Click()
Dim msg As String
Dim filename As String
Dim r As Integer
Dim lrow As Long
Dim wrdapp As Word.Application
Dim wrddoc As Word.Document
Dim strAddressData As String
Dim objShape As Shape

lrow = Worksheets("sheet1").Range("a" & Rows.Count).End(xlUp).Row
For r = 3 To lrow
Set wrdapp = CreateObject("word.application")
wrdapp.Visible = True
Set wrddoc = wrdapp.Documents.Open("C:\Rejection\document1.docx")

With wrddoc
msg = ""
msg = msg & "To Finance Manager" & "," & vbCrLf

'msg = msg & ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 2.5, 1.5, _
116, 145).TextFrame.TextRange.Text = "Daniel" 'Worksheets("sheet1").Range("W" & r) & vbCrLf & vbCrLf & vbCrLf

msg = msg & "Dear Sir/Madam" & "," & vbCrLf & vbCrLf
msg = msg & "We regret that we are unable to process the below referred invoice for the following reason:" & " " & Worksheets("sheet1").Range("P" & r) & vbCrLf & vbCrLf

msg = msg & "Accenture Ref - " & Worksheets("sheet1").Range("A" & r) & vbCrLf
msg = msg & "Invoice Number - " & Worksheets("sheet1").Range("H" & r) & vbCrLf
msg = msg & "Invoice Date - " & Worksheets("sheet1").Range("I" & r) & vbCrLf
msg = msg & "Invoice Amount - " & Worksheets("sheet1").Range("J" & r) & "" & Worksheets("sheet1").Range("K" & r) & vbCrLf & vbCrLf & vbCrLf

msg = msg & "Correct Details" & vbCrLf
msg = msg & Worksheets("sheet1").Range("N" & r) & vbCrLf & vbCrLf & vbCrLf

msg = msg & "Request you to kindly amend your invoice accordingly and resend it to us at" & " " & Worksheets("sheet1").Range("V" & r) & "as soon as possible" & vbCrLf & vbCrLf
msg = msg & "If you have any queries please do not hesitate to contact customer support at" & " " & Worksheets("sheet1").Range("U" & r) & vbCrLf & vbCrLf & vbCrLf
msg = msg & "Yours sincerely" & vbCrLf & "Accenture Business Services."

.Content.InsertAfter msg
.Content.InsertParagraphAfter

filename = Worksheets("sheet1").Range("AA" & r)
'If Dir("C:\Rejection\filename.doc") <> "" Then
' Kill "C:\Foldername\filename.doc"
' End If
.SaveAs ("c:\Rejection\" & filename)
.Close
End With
' Sending Mails from Outlook
Dim olf As Outlook.MAPIFolder, olmailitem As Outlook.MailItem
Dim tocontact As Outlook.Recipient
Set olf = GetObject("", _
"outlook.application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set olmailitem = olf.Items.Add
With olmailitem
.Subject = filename
Set tocontact = .Recipients.Add(Worksheets("sheet1").Range("X" & r))
Set tocontact = .Recipients.Add(Worksheets("sheet1").Range("Y" & r))
tocontact.Type = olCC
Set tocontact = .Recipients.Add(Worksheets("sheet1").Range("Z" & r))
Set tocontact = .Recipients.Add(Worksheets("sheet1").Range("Z" & r))
.body = "This is the Message text"
.Attachments.Add "c:\Rejection\" & filename & ".docx", olByValue, , _
Attachment

.OriginatorDeliveryReportRequested = True
.ReadReceiptRequested = True
.Save
.Send
End With

Next r

wrdapp.Quit
Set wrddoc = Nothing
Set wrdapp = Nothing

Set tocontact = Nothing
Set olmailitem = Nothing
Set olf = Nothing

End Sub


Please help me with VBA code i should put to have Name and address in Text Box in Word Document.

Thanks
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,224,550
Messages
6,179,459
Members
452,915
Latest member
hannnahheileen

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