Outlook Email Body to Excel - excel

I am trying to have the body of all emails in a folder output to an excel file. The below code is what I am using:
Dim appExcel As Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Dim rng As Excel.Range
Dim strSheet As String
Dim strPath As String
Dim intRowCounter As Integer
Dim intColumnCounter As Integer
Dim msg As Outlook.MailItem
Dim nms As Outlook.NameSpace
Dim fld As Outlook.MAPIFolder
Dim itm As Object
strSheet = "Test.xlsm"
strPath = "C:user\Documents\Action Items\"
strSheet = strPath & strSheet
Debug.Print strSheet
'Select export folder
Set nms = Application.GetNamespace("MAPI")
Set fld = nms.PickFolder
'Open and activate Excel workbook.
Set appExcel = CreateObject("Excel.Application")
appExcel.Workbooks.Open (strSheet)
Set wkb = appExcel.ActiveWorkbook
Set wks = wkb.Sheets(1)
wks.Activate
appExcel.Application.Visible = True
'Copy field items in mail folder.
For Each itm In fld.Items
intColumnCounter = 1
Set msg = itm
intRowCounter = intRowCounter + 1
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = msg.Body
intColumnCounter = intColumnCounter + 1
Next itm
The issue is that each message is being put into a single cell when I want each line in outlook to have its own line in excel as if I were to copy and paste the body from outlook to excel manually (using ctrl+a, ctrl+c, ctrl+v, for example).
I feel like I need to use Split() to parse the body, but I've had no experience with that function and can't seem to get it to work.
EDIT:
I was able to solve this by using the below:
Sub SplitTextColumn()
Dim i As Long
Dim vA As Variant
[A1].Select
Range(Selection, Selection.End(xlDown)).Select
For i = 1 To Selection.Rows.Count
vA = Split(Selection.Resize(1).Offset(i - 1), vbLf)
Selection.Offset(i - 1).Resize(1, UBound(vA) + 1).Offset(, 1) = vA
Next
[A1].CurrentRegion.Offset(0, 1).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
End Sub
And
Sub MakeOneColumn()
Dim vaCells As Variant
Dim vOutput() As Variant
Dim i As Long, j As Long
Dim lRow As Long
If TypeName(Selection) = "Range" Then
If Selection.Count > 1 Then
If Selection.Count <= Selection.Parent.Rows.Count Then
vaCells = Selection.Value
ReDim vOutput(1 To UBound(vaCells, 1) * UBound(vaCells, 2), 1 To 1)
For j = LBound(vaCells, 2) To UBound(vaCells, 2)
For i = LBound(vaCells, 1) To UBound(vaCells, 1)
If Len(vaCells(i, j)) > 0 Then
lRow = lRow + 1
vOutput(lRow, 1) = vaCells(i, j)
End If
Next i
Next j
Selection.ClearContents
Selection.Cells(1).Resize(lRow).Value = vOutput
End If
End If
End If
Dim c As Range
Set rng = ActiveSheet.Range("A1:A5000")
For dblCounter = rng.Cells.Count To 1 Step -1
Set c = rng(dblCounter)
If c.Value Like "*MEADWESTVACO SUMMARY 856*" Then
c.EntireRow.Insert
End If
Next dblCounter
But I don't feel like I have the excel objects referenced quite right as those subs are being called from outlook VBA. I get an error exactly every other time I run it. That is to say I can run it once, it will work, but then the second time it will break, then the third it will work again. Any suggestions?

An example is the 'SplitEmByLine' function below, I left the ReturnString and PrintArray functions in for some clarity, but these can essentially be ignored.
Sub callSplitFunction()
Dim FileFull As String, a() As String, s As Long
FileFull = "C:\Users\thomas.preston\Desktop\ThisBookOfMine.txt"
'The below line calls function
a = SplitEmByLine(ReturnString(FileFull))
PrintArray a
End Sub
'*****The below function is what you need*****
Function SplitEmByLine(ByVal Body As String) As String()
Dim x As Variant
x = Split(Body, vbCrLf)
SplitEmByLine = x
End Function
Sub PrintArray(ByRef Arr() As String)
With Sheets("Sheet1")
For i = 0 To UBound(Arr)
.Cells(i + 1, 1).Value = Arr(i)
Next i
End With
End Sub
Function ReturnString(FilePath As String) As String
Dim TextFile As Integer
Dim FileContent As String
TextFile = FreeFile
Open FilePath For Input As TextFile
FileContent = Input(LOF(TextFile), TextFile)
Close TextFile
ReturnString = FileContent
End Function

Related

Copying images in an Excel file into a Word table

I am using Office 365 on a Windows 10 64-bit pc.
I have a Word document with a table into which I want to copy elements from an Excel document. The elements are a) text from its cell, b) a hyperlink from its cell and c) images from a list of images.
The first two tasks are performed successfully by the following sub:
Sub ImportFromExcel()
Dim RowNo As Long, RowTarget As Long
Dim RowFirst As Long, RowLast As Long
Dim strContent As String, strLink As String, strDisplay As String
Dim xlAppl As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Dim ExcelFileName As String
Dim tbl As Word.Table
On Error GoTo Finish
ExcelFileName = "C:\MyPath\MyExcelDoc.xlsm"
Set xlAppl = CreateObject("Excel.Application")
xlAppl.Application.Visible = False
xlAppl.Workbooks.Open ExcelFileName
Set xlBook = xlAppl.ActiveWorkbook
Set xlSheet = xlBook.Worksheets("Titan")
Set tbl = ActiveDocument.Tables(1)
RowFirst = 6: RowLast = 19
For RowNo = RowFirst To RowLast
RowTarget = RowNo - RowFirst + 1
strContent = xlSheet.Cells(RowNo, 5).Value
tbl.Cell(RowTarget, 1).Range.Text = strContent
strDisplay = xlSheet.Cells(RowNo, 3).Value
tbl.Cell(RowTarget, 3).Range.Text = strContent
strLink = xlSheet.Cells(RowNo, 3).Hyperlinks(1).Address
InsertHyperlinkInTable tbl, RowTarget, 3, strLink, strDisplay
' CopyImageFromExcelToWord xlSheet, RowTarget, tbl
Next RowNo
Finish:
xlAppl.ActiveWorkbook.Close False ' Word will not freeze at this point
xlAppl.Quit
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlAppl = Nothing
End Sub
I copy the hyperlink by reading its address and caption and then recreating it in Word.
Also from Word I can select a give image by way of its index using the first two active lines of the following sub:
Sub CopyImageFromExcelToWord(xlSheet As Excel.Worksheet, imgNo As Long, tbl As Word.Table)
Dim strId As String
' Syntax at https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.select
strId = "Picture " & CStr(2 * imgNo)
xlSheet.Shapes.Range(Array(strId)).Select
' Missing link !
With tbl.Cell(1, 4)
.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.VerticalAlignment = wdCellAlignVerticalCenter
.Select
End With
Selection.PasteAndFormat (wdFormatOriginalFormatting)
End Sub
An image residing in the clipboard can be inserted into Word using the last six lines.
But I have not found out how to copy the image I selected in the Excel document to the clipboard with a Word macro.
Can this be done somehow?
Can the copying of the hyperlink be performed in a smarter way?
Try
Sub CopyImageFromExcelToWord(xlSheet As Excel.Worksheet, imgNo As Long, tbl As Word.Table)
Dim strId As String
' Syntax at https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.select
strId = "Picture " & CStr(2 * imgNo)
xlSheet.Shapes.Range(Array(strId)).Item(1).Copy
With tbl.Cell(1, 4)
.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.VerticalAlignment = wdCellAlignVerticalCenter
.Range.PasteAndFormat wdFormatOriginalFormatting
End With
End Sub

How to skip code in workbook2 when closing file?

My problem is when closing workbook2 I need to use code to automatically select No on a message box that pops up. This is how my code is laid out:
Workbook1 creates multiple files based on user input.
The loop in Workbook1 opens up Workbook2 and inputs data from Workbook1.
When the loop is done inputing data it closes workbook2 and a message box pops up with a Yes or No button on it.
User at this time should always select No.
Another window ask if the user would like to save and it should always be yes.
Loop continues until it has created all the files user has requested
I tried googling variations of my question but have not had much luck. Any help is much appreciated.
Dim JobName As String
Dim lngLoop As Long
Dim i As Integer
Dim Customer As String
Dim LastRow As Long
Dim iCus As Integer
Dim CompanyName As String
Dim d As Long
Dim strDir As Variant
Dim DIV As String
Dim XL As Excel.Application
Dim WBK As Excel.Workbook
Dim ActSheet As Worksheet
Dim ActBook As Workbook
Dim CurrentFile As Workbook
Dim NewFileType As String
Dim NewFile As String
Dim QTR_NUM As String
Dim MFG As String
Dim Job As String
Dim visitdate As Variant
Dim visitdate_text As String
Dim Quote_Request As Worksheet
Dim QTR As Workbook
Dim QTRLOG As Workbook
Dim FORM As Workbook
Dim DCSProgram As Workbook
Dim ILast As Long
Dim j As Integer
Dim k As Integer
Dim CustomerIDNum As String
Dim QTRNUM As String
Dim FolderName As String
'Creates Quote For Each MFG
For j = 0 To QTRList.ListCount - 1
Set QTRLOG = Workbooks.Open("C:\QTR LOG.xlsm")
Set QTR = Workbooks.Open("C:\QTR.xlsx")
'CODE TO INPUT DATA FROM USERFORM NEW QTR
With DCSProgram.Sheets("MFG_DATA")
ILast = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 1 To ILast
If .Cells(i, 1).Value = MFG Then
QTR.Sheets(1).Range("B7").Value = .Cells(i, 2).Value
QTR.Sheets(1).Range("B8").Value = .Cells(i, 3).Value
QTR.Sheets(1).Range("B9").Value = .Cells(i, 4).Value
QTR.Sheets(1).Range("B12").Value = .Cells(i, 5).Value
QTR.Sheets(1).Range("B13").Value = .Cells(i, 6).Value
QTR.Sheets(1).Range("B14").Value = .Cells(i, 7).Value
QTR.Sheets(1).Range("B15").Value = .Cells(i, 8).Value
End If: Next: End With
With QTRLOG.Sheets("QTR_LOG")
ILast = .Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To ILast
If .Cells(i, 1).Value = QTR_NUM Then
.Cells(i, 2) = QTRList.List(j)
'.Cells(i, 3) = FORM.Sheets(1).Range("H11").Value
.Cells(i, 5) = JobName
.Cells(i, 8) = "OPEN"
.Cells(i, 9) = QTR.Sheets(1).Range("H9").Value
End If: Next: End With
QTRLOG.Save
QTRLOG.Close
QTR.SaveAs Filename:="C:\Users\Geoffrey\Dropbox\DCS PROGRAM\FILES\2. QUOTE REQUESTS\" & JobName & "\" _
& " DCS QTR " & QTRList.List(j) & " " & JobName & " (" & CustomerIDNum & ") " & visitdate_text & " .xlsx", _
FileFormat:=51, CreateBackup:=False, local:=True
'Code To Close File After Creating It
QTR.Close
Next j
End If
Application.ScreenUpdating = True
Call Shell("explorer.exe" & " " & "C:\Users\Geoffrey\Dropbox\DCS PROGRAM\FILES\2. QUOTE REQUESTS", vbNormalFocus)
Unload NewQTR
End Sub
When this code runs a msgbox appears from the workbook QTR. I dont want the user to have to click yes or no at this time. I want to automatically select No and continue on to the next file. This process is repeated for each MFG.
Code in QTR:
Application.ScreenUpdating = True
MSG1 = MsgBox("Are you ready to email to MFG?", vbYesNo, "EMAIL MFG")
If MSG1 = vbYes Then
'Code to create email and attached workbook as PDF
Else
Const kPath As String = "C:\"
Const kFile As String = "Users\Geoffrey\Dropbox\DCS PROGRAM\FILES\9. PROGRAM FILES\1. QUOTE REQUEST\QUOTE REQUEST LOG.xlsm"
Dim TOTALFOB As Double
Dim TOTALWC As Double
Dim Wbk As Workbook
Dim INWBK As Workbook
Dim QTR_NUM As String
Dim ILast As Long
Dim i As Long
Dim TOTMFG As Variant
Dim TOTWC As Variant
Dim LR As Long
Dim TOTALTIME As Variant
Set INWBK = ThisWorkbook
With Sheets("QTR")
LR = .Range("I" & Rows.Count).End(xlUp).Row
TOTALFOB = WorksheetFunction.Sum(.Range("I23:I" & LR))
End With
TOTALWC = TOTALFOB + INWBK.Sheets("QTR").Range("D18").Value
QTR_NUM = INWBK.Sheets("QTR").Range("H7").Value
TOTALTIME = INWBK.Sheets("WS_LOG").Range("J3").Value
Rem Set Wbk in case it's open
On Error Resume Next
Set Wbk = Workbooks(kFile)
On Error GoTo 0
Rem Validate Wbk
If Wbk Is Nothing Then Set Wbk = Workbooks.Open(kPath & kFile)
With Workbooks("QUOTE REQUEST LOG.xlsm").Sheets("QTR_LOG")
ILast = .Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To ILast
If .Cells(i, 1).Value = QTR_NUM Then
.Cells(i, 6) = TOTALFOB
.Cells(i, 7) = TOTALWC
.Cells(i, 10) = TOTALTIME
End If: Next: End With
Wbk.Save
Wbk.Close
End If
End Sub
If your problem is avoiding some Workbook_BeforeClose() event handler placed in "ThisWorkbook" code to be executed, then you must "enclose" the code lines that close the workbook like follows
Application.EnableEvents = False
' your code that closes the workbook
Application.EnableEvents = True
Exit Sub before end if is making the code exit earlier.
So remove the above mentioned one and check.

Getting error 1004 on my output range. Need to specify it correctly

I have a worksheet with the following data on it:
A B C D E
SF15-100 MFG1 JOB1 TOTALMFG TOTALWC
SF15-101 MFG2 JOB1
SF15-102 MFG3 JOB1
Im trying to write a loop to go thru column A and determine if that value is the same on a different workbook in a specific range.If its the same then it needs to paste values to the right of it in columns D and E.
i.e If
INWBK.Sheets("QTR").Range("H7").Value = "SF15-101"
Then
A B C D E
SF15-100 MFG1 JOB1 TOTALMFG TOTALWC
SF15-101 MFG2 JOB1 TOTALFOB TOTALWC
SF15-102 MFG3 JOB1
This is what I have tried so far:
Private Sub OKBTN_Click()
Dim TOTALFOB As String
Dim TOTALWC As String
Dim wbk As Workbook
Dim INWBK As Excel.Workbook
Dim TOTMFG As Variant
Dim TOTWC As Variant
Dim QTR_NUM As String
Dim ILast As Long
Dim i As Long
TOTALFOB = RefEdit1
TOTALWC = RefEdit2
Set INWBK = ActiveWorkbook
Set wbk = Workbooks.Open("C:\QUOTE REQUEST LOG 2015.xlsm")
QTR_NUM = INWBK.Sheets("QTR").Range("H7").Value
ILast = wbk.Sheets("QTR_LOG").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To ILast
If Cells(i, 1).Value = QTR_NUM Then
wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 4) = TOTALFOB
wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 5) = TOTALWC
Else
End If
Next i
ThisWorkbook.Save: ThisWorkbook.Saved = True
Unload Me
ActiveWorkbook.Close
End Sub
I get errors on:
wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 4) = TOTALFOB
wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 5) = TOTALWC
Run-time error '1004': Application-defined or object-defined error
No values were update as the workbook to be compared though declared was not used as the Cells(I,1) in this line was not qualified so the procedure was using whatever worksheet was active.
This is your code modified, please try and let me know about the results...
I assigned some values to RefEdit1 and RefEdit2 for testing
Private Sub OKBTN_Click()
Const kPath As String = "C:\"
Const kFile As String = "QUOTE REQUEST LOG 2015.xlsm"
Dim TOTALFOB As double
Dim TOTALWC As double
Dim Wbk As Workbook
Dim INWBK As Workbook
'Dim TOTMFG As Variant ' Not Used
'Dim TOTWC As Variant ' Not Used
Dim QTR_NUM As String
Dim ILast As Long
Dim i As Long
Dim RefEdit1, RefEdit2 'Not declared before
'Values Assigned for testing
TOTALFOB = 450
TOTALWC = 500
' TOTALFOB = RefEdit1
' TOTALWC = RefEdit2
Set INWBK = ThisWorkbook
Rem Set Wbk in case it's open
On Error Resume Next
Set Wbk = Workbooks(kFile)
On Error GoTo 0
Rem Validate Wbk
If Wbk Is Nothing Then Set Wbk = Workbooks.Open(kPath & kFile)
QTR_NUM = INWBK.Sheets("QTR").Range("H7").Value
With Wbk.Sheets("QTR_LOG")
ILast = .Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To ILast
If .Cells(i, 1).Value = QTR_NUM Then
.Cells(i, 4) = TOTALFOB
.Cells(i, 5) = TOTALWC
End If: Next: End With
INWBK.Save: INWBK.Saved = True
'Unload Me
Wbk.Close SaveChanges:=True
End Sub
Suggest to visit these pages:
Excel Objects, If...Then...Else Statement, On Error Statement
Range Object (Excel), Variables & Constants, With Statement
Do let me know of any question you might have about the code and resources used.
You miss row and column index in Cells function
wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 4) = TOTALFOB
wbk.Sheets("QTR_LOG").Range(Cells).Offset(0, 5) = TOTALWC
Const kPath As String = "C:\"
Const kFile As String = "QUOTE REQUEST LOG 2015.xlsm"
Dim TOTALFOB As Variant
Dim TOTALWC As String
Dim Wbk As Workbook
Dim INWBK As Workbook
Dim QTR_NUM As String
Dim ILast As Long
Dim i As Long
Dim TOTMFG As Variant
Dim TOTWC As Variant
Dim LR As Long
Set INWBK = ThisWorkbook
With Sheets("QTR")
LR = .Range("I" & Rows.Count).End(xlUp).Row
TOTALFOB = WorksheetFunction.Sum(.Range("I23:I" & LR))
End With
'Values Assigned for testing
' TOTALFOB = 450
' TOTALWC = 500
TOTALWC = TOTALFOB + INWBK.Sheets("QTR").Range("D18").Value
QTR_NUM = INWBK.Sheets("QTR").Range("H7").Value
Rem Set Wbk in case it's open
On Error Resume Next
Set Wbk = Workbooks(kFile)
On Error GoTo 0
Rem Validate Wbk
If Wbk Is Nothing Then Set Wbk = Workbooks.Open(kPath & kFile)
With Wbk.Sheets("QTR_LOG")
ILast = .Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To ILast
If .Cells(i, 1).Value = QTR_NUM Then
.Cells(i, 6) = TOTALFOB
.Cells(i, 7) = TOTALWC
End If: Next: End With
'INWBK.Save: INWBK.Saved = True
'Unload Me
'Wbk.Close SaveChanges:=True
End If
End Sub

Copy specific cell in multiple workbook to one wok book using vba

Hello please help me yo solve this.
I have multiple workbook and I want to copy a specific cell in each workbook into another workbook orderly.
Great thanks
Sophannaa
Copy this whole code to the list of your choice(to visual basic of course).
Run this by running begin().
Change values in part"HERE COMES YOUR PART!!!"
Sub begin()
ThisWorkbook.Save
DoEvents
Const ROW_FIRST As Integer = 5
Dim intResult As Integer
Dim strPath As String
Dim objFSO As Object
Dim intCountRows As Integer
Dim sourceWB As Workbook
Dim targetWB As Workbook
Set targetWB = ThisWorkbook
Dim xrow As Integer
xrow = 5
Application.FileDialog(msoFileDialogFolderPicker).Title = "Please select File to load"
Application.FileDialog(msoFileDialogFolderPicker).ButtonName = "Choose a file"
intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
For Each Item In Application.FileDialog(msoFileDialogFolderPicker).SelectedItems
If intResult <> 0 Then
Application.ScreenUpdating = False
Range("A:A").ClearContents
Range("B:B").ClearContents
Range("C:C").ClearContents
Cells(4, 1).Value = "NAME"
Cells(4, 2).Value = "PATH"
Cells(4, 3).Value = "TAIL"
strPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
Set objFSO = CreateObject("Scripting.FileSystemObject")
intCountRows = GetAllFiles(strPath, ROW_FIRST, objFSO)
Call GetAllFolders(strPath, objFSO, intCountRows)
Application.ScreenUpdating = True
End If
Next Item
Cells(1, 1).Value = Application.WorksheetFunction.CountA(Range("B:B")) - 1
'HERE COMES YOUR PART!!!
Dim nextrow As Integer
nextrow = 2 'choose starting row where to copy the results
Do
strFile = Cells(xrow, 2).Value
Set sourceWB = Workbooks.Open(strFile)
targetWB.Sheets("desired sheet to copy to").Cells(nextrow, 1) = sourceWB.Sheets("desired sheet to copy from").Cells(2, 1)
'instead of cells(2,1) up here ^^ and here^^ choose what cells you want to copy from, edit only numbers
sourceWB.Save
sourceWB.Close
xrow = xrow + 1
nextrow = nextrow + 1
Loop Until ThisWorkbook.Sheets(1).Cells(xrow, 2).Value = ""
End Sub
Private Function GetAllFiles(ByVal strPath As String, ByVal intRow As Integer, ByRef objFSO As Object) As Integer
DoEvents
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
i = intRow - ROW_FIRST + 1
Set objFolder = objFSO.GetFolder(strPath)
For Each objFile In objFolder.Files
inte = InStr(1, objFile.Name, "prázdný")
Cells(i + ROW_FIRST - 1, 1) = objFile.Name
Cells(i + ROW_FIRST - 1, 2) = objFile.Path
Cells(i + ROW_FIRST - 1, 3) = Right(objFile.Name, Len(objFile.Name) - InStrRev(objFile.Name, "."))
i = i + 1
Next objFile
GetAllFiles = i + ROW_FIRST - 1
End Function
Private Sub GetAllFolders(ByVal strFolder As String, ByRef objFSO As Object, ByRef intRow As Integer)
DoEvents
Dim objFolder As Object
Dim objSubFolder As Object
Static veSpravneSlozce As Boolean
Set objFolder = objFSO.GetFolder(strFolder)
For Each objSubFolder In objFolder.subfolders
intRow = GetAllFiles(objSubFolder.Path, intRow, objFSO)
Call GetAllFolders(objSubFolder.Path, objFSO, intRow)
Next objSubFolder
End Sub
Since it is really useless to copy all values to one cell a added nextrow to shift cells and write down values one after another

how to scrape aka display all contents of an excel document in vb6

i made a program to read an excel document and display it on a messageBox. but, the thing is, i want to scrap aka display all of it to messageBox without knowing which row or column to choose. i wrote this code:
Private Sub Command1_Click()
On Error GoTo Err
StartExcel
Set ExcelWBk = Excel.Workbooks.Open(App.Path & "\Dataku.xls")
Set ExcelWS = ExcelWBk.Worksheets(1)
With ExcelWS
Dim i As Integer
Dim strData As String
For i = 1 To 5
strData = strData & .Cells(i, 1) & vbCrLf
Next i
End With
MsgBox strData
CloseWorkSheet
ClearExcelMemory
Exit Sub
Err:
ClearExcelMemory
End Sub
but it returned into datas of that column (column 1) only. i need to read whole excel file.
Something like this (tested in PowerPoint as I dont have VB6) will get the UsedRange of the first sheet cell by cell (using an array for efficiency)
Pls change your file path to suit.
First version - tested in PowerPoint
Sub GetData()
Dim objExcel As Object
Dim objWB As Object
Dim objws As Object
Dim X As Variant
Dim lngCol As Long
Dim lngRow As Long
Dim strOut As String
Set objExcel = CreateObject("Excel.Application")
On Error Resume Next
Set objWB = objExcel.Workbooks.Open("c:\temp\test.xlsx")
Set objws = objWB.Sheets(1)
On Error GoTo 0
If objws Is Nothing Then Exit Sub
'recalc usedrange
objws.usedrange
X = objws.usedrange
For lngRow = 1 To UBound(X, 1)
For lngCol = 1 To UBound(X, 2)
strOut = strOut & (X(lngRow, lngCol) & vbNewLine)
Next
Next
objWB.Close False
objExcel.Quit
Set objExcel = Nothing
If Len(strOut) > 0 Then MsgBox strOut
End Sub
VBS version
Dim objExcel
Dim objWB
Dim objws
Dim X
Dim lngCol
Dim lngRow
Dim strOut
Set objExcel = CreateObject("Excel.Application")
On Error Resume Next
Set objWB = objExcel.Workbooks.Open("c:\temp\test.xlsx")
Set objws = objWB.Sheets(1)
On Error GoTo 0
If IsEmpty(objws) Then Stop
'recalc usedrange
objws.usedrange
X = objws.usedrange
For lngRow = 1 To UBound(X, 1)
For lngCol = 1 To UBound(X, 2)
strOut = strOut & (X(lngRow, lngCol) & vbNewLine)
Next
Next
objWB.Close False
objExcel.Quit
Set objExcel = Nothing
If Len(strOut) > 0 Then MsgBox strOut

Resources