Copy table in email to Excel using VBA - vba

I am trying to create some code that will copy the body of an email into a new Excel spreadsheet. I have this code:
Public Sub ExportToExcel1()
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim myitem As Outlook.MailItem
Dim FileName As String
Dim i As Integer
Dim objSearchFolder As Outlook.MAPIFolder
Dim item As Object
Dim mai As MailItem
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox).Folders("Hold Info")
Set objSearchFolder = Inbox
i = 0
For Each item In Inbox.Items
item.Display
item.Body.Select
Selection.Copy
Dim xlApp As Object ' Excel.Application
Dim xlWkb As Object ' Excel.Workbook
Set xlApp = CreateObject("Excel.Application") ' New Excel.Application
Set xlWkb = xlApp.Workbooks.Add
xlApp.Visible = True
xlApp.Workbooks.Add
xlApp.Selection.Paste False, False, False
Next
End Sub
It keeps giving me an error at item.Body.Select and I have no clue why. It may have something to do with the fact that the email I am trying to copy is nothing but tables that were generated in Oracle, but I have no clue.

You could use the Clipboard directly rather than trying to select and copy. If you have a userform in your project, you already have this reference set. If not, set a reference to Microsoft Forms 2.0 Object Library. Then use a DataObject to put some text into the Clipboard.
Public Sub ExportToExcel1()
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim item As Object
Dim doClip As MSForms.DataObject
Dim xlApp As Object ' Excel.Application
Dim xlWkb As Object
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox).Folders("Hold Info")
Set doClip = New MSForms.DataObject
For Each item In Inbox.Items
If TypeName(item) = "MailItem" Then
doClip.SetText item.Body
doClip.PutInClipboard
Set xlApp = CreateObject("Excel.Application") ' New Excel.Application
xlApp.Visible = True
Set xlWkb = xlApp.Workbooks.Add
xlWkb.Sheets(1).Range("A1").PasteSpecial "Text"
End If
Next
End Sub
A couple of points to consider. When you use the Clipboard in this fashion, you're using the Windows Clipboard, not the Office Clipboard. The Windows Clipboard doesn't recognize Office specific Clipboard formats, so you lose a little in translation.
Pasting from the Clipboard has some advantages. But if you want complete control of how your data shows up in Excel, then read the Body into a string, parse the string for the data you want, and write the specific data to the cells you want.

Related

PowerPoint VBA to Update Embedded Spreadsheets

I have a PowerPoint with 21 embedded spreadsheets that are used to populate charts in the PowerPoint. Each week data from two spreadsheets is copied into the embedded spreadsheets to update the charts with new data. I've started working on a macro to automate this process. I have the macro to open the spreadsheets with the new data, copy the data, then open the first embedded file. I am getting a run-time error'438' Object doesn't support this property or method on the paste function.
Note: I know linked spreadsheets would be desirable, but my customer wants the embedded excel files.
Sub UpdatedEmbeddedSpreadsheets()
Dim oData As ChartData
Dim oCht As Chart
Dim xlApp As Object
Dim xlWorkBook As Object
'Open Weekly File
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("C:\abc.xlsx", True, False)
xlWorkBook.sheets("Weekly ABC Chart").Range("B2:B5").Copy
Set xlApp = Nothing
Set xlWorkBook = Nothing
'Open Embedded Spreadsheet
ActivePresentation.Slides(1).Shapes(1).Select
Set oCht = ActiveWindow.Selection.ShapeRange(1).Chart
oCht.ChartData.Activate
Set oData = oCht.ChartData
'The line below has the run-time error '438'
Debug.Print oData.Workbook.sheets("123").End(x1toright).Offset(0,-1).Paste
oData.Workbook.Close
Set oData = Nothing
Set oCht = Nothing
End Sub
Second Attempt:
Sub UpdatedEmbeddedSpreadsheets()
Dim xlApp As Object
Dim xlWorkBook As Object
'Open Weekly File
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("C:\abc.xlsx", True, False)
xlWorkBook.sheets("Weekly AVA Chart").Range("B2:B5").Copy
Set xlApp = Nothing
Set xlWorkBook = Nothing
'Open Embedded Spreadsheet
ActivePresentation.Slides(1).Shapes(1).Select
ActiveWindow.Selection.ShapeRange(1).Chart.ChartData.Activate
Dim ChartData As Object
Set ChartData = CreateObject("Excel.Application")
ChartData.Visible = True
ChartData.Workbook.sheets("123").End(xltoright).Offset(0, -1).Paste
ChartData.Workbook.Close
End Sub

Write in an already opened Excel workbook from Powerpoint Slide

I am trying to write in an already manually opened workbook using VBA from my PowerPoint presentation.
Using CreateObject and then Workbook.Open it opens a new instance of the file.
I'm trying to use GetObject as found in several examples over the web.
Here's my code :
Dim xlApp As Object
Dim xlWorkbook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = Trueme
Set xlWorkbook = xlApp.Workbooks.Open(ActivePresentation.Path & "\Suivi individuel.xlsx", True, False)
xlWorkbook.Sheets(1).Range("C14").Value = "Hello"
SlideShowWindows(1).View.GotoSlide nextSlide
I've tried:
Dim xlApp As Object
Dim xlWorkbook As Object
Set xlWorkbook = GetObject(ActivePresentation.Path & "\Suivi individuel.xlsx", "Excel.Application")
xlWorkbook.Sheets(1).Range("C14").Value = "Hello"
SlideShowWindows(1).View.GotoSlide nextSlide
It says runtime error 432: File Name or Class Name not found during automation operation.
Try:
Dim xlApp As Object
Dim xlWorkbook As Object
Set xlWorkbook = GetObject(ActivePresentation.Path & "\Suivi individuel.xlsx")
If you just want to get an open instance of Excel then
Set xlApp = GetObject(,"Excel.Application")
Set xlWorkbook = xlApp.ActiveWorkbook
Wrap that in an error handler if there's a chance Excel might not be open
See: https://support.microsoft.com/en-us/kb/288902

Search for a given name in a range in excel before sending an email

I am creating a macro in outlook to send an eamil with some specific information in it. But only some people from the list in an excel sheet can send that email out. When they hit "SEND" on that macro, it needs to open the excel sheet and varify if that person is listed on the list. If he isn't it should just give him an error " You are not eligible to send this message" .
I am able to open the excel file using the code below. But I am not sure how to do the checking (names are listed on Sheet1 from C1: C100) to see that sending person is listed in there.
Below is my code:
[Dim strFldr As String
Dim OutMail As Object
Dim xlApp As Object
strFldr = "C:\\users-d\gxg063\Gift\test\"
Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = True
xlApp.Workbooks.Open strFldr & "\RegionalAuthority.xlsx"]
Let me know how this works out - you'll need a reference to Excel in your Outloook VBE
Sub TestSub()
Dim strFldr As String
Dim OutMail As Object
Dim xlApp As Excel.Application
Dim xlWb As Workbook
Dim xlWs As Worksheet
Dim r As Range
Dim User As String
Dim c As Range
strFldr = "C:\\users-d\gxg063\Gift\test\"
Set xlApp = New Excel.Application
Set xlWb = xlApp.Workbooks.Open(strFldr & "\RegionalAuthority.xlsx")
Set xlWs = xlWb.Worksheets("Sheet1")
Set r = xlWs.Range("C1:C100")
User = (Environ$("Username"))
For Each c In r
If c = User Then
'Call your Send Macro here
Exit For
End If
Next c
xlApp.Visible = True
Set xlApp = Nothing
Set xlWb = Nothing
Set xlWs = Nothing
End Sub

VBA Type mismatch error when setting Excel Range in Word

I have the following code as part of my sub trying to assign a range:
'Set xlApp = CreateObject("Excel.Application")
Dim xlApp As Object
Set xlApp = GetObject(, "Excel.Application")
xlApp.Visible = False
xlApp.ScreenUpdating = False
Dim CRsFile As String
Dim CRsMaxRow As Integer
' get the CR list
CRsFile = "CRs.xls"
Set CRsWB = xlApp.Workbooks.Open("C:\Docs\" + CRsFile)
With CRsWB.Worksheets("Sheet1")
.Activate
CRsMaxRow = .Range("A1").CurrentRegion.Rows.Count
Set CRs = .Range("A2:M" & CRsMaxRow)
End With
Dim interestingFiles As Range
' get the files names that we consider interesting to track
Set FilesWB = xlApp.Workbooks.Open("files.xlsx")
With FilesWB.Worksheets("files")
.Activate
Set interestingFiles = .Range("A2:E5")
End With
Do you have any idea why am I getting a run time type mismatch error?
If you run the code from Word then the problem is in the declaration of 'interestingFiles' variable. Range exist in Word as well so use either Variant or add reference to Excel and then use Excel.Range.
Without Excel reference:
Dim interestingFiles As Variant
And with Excel reference:
Dim interestingFiles As Excel.Range
Kindly set xlApp object as in below code.
Also you provide complete path for your workbook when opening it.
Sub test()
Dim interestingFiles As Range
Dim xlApp As Object
Set xlApp = GetObject(, "Excel.Application")
' get the files names
Dim path As String
path = "C:\Users\Santosh\Desktop\file1.xlsx"
Set FilesWB = xlApp.Workbooks.Open(path)
With FilesWB.Worksheets(1)
.Activate
Set interestingFiles = .Range("A2:E5")
End With
End Sub

Loop Through PSTs in Outlook 2003 with VBA

In Outlook 2007, I am able to loop through mails stores, including PSTs, using code like this:
Dim stores As Outlook.stores
Set stores = objNamespace.stores
Dim store As Outlook.store
For Each store In stores
MsgBox store.FilePath
Next
However, in Outlook 2003, the Outlook.store and Outlook.stores objects do not exist.
Are there equivalent objects in Outlook 2003?
What other method might I use to loop through mail stores?
Thank you.
This sample code for Outlook 2003 will loop through the high level mailboxes and print certain properties to the Immediate Window. I chose the properties that looked most useful based on your request.
Sub LoopThruMailboxes()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim mailboxCount As Long
Dim i As Long
Dim folder As Outlook.MAPIFolder
' get local namespace
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
mailboxCount = olNS.Folders.count
For i = 1 To mailboxCount
Set folder = olNS.Folders(i)
Debug.Print folder.EntryID
Debug.Print folder.StoreID
Debug.Print folder.Name
Debug.Print folder.FolderPath
Next i
End Sub
folder.Name is the name of the mailbox, folder.StoreID is the store ID (I'm not sure what you meant by "store file path", I didn't see anything that looked relevant anyway).
Here's a functionized version that returns folder name and store ID as an array, which you could assign directly to a listbox:
Function GetMailBoxInfo() As String()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim mailboxCount As Long
Dim i As Long
Dim folder As Outlook.MAPIFolder
Dim tempString() As String
' get local namespace
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
mailboxCount = olNS.Folders.count
' size array accordingly
ReDim tempString(1 To mailboxCount, 1 To 2)
For i = 1 To mailboxCount
Set folder = olNS.Folders(i)
tempString(i, 1) = folder.Name
tempString(i, 2) = folder.StoreID
Next i
GetMailBoxInfo = tempString
End Function
ex:
ListBox1.List = GetMailBoxInfo