Getting Name of Worksheet C++/CLI - excel

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;

Related

VBA Using a variable to select a worksheet

I'm attempting to code a system where I Select data from a set of worksheets whose titles are generated by the user. The Titles of the created sheets are then stored as strings. Is there a way to open/refer to a worksheet using these strings?
My code is as below
Dim Title As String
Dim Sheet_title As Worksheet
Sheets("Config").Select
Sheets("config").range("C25").Select
Title = ActiveCell.Value
Debug.Print Title
Sheet_title = Title
Sheets("Results").range("B7") = Sheets(title).range("E8")
The "sheets(Title)" fails, as well as my attempt to fix it by setting Sheet_Title = Title (Object variable not set - Error 91).
Is there any way to select a worksheet using a string variable?
Referencing Objects (Set)
A Quick Fix
To reference a worksheet (any object), you need to use the Set keyword:
Set Sheet_title = Sheets(Title)
Sheets("Results").Range("B7").Value = Sheet_title.Range("E8").Value
A Recommendation
Option Explicit
Sub Test()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim wsCon As Worksheet: Set wsCon = wb.Sheets("Config")
Dim Title As String: Title = wsCon.Range("C25").Value
Debug.Print Title
Dim wsTit As Worksheet: Set wsTit = wb.Sheets(Title)
Dim wsRes As Worksheet: Set wsRes = wb.Sheets("Results")
wsRes.Range("B7").Value = wsTit.Range("E8").Value
End Sub

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!

Runtime error using VLookup with 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

Get sheet name from a named range's Name object

I have:
Microsoft.Office.Interop.Excel.Workbook wb;
Microsoft.Office.Interop.Excel.Name name;
Is there any way to get the worksheet name that the named range is on in the given workbook, assuming I've gotten the named range's Name object and wb already?
Yes, use the Parent property to work your way up the object hierarchy:
ws = name.RefersToRange.Parent.name;
Range.Worksheet is a self-documenting alternative to Range.Parent:
string wsName = name.RefersToRange.Worksheet.Name;
(Or in 2 steps:
Microsoft.Office.Interop.Excel.Worksheet ws = name.RefersToRange.Worksheet;
string wsName = ws.Name;
)
Reference:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.name.referstorange.aspx
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.worksheet.aspx
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._worksheet.name(v=office.15).aspx
wb.Names(name).RefersToRange.Parent.Name

Resources