I have the following code in word VBA 2010
Dim oExcel As excel.Application
Dim oWB As workbook
Set oExcel = New excel.Application
Set oWB = oExcel.Workbooks.Open("C:\users\jane\desktop\excelSheet.xlsx")
oExcel.Visible = True
Set oWB = oExcel.Workbooks.Open()
it has throws a user defined type during the first line - basically, the code has no idea what an excel.Application is; but it knows what an Application is. How to make it know what an excel.Application is? Also, assuming it knows what an excel.Application is, would this code open excelSheet.xlsx at the given location?
In the VBA Editor go to Tools > References and check the Reference to Excel.
Here is a picture (check the line in blue):
You have to reference the Excel Vba Library to you Document. Below you see the Vba Editor Window from within a Word Document. You have to click on the menue point Extras and then on References (I am using a german version of Word so I am assumig that these points are named like that). Then you have to refrence the library you see in the picture (the version is not important, just pick whichever latest version is available to you).
When you did refrence it you should that lines like Dim oExcel As excel.Application change to Dim oExcel As Excel.Application, indicating that your VBA environment now knows what Excel. ... is. Please note that there are two ways to reference in VBA: early binding and late binding. When you reference as I described you use early binding, which is good for development but bad for everything else. To learn how to you late binding instead a first start would be this article.
Edit:
Oh someone answered faster. I will let this answer live though, so you can check the binding part.
Related
Normally when using the VBA editor, code autosuggests from available methods.properties for whatever object being referenced.
I'm trying to pull data from an Excel sheet into a Word document using a macro on the Word document. Whenever I try to use worksheets.activate, no autosuggestion for activate comes up, leading me to think it's not activating. Neither can I use it from a VBA script in Excel.
My script is still in it's beginning stages:
Sub Populate()
Dim doc As Document
Set doc = ActiveDocument
Dim appXL As excel.Application
Set appXL = CreateObject("excel.Application")
Dim partnerNames As excel.Workbook
Dim ihmNames As excel.Workbook
Set partnerNames = appXL.Workbooks.Open("D:/Database/Imports and Exports/Funder Credit Lists/2022-01 Partners.csv")
Set ihmNames = appXL.Workbooks.Open("D:\Database\Imports and Exports\Funder Credit Lists\2022-01 IHM.csv")
appXL.Worksheets(Left(partnerNames.Name, Len(partnerNames.Name) - 4)).Activate
Dim lastRow As Long
lastRow = appXL.Cells.Find(What:="*", After:=Range("C1"), SearchOrder:=xlByRows, searchDirection:=xlPrevios).Row
appXL.Range("A1").Resize(lastRow, 3).Select
'Insert Hero Names
Dim hero As Range
Set hero = doc.Range(Start:=doc.Bookmarks("Hero").Start, End:=doc.Bookmarks("Hero").End)
hero.InsertAfter ("IT WORKS!!!")
End Sub
The lastRow = appXL.Cells..... is causing a type mismatch, which I believe is being caused by the fact that appXL.Cells refers to the active sheet, and the ActiveDocument is a Word document.
That leads me to activating the sheet, but trying to do so causes the error "Subscript out of range," even if I explicitly type the sheet name.
appXL is an excel.Application object. Worksheets property belongs to Workbook class. You can use just Worksheets.(...) to refer the active workbook sheets. Same to property .Cells
Or you can define a new Workbook variable and handle it:
Dim wbXL as Workbook
set wbXL = ActiveWorkbook
wbXL.Worksheets(...).Activate
It seems you just need to add an Excel COM reference in Word VBA to be able to get auto-suggestions. From the Tools menu, choose References to display the References dialog box. The References dialog box shows all object libraries registered with the operating system. Scroll through the list for the application whose object library you want to reference. References whose check boxes are selected are used by your project; those that aren't selected are not used, but can be added. Select the object library reference in the Available References box in the References dialog box and choose OK. Your Visual Basic project now has a reference to the application's object library. If you open the Object Browser (press F2) and select the application's library, it displays the objects provided by the selected object library, as well as each object's methods and properties. In the Object Browser, you can select a class in the Classes box and select a method or property in the Members box.
So the problem was specifically the "After:=Range" portion in the appXL.Cells.Find function. I forgot that, since I'm working from a word doc and not an excel, I needed to specify appXL.Range instead of just Range. Oh the joy of finding out my weeklong problem was just a simple missed class specification.
That said, thanks to #Eugene for informing me of the Object Browser window. That was useful.
Helo all,
I'm trying to automate the reporting system at my work. To this end I'm setting up a Macro that, at the press of a button in Excel, will:
Open a new presentation from a given template (.potx) in a given path
Add various charts and tables
Save the newly created presentation in another given path
As this is pretty new to me I'm moving step by step, but I'm stuck at the beginning. This is my code so far:
Dim PP As PowerPoint.Application
Dim report As PowerPoint.Presentation
Dim report_activeslide As PowerPoint.Slide
'Dim Slide_1_text As Shape
Dim path_template As String
Dim path_report As String
path_template = "path_template.potx"
path_report = "path_report"
Set PP = New PowerPoint.Application
Set report = PP.Presentations.Open(path_template, False, True, True)
PP.Visible = msoTrue
'Set report_activeslide = report.Slides(1)
report.SaveAs path_report, ppSaveAsOpenXMLPresentation, msoTrue
End Sub
As of now I'm able to open the presentation from the template and correctly save it. However, the moment I actually start doing anything on the presentation itself, for example taking the comment off the
'Set report_activeslide = report.Slides(1) line, Excel hard crashes.
Does anyone know where the problem is?
I'm running Office 365 on Mac if that may be of any difference.
As Ike in the comments pointed out, the code works on Windows (I had the chance to test it myself). This pointed me to the possibility of my problem being due to Mac, not to code and, as it turns out, this is correct. Indeed there are more than a few people reporting the same issue (see for example: Excel VBA crashing when referencing a PowerPoint slide index number)
So for now, until Microsoft provides a better implementation of OLE there is nothing I can realistically do to solve this.
I am trying to import data from an Excel sheet into Word.
But I am failing pretty quickly. Word gets stuck right at the beginning.
My code is a simple as that:
Sub ImportDataFromExcel()
Dim XLapp As Excel.Application
Set XLapp = New Excel.Application
End Sub
Here is what happens:
Excel opens up.
But Word freezes immediately. I have to force quit Word.
Word would not get to any further line of code I had included. It stops at „Set XLapp = New Excel.Application“.
I tried a couple of things:
I wrote similar code to open PowerPoint. That worked.
I wrote similar code into PowerPoint, to open Word from there. That worked.
I wrote the same code, that I posted here, into PowerPoint, to open Excel. That failed the same way!
And I also tried on a different Mac. Also there, same thing: Word freezes, when calling on Excel.
I am using Office 365 with Word and Excel for Mac, Version 16.24.
As macro-reference in VBA I use the „Microsoft Excel 16.0 Object Library“.
I am running MacOS Mojave (10.14.3) on my Mac.
Can anyone please help me?
I have no experience with this issue on Mac Os, but maybe rewriting the same code in a different way, will work. Try this:
Dim XLapp As New Excel.Application
Hope this helps.
Try this please
Sub ImportDataFromExcel()
Dim XLapp As Object
Set XLapp = CreateObject("Excel.Application")
End Sub
Maybe MAC do not recoginized the excel library
I am writing a macro in outlook for the first time and need some excel functions. So I go to Tools > Reference and choose the "Microsoft Excel 15.0 Object Library", then test it in the immediate window. Below is what I wrote in immediate window:
? WorksheetFunction.Weekday(Now(), 2)
There is some observable but minimal lag (may be just 0.2s or less) for it to show the answer, but this is not the case if I try same thing in excel. I have to admit this does not hurt the macro much but I am curious. Why did outlook get the worksheetfunction object slower? Am I adding the library in a wrong way?
Update:
about preventing unqualified reference, is that I should use some code like below
Dim oExcel As Excel.Application
Set oExcel = CreateObject("Excel.Application")
'main body here
oExcel.Quit
Set oExcel = Nothing
The WorksheetFunction object is a member of the Excel Application object. When you call a WorksheetFunction member from Outlook, you're implicitly creating an instance of Excel, which will take time to load.
If you need the WeekDay function, there's already something similar in the VBA library:
Function Weekday(Date, [FirstDayOfWeek As VbDayOfWeek = vbSunday])
Member of VBA.DateTime
I want to reference different workbooks in my code and I have used this code:
Dim Basicwb As Excel.Workbook
Dim Basic As Excel.Application
Set Basic = New Excel.Application
Set Basicwb = Basic.Workbooks.Open("X:\Job\Masroori\3-042-PMS.xlsx")
but the problem is how can I refrence it if I dont want to open it each time. I used this code (without .Open) but I get this Error! : "Subscript out of range"
Set Basicwb = Basic.Workbooks("X:\Job\Masroori\3-042-PMS.xlsx")
Also, I dont want to activate the workbook each time, Is there any way?
Taken from the msdn site for the Workbooks property:
"Returns a Workbooks collection that represents all the open workbooks. Read-only."
hence the last line of your code gives you an error since the file is not open. AFAIK, you cannot reference objects within a workbook if that workbook is not open. You can access whatever a workbook has without activating it (so without using .Activate), but it has to be open. Maybe this SO question is of help to you:
Open Excel file for reading with VBA without display
If your workbook is open, you can do the following:
Dim wBook as Excel.Workbook, rngTemp as range
Set wBook = workbooks("wbName.xls")
With wBook
' Do stuff, no need to activate. Example:
set rngTemp=.sheets(1).usedRange
End With
I hope this helps?