Vba: How to align left a selection of cells sent in outlook as html?

Asked

Viewed 726 times

0

I will delete parts of the code to facilitate viewing the problem:

Dim objSelection As Excel.Range
Dim objTempWorkbook As Excel.Workbook
Dim objTempWorksheet As Excel.Worksheet
Dim strTempHTMLFile As String
Dim objTempHTMLFile As Object
Dim objFileSystem As Object
Dim objTextStream As Object
Dim objOutlookApp As Outlook.Application
Dim objNewEmail As Outlook.MailItem
Dim usuario As String
Dim linhaemail As Long

'Copia a seleção de uma sheet que será enviada no e-mail'

Sheets("Padrão E-mail").Activate
ActiveSheet.Range("B1:K29").Select
Set objSelection = Selection
Selection.Copy

'Cola a range em uma pasta de trabalho temporária'
Set objTempWorkbook = Excel.Application.Workbooks.Add(1)
Set objTempWorksheet = objTempWorkbook.Sheets(1)

'Mantem os valores e formatação durante a colagem'
 With objTempWorksheet.Cells(1)
    .PasteSpecial xlPasteValues
    .PasteSpecial xlPasteColumnWidths
    .PasteSpecial xlPasteFormats
 End With

'Salva a worksheet temporária como hmtl'
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
strTempHTMLFile = objFileSystem.GetSpecialFolder(2).Path & "\Temp for Excel" & 
Format(Now, "YYYY-MM-DD hh-mm-ss") & ".htm"
Set objTempHTMLFile = objTempWorkbook.PublishObjects.Add(xlSourceRange, 
strTempHTMLFile, objTempWorksheet.Name, objTempWorksheet.UsedRange.Address)
objTempHTMLFile.Publish (True)
Set objTextStream = objFileSystem.OpenTextFile(strTempHTMLFile)

'Cria um novo e-mail'
Set objOutlookApp = CreateObject("Outlook.Application")
Set objNewEmail = objOutlookApp.CreateItem(olMailItem)

'Le o arquivo HTML e insere no corpo do e-mail'
 objNewEmail.HTMLBody = objTextStream.ReadAll

When this selection of cells (saved in a temporary file like html) goes to outlook, it recognizes it as a table, and by default, these come aligned to the center, and I get this blank on the left. According to the image:

inserir a descrição da imagem aqui

How could I put the left alignment?

1 answer

1


Got it!

 objNewEmail.HTMLBody = "<table align=""left"">" & objTextStream.ReadAll & "</table>" & "<p align=""left"">" & objNewEmail.HTMLBody & "</p>"
  • Please mark later as answered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.