I'm trying to use For/If/Else if to test criteria and return various ranges depending on whether certain cell are empty or not, then past that range into an email.
When I run the code below it only returns range B15:G20
, no matter if the cell in question (b20) is empty or not. Ideally I'd like it to test up to 4 four cells to see if they're empty or not and return a range that only contains cells with data in them.
Sub EXPVendorCopyRangeToOutlook_single()
'Declare Outlook Vairables
Dim oLookApp As Outlook.Application
Dim oLookItm As Outlook.MailItem
Dim oLookIns As Outlook.Inspector
'Declare Word Vaiables
Dim oWrdDoc As Word.Document
Dim oWrdRng As Word.Range
'Declare Excel Variables
Dim ExcRng As Range
On Error Resume Next
'Get the Active instance of Outlook
Set oLookApp = GetObject(, "Outlook.Application")
'If error create a new instance of outlook
If Err.Number = 429 Then
'Clear Error
Err.Clear
'Create new instance of Outlook
Set oLookApp = New Outlook.Application
End If
'Create a new email 'Possible Problem here
Set oLookItm = oLookApp.CreateItem(olMailItem)
'Create a reference to the ex range you want to export
For Each Cell In Worksheets("sheet1")
If Not IsEmpty(b20.Value) Then
Set ExcRng = Sheet1.Range("b15:g20")
ElseIf Not IsEmpty(b19.Value) Then
Set ExcRng = Sheet1.Range("b15:g18")
End If
Next
With oLookItm
'Define basic info
.From = "ABC@XYZ.COM"
.To = "123@345.COM"
.CC = ""
.Subject = Range("m3")
.Body = "Please review the attached invoices and confirm that the goods or services have been received and payment should be made."
'Display email
.Display
'Get the active inspector
Set oLookIns = .GetInspector
'Get word editor
Set oWrdDoc = oLookIns.WordEditor
'Specify rang in document
Set oWrdRng = oWrdDoc.Application.ActiveDocument.Content
oWrdRng.Collapse Direction:=wdCollapseEnd
'Add new paragraphand then insert break
Set oWrdRng = oWdEditor.Paragraph.Add
oWrdRng.InsertBreak
'Copy the Range
ExcRng.Copy
'Paste it
oWrdRng.PasteSpecial DataType:=wdPasteMetafilePicture
End With
End Sub
This is the part of the code giving me problems.
For Each Cell In Worksheets("sheet1")
If Not IsEmpty(b20.Value) Then
Set ExcRng = Sheet1.Range("b15:g20")
ElseIf Not IsEmpty(b19.Value) Then
Set ExcRng = Sheet1.Range("b15:g18")
End If
Next
I feel like i'm overlooking something simple with syntax...idk TIA
Aucun commentaire:
Enregistrer un commentaire