Runtime error using VLookup with Excel - excel

I keep getting:
a Run-time error '1004' Unable to get the Vlookup property of the WorksheetFunction class
with the Vlookup code below.
If I enable Microsoft outlook 12.0 Object Library it works but I run into issues where the code is used with Excel 2013 and adds Microsoft outlook 15.0 Object Library references that are not available on excel 2007. I have incorporated late binding which has worked for the most part except for this one bit of code.
I have included a snippet of code which I hope is enough for someone to help me.
Sub Button154_Click()
Dim forename As String
Dim surname As String
Dim movedate As String
Dim callref As String
Dim dept As String
Dim deptmove As String
Dim wb As Workbook
Set wb = ThisWorkbook
forename = Sheet1.Range("f8").Value
surname = Sheet1.Range("f9").Value
movedate = Sheet1.Range("k13").Value
callref = Sheet1.Range("k8").Value
dept = Application.WorksheetFunction.VLookup(Name, Sheet1.Range("K10"), 1)

Added late binding code
Dim otlApp As Object
Set otlApp = CreateObject("Outlook.Application")
An changed the vlookup to:
dept = Application.WorksheetFunction.VLookup(oltApp, Sheet1.Range("K10"), 1)
Now works great

Related

How to get the name property of the active NamedSheetView class?

Excel now has the possibility to store personal filtering views to help collaboration in simultaniously used documents.
I could only find Microsoft documentation for an add-in, but the function is available in my Excel version of MS Excel for Microsoft 365 MSO (16.0.13127.20266) 32bit.
https://learn.microsoft.com/en-us/javascript/api/excel/excel.namedsheetview?view=excel-js-preview
I am trying to store the currently applied NamedSheetView name property (for later restoring option) but
this code fails:
Dim sh1 As Worksheet
Dim xViewName As String
Set sh1 = ThisWorkbook.Sheets(Sheet6.Name)
xViewName = sh1.NamedSheetView.Name
However this code works (with previously created "Test" view):
sh1.NamedSheetViews.GetItem("Test").Activate
If this NamedSheetViews is a collection, I should be able to get the item property, but these codes also fail:
strName = sh1.NamedSheetViews.GetItem(1).Name
strName = sh1.NamedSheetViews.Item(1).Name
Anyone has ever succeeded in getting the current NamedSheetView of a Worksheet?
Here is how I probe unknown Object properties:
I start with a reference to the Object. If I don't know what the Object is I use TypeName() to return it's class name (data type). I then declare a variable of that data type. Wash, rinse and repeat as I drill down the structure. Once the variable is declared, selecting the variable and pressing F1 with open the Microsoft Help document for that data type.
Module Code
Sub WhatIsThat()
Const TestName As String = "TestName"
Dim View As NamedSheetViewCollection
Set View = Sheet6.NamedSheetViews
On Error Resume Next
View.GetItem(TestName).Delete
On Error GoTo 0
View.Add TestName
Dim SheetView As NamedSheetView
Dim n As Long
For n = 0 To View.Count - 1
Debug.Print View.GetItemAt(n).Name
Set SheetView = View.GetItemAt(n)
Debug.Print SheetView.Name
Next
Stop
End Sub
Immediate Window Tests
?TypeName(Sheet6.NamedSheetViews)
?View.GetItemAt(0).Name
?TypeName( View.GetItemAt(0))
SOLUTION:
(Thanks for the great help from TinMan)
Dim SheetView As NamedSheetView
Dim sh1 As Worksheet
Dim ActiveSheetView as string
Set sh1 = ThisWorkbook.Sheets(Sheet6.Name)
Set SheetView = sh1.NamedSheetViews.GetActive
ActiveSheetView = SheetView.Name
Application:
sh1.NamedSheetViews.GetItem(ActiveSheetView).Activate

Debug doesn't work with library references - Errormsg "Cant enter break mode at this time"

I wrote a macro in Excel VBA to make users send their Excel-File via E-Mail automatically back to me.
To use this macro every user must install the Outlook Library. For this I created the function add_outlook. If I try to run the function it works.
The only problem occurring is that VBA doesn't let me debug. When stepping through the code I get the Errormsg "Cant enter break mode at this time"
Is there a workaround or fix?
Thanks a lot!
Option Explicit
Public Function add_outlook()
'DEBUGGING DOESNT WORK
'late binding
Dim vbProj As Object
Set vbProj = ThisWorkbook.VBProject
Dim vbRefs As Object
Set vbRefs = vbProj.References
Dim vbRef As Object
'Libary GUID and Data
Dim libname As String
libname = "Outlook"
Dim guid As String
Dim major As Long
Dim minor As Long
Dim exists As Boolean
guid = "{00062FFF-0000-0000-C000-000000000046}"
major = 9
minor = 6
'Reference cleanup function
For Each vbRef In vbRefs
If vbRef.Name = libname Then
'problem occurs here
vbRefs.Remove Reference:=vbRef
End If
Next
'add Ref
vbRefs.AddFromGuid guid:=guid, major:=major, minor:=minor
End Function
Solution:
Sub Workbook_Open()
Call add_outlook
End Sub
Cant step through code
Get reference when Workbook_open event is triggered

Creating Word document from Excel through VBA

Folks. I have an Excel file and want to create a Word document with the data in a sheet. The program shows an error and I can't find the reason.
I tried to use the following code:
Private Sub CommandButton1_Click()
' Objetos Word
Dim obj1 As New Application
Dim wdDoc As Word.Document
' Objetos Excel
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim Caminho, Arquivo, Nome_aluno, Ender As String
Dim Gen_p, Gen_a, Hora, Prof, Resp As String
Dim i, Comp As Integer
Dim Coord_C As Integer
Dim Coord_L As Integer
Caminho = "D:\Data\Office\Excel\"
Arquivo = "Anexo D - Ata de defesa TCC.docx"
The code continues, but is irrelevant for now.
The point is when I run the code I get the message:
User defined type not defined
This is weird, because in another Excel file I can run it, and it works fine.
What have I missed? Do I have to link something?
To create a new Word doc from another application, add this...
Dim appWD As Word.Application
Set appWD = CreateObject("Word.Application")
appWD.Documents.Add
https://learn.microsoft.com/en-us/office/vba/excel/concepts/working-with-other-applications/controlling-one-microsoft-office-application-from-another
since your code uses:
Dim obj1 As New Application
You must set a VBA reference to whatever application (Word?) obj1 relates to. That is done via Tools|References. You must also tell VBA what application that is. For example:
Dim obj1 As New Word.Application

How to use OpenOffice Spreadsheet to get an image from an excel file

I have a code that exports image from excel into a picturebox and here it is.
Dim appExcel As Object
Set appExcel = CreateObject("Excel.Application")
appExcel.Visible = False
Dim xlsBook As New excel.Workbook
Dim xlsSheet As New excel.Worksheet
Dim rowlocation As Integer
Dim columnlocation As Integer
Dim celladdress As String
Set xlsBook = appExcel.Workbooks.Open(Text1.Text)
Set xlsSheet = xlsBook.Worksheets("Sheet1")
Dim x As excel.Shapes
For Each x In xlsSheet.Shapes
x.Copy
Picture1.Picture = Clipboard.GetData(vbCFBitmap)
Text2.Text = x.Name
rowlocation = x.TopLeftCell.Row
columnlocation = x.TopLeftCell.Column
celladdress = xlsSheet.Cells(x.BottomRightCell.Row + 1, x.TopLeftCell.Column).Address(RowAbsolute:=False, ColumnAbsolute:=False)
MsgBox ActiveSheet.Range(celladdress)
Next
End If
and unfortunately this code wont work on my friends PC becuase he does not have an Excel installed but he has OpenOffice spreadsheet. I tried to open the Excel in Openoffice then the file opens now my goal is how can i convert the code above in OpenOffice? I mean run the code for OpenOffice files.
This is my code but its not working
Dim objServiceManager As Object
Dim objDesktop As Object
Dim objDocument As Object
Dim objText As Object
Dim objCursor As Object
Dim oDoc As Object
Dim ARG()
Dim oGraph As Object
Dim oView As Object
Dim oDrawPage As Object
Dim oSheet As Object
Dim Image As System_Drawing.Image
Dim oimage As Object
Dim osize As Object
Set objServiceManager = CreateObject("com.sun.star.ServiceManager")
Set objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
Set oDoc = objDesktop.loadComponentFromURL("file:///C:\Users\paul\Desktop\Testing.ods", "_blank", 0, ARG())
Set oSheet = oDoc.getSheets().getByIndex(0)
Set oGraph = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Set oView = oDoc.CurrentController
Set oDrawPage = oView.getActiveSheet.DrawPage
For i = 0 To 2
For j = 0 To 9
' Form1.Image1.Picture = Clipboard.GetData
Form1.Image1.Picture = LoadPicture(oDrawPage)
Next
Next
TYSM for future help
This is the latest code in VB6 and it has an error saying vnd.sun.star is missing
Dim objServiceManager As Object
Dim objDesktop As Object
Dim objDocument As Object
Dim objText As Object
Dim objCursor As Object
Dim oDoc As Object
Dim ARG()
Dim oGraph As Object
Dim oView As Object
Dim oDrawPage As Object
Dim oSheet As Object
Dim Image As System_Drawing.Image
Dim oimage As Object
Dim osize As Object
Dim Cell As Object
Dim sGraphicUrl As String
Dim oDisp
Dim oFrame
Dim opos As Object
Set objServiceManager = CreateObject("com.sun.star.ServiceManager")
Set objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
Set osize = objServiceManager.Bridge_GetStruct("com.sun.star.awt.Size")
Set opos = objServiceManager.Bridge_GetStruct("com.sun.star.awt.Point")
Set oDoc = objDesktop.loadComponentFromURL("file:///C:\Users\paul\Desktop\ACE Express - Fairview_Sample PC of Gondola.ods", "_blank", 0, ARG())
Set oSheet = oDoc.getSheets().getByIndex(0)
Set oimage = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Set oView = oDoc.CurrentController
Set oDrawPage = oView.getActiveSheet.DrawPage
Set oimage = oDrawPage.getByIndex(0)
Image1.Picture = LoadPicture(oimage.GraphicURL)
Here is the output of the unzip picture
I do not know exactly what your code does, because I normally do not use Microsoft Office. However it looks like this task can be accomplished using OpenOffice Basic. One of the best places to learn OpenOffice Basic is Andrew Pitonyak's Macro Document.
To start with, look at section 5.9.5. Convert all linked images.
EDIT:
To do this in Calc, first I went to Tools -> Macros -> Organize Dialogs and created a dialog named "ImageViewerForm" with an image control named "MyImageControl".
Then I went to Tools -> Macros -> Organize Macros -> OpenOffice Basic and added the following code:
Sub ShowImageViewerDialog
oDoc = ThisComponent
oDlg = CreateUnoDialog(DialogLibraries.Standard.ImageViewerForm)
oControl = oDlg.Model.MyImageControl
oDrawPage = oDoc.getDrawPages().getByIndex(0)
oImage = oDrawPage.getByIndex(0)
oControl.ImageURL = oImage.GraphicURL
oDlg.execute()
End Sub
To run the code, go to Tools -> Macros -> Run Macro. Here is the result:
The "Next Image" button should be fairly straightforward to implement by adding an event handler.
For documentation, see GraphicObjectShape and UnoControlButtonModel. But mostly I just used the MRI tool to figure it out.
EDIT 2:
Regarding the error you posted, the GraphicURL property in this case is a string that references an in-memory object, not a filepath. An example of the string is shown here: https://www.openoffice.org/api/docs/common/ref/com/sun/star/graphic/XGraphicObject.html.
So it is not suitable for passing to LoadPicture, which takes a filename.
Perhaps you can get the actual image data from oImage.Bitmap or oImage.Graphic. Use MRI to see what attributes are available.
For example, it looks like there is a getDIB() method that might work like this:
Form1.Image1.Picture = oImage.Bitmap.getDIB()
One more idea: Instead of using an Office application, you could write code that unzips the file and reads each image in the Pictures subdirectory.
I have never tried it but according to the docs you can control Open Office through COM automation.
Your VB6 code above is controlling Excel through COM automation. You could install Open Office on your machine, and see whether you can automate Calc from VB6 to open a spreadsheet and extract an image. I don't know whether it allows that. Excel COM automation is very powerful and allows you to do almost anything, Open Office may not be as powerful (I don't know).
I would advise thinking carefully about what problem you are trying to solve and whether there's another approach entirely!

Getting Name of Worksheet C++/CLI

How do I get the name of a worksheet in an Excel workbook using C++/cli?
I understand when you use C# you can do this:
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
string strWorksheetName = worksheet.Name;
Although I don't know how to do the:
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
in C++.
This would translate to the following C++/CLI:
Excel::Worksheet^ worksheet = dynamic_cast<Excel::Worksheet^>(sheets->get_Item(1));
String^ strWorksheetName = worksheet->Name;

Resources