mbuk320000
New Member
- Joined
- Jan 12, 2021
- Messages
- 4
- Office Version
- 365
- 2019
- 2013
- 2011
- 2010
- 2007
- Platform
- Windows
- MacOS
i have a VBA code set up to transfer cell values to a word document but it always miss any 4 ie in excel one cell i have "Royal Mail 48 Small Parcel" but when it transfer to the word document its "Royal Mail 8 Small Parcel"
VBA Code:
vba Code
Option Explicit
Sub shipping()
Dim CustRow, CustCol, LastRow, TemplRow, DaysSince, FrDays, ToDays As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName As String
Dim CurDt, LastAppDt As Date
Dim WordDoc, WordApp, OutApp, OutMail As Object
Dim wordcontent As Word.Range
With Sheet28
If .Range("M1").Value = Empty Then
MsgBox "Please select a correct template from the drop down list"
.Range("M1").Select
Exit Sub
End If
TemplRow = .Range("M2").Value 'Set Template Row
TemplName = .Range("M1").Value 'Set Template Name
DaysSince = .Range("U1").Value
DocLoc = Sheet29.Range("C4").Value
'Open Word Template
On Error Resume Next 'If Word is already running
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
'Launch a new instance of Word
Err.Clear
'On Error GoTo Error_Handler
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True 'Make the application visible to the user
End If
LastRow = .Range("A9999").End(xlUp).Row 'Determine Last Row in Table
For CustRow = 3 To LastRow
If DaysSince = .Range("j" & CustRow).Value Then
Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False) 'Open Template
For CustCol = 1 To 14 'Move Through 10 Columns
TagName = .Cells(2, CustCol).Value 'Tag Name
TagValue = .Cells(CustRow, CustCol).Value 'Tag Value
With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = TagValue
.Wrap = wdfindcontinue
.Execute Replace:=wdReplaceAll 'Find & Replace all instances
End With
.Range("j" & CustRow).Value = Now
.Range("k" & CustRow).Value = TemplName 'Template Name
Next CustCol
End If
If .Range("N1").Value = "PDF" Then
FileName = "\\drobo\documents\P0188 - The Body Shop At Home\14 - Shiping to Clients\02 - Tracking Letters\" & .Range("D" & CustRow).Value & "_" & "Tracking" & "_" & .Range("b" & CustRow).Value & ".pdf" 'Create full filename & Path with current workbook location, Last Name & First Name
WordDoc.ExportAsFixedFormat OutputFileName:=FileName, ExportFormat:=wdExportFormatPDF
WordDoc.Close False
Else: 'If Word
FileName = "\\drobo\documents\P0188 - The Body Shop At Home\14 - Shiping to Clients\02 - Tracking Letters\" & .Range("D" & CustRow).Value & "_" & "Tracking" & "_" & .Range("b" & CustRow).Value & ".docx"
WordDoc.SaveAs FileName
End If
If .Range("P1").Value = "Email" Then
Set OutApp = CreateObject("Outlook.Application") 'Create Outlook Application
Set OutMail = OutApp.CreateItem(0) 'Create Email
With OutMail
.To = Sheet28.Range("f" & CustRow).Value
.Subject = "Hi, " & Sheet28.Range("d" & CustRow).Value & " your Tracking Details"
.Body = "Hello, " & Sheet28.Range("d" & CustRow).Value & " Thank You for your orded . Please see the attached file"
.Attachments.Add FileName
.Display 'To send without Displaying change .Display to .Send
End With
End If
also have an issue with the email option as well
WordDoc.Close
Next CustRow
WordApp.Quit
End With
End Sub
Last edited by a moderator: