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
Related
When using the Documents.Open("XX") command in excel. My excel prompts the window where it says that the document is lock for editing, by ME. Which isn't the case.
This command does seem to work when i'm using microsoft word/excel 2010 at work. But at home, using office 2016, it doesn't. This goes for my personal laptop with office 2016 aswell.
The following code is used:
Sub test()
Dim wordDoc As Word.Application
Dim testDoc As Word.Application
Set wordDoc = CreateObject("Word.Application")
Set testDoc = wordDoc.Documents.Open("C:\Users\Me\Desktop\Test.docm")
' Do stuff
End Sub
My thoughts:
Office 2016 seems to open a 'Shadow file' which gives the document 'Open' status. But when restarting my PC the code simply does NOT work. Giving an 'Filename' error, upon restarting the code the 'Locked for editing' error appears.
My question:
Am i using the wrong approach on opening a document in Office 2016? If so, what is the right approach? If not, are there settings that need to be changed in order for this to work?
Thanks in advance,
I've had this happen before, the issue for me was that a previous run of the macro wasn't able to close the file properly. Give this thread a read:
Application.Quit command not closing the entire Excel Application
You could test to see if this is the case by "End Task"ing all MS Word instances and running your macro again, If it's able to run the first time, then this is your issue.
Also, This is the code I used for testing. Dim'ing the variables as "Word.Application" didn't work for me, so if my earlier suggestion doesn't work then try just "Object" instead:
Sub openWord()
Dim wrd As Object
Dim doc As Object
Set wrd = CreateObject("Word.Application")
wrd.Visible = True
Set doc = wrd.Documents.Open("C:\Users\name\Desktop\doc.docx")
doc.Close
wrd.Quit
End Sub
I bought a software (with a large database), and its output is a simple Excel workbook, not saved anywhere (no path), named generically "Book1", that simply pops up on my screen.
Every time I ask the software for this output, I need to copy the content of this workbook and paste into another workbook, a mother-workbook, as I named it, to consolidate all the data.
I have to repeat this action dozens of times a day, so I thought it would be a great idea to create some VBA code to automate this task.
So... I made a very simple one:
ActiveWorkbook.ActiveSheet.Range("A1:C32").Copy
Workbooks("Mother-Workbook.xlsm").Worksheets("Sheet1").Range("B6:D37").PasteSpecial Paste:=xlPasteValues
The problem is... Each time the software outputs a new workbook, it seems that it is created in a new instance of Excel, which my macro can't reach. I mean, I run the code, but nothing happens, because my mother-workbook doesn't find the generic, unsaved and located in another excel instance "Book1".
If I open the mother-workbook after the output is opened, OK, the code works, because both are in the same instance. But as I need to keep the mother-workbook open all the time, I can't do this. I don't want to save each new output file either. It would take me a lot of time.
I'm using the 2016 version of Excel, but already tried the 2010 as well. My OS is Windows 10 Pro.
Any thoughts?
This code should do it.
Dim xlapp As Object
Set xlapp = GetObject("Book1").Application
xlapp.ActiveWorkbook.ActiveSheet.Range("A1:C32").Copy
Workbooks("Mother-Workbook.xlsm").Worksheets("Sheet1").Range("B6:D37").PasteSpecial Paste:=xlPasteValues
xlapp.DisplayAlerts = False
xlapp.Quit
Note that you need to close "Book1" at the end of your code to make sure that the next time an Excel file is created it will also be called "Book1" and not "Book2". And might as well close the Excel instance while we are at it!
For more information on the GetObject function, you can have a look at this page
Thanks a lot, DecimalTurn and Patrick Lepelletier!
The GetObject really helped me. The "closing" command worked better like this:
Sub CollectA()
Dim oApp As Application
Dim oWb As Workbook
Set oWb = GetObject("Book1")
Set oApp = oWb.Parent
oWb.ActiveSheet.Range("A1:C32").Copy
Workbooks("Mother-Workbook.xlsm").Worksheets("Sheet1").Range("B6:D37").PasteSpecial Paste:=xlPasteValues
oWb.Close False
oApp.Quit
End Sub
Cheers!
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.
Could someone please tell me why the following VB script works fine if executed from within excel but won't work if it executed using the cmd: cscript c:\vb\test.vbs?. Below is my code I'm trying to get it working using the cmd. I'm using excel .xls (excel 97-2003).
Private Sub CopyData()
Dim x
Dim y
'## Open both workbooks first:
Set x = Workbooks.Open("C:\VB\CopyDataTest.xls")
'Now, copy what you want from x:
Sheets("Sheet1").Range("A:B").Copy
Set y = Workbooks.Open("C:\VB\Destination.xls")
'Now, paste to y worksheet:
y.Sheets("Sheet2").Range("A:B").PasteSpecial
'Close x:
y.Close
End Sub
If you need to run the script from cmd, you need to create an excel object. Try this:
Private Sub CopyData()
Dim x
Dim y
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\VB\Destination.xls", 0, True)
## Open both workbooks first:
Set x = xlApp.Workbooks.Open("C:\VB\Destination.xls")
Now, copy what you want from x:
xlApp.Sheets("Sheet1").Range("A:B").Copy
Set y = xlApp.Workbooks.Open("C:\VB\Destination.xls")
Now, paste to y worksheet:
y.Sheets("Sheet2").Range("A:B").PasteSpecial
Close x:
y.Close
End Sub
CopyData()
Mnimonic already gave an perfectly usable workaround for this, but an explanation could be usefull too.
You have written a piece of code in VBA (Visual Basic for Applications).
You tried to run it as VBS (VB Script)
VB script doesn't know about Office and other libraries already loaded when running the code inside excel.
You'll need to learn how to interact with the COM interfaces from office in VBscript.
Better solution now would be to program in VB.NET and interact with excel inside .NET:
Link: VB.NET and excell
The code will still look very familiar, but it's what Microsoft would like you to do now.
Attention: You will always need to have Excel installed on the PC running the script!
If you want to avoid that, maybe look at something like Aspose to do things without Office installed...
I am using following VBA code in Excel 2010 in a button click event:
Dim objExcel as Object
Set objExcel - New Excel.Application
and used that instance objExcel in subsequent code.
This code is very simple and working in most of the computers I have tested including my computer. Whereas in some computers, that instance is not creating. objExcel is Nothing after the line is executed. So after the line of code is run, objExcel is Nothing. That is why subsequent codes are failing to execute.
I also tried following code:
Set objExcel=CreateObject("Excel.Application")
This also has same problem.
All machines have MS Office 2010 installed.
Problem is not due to large number of Excel instances Problematic computers cannot run the code even once.
Should I check or register any component which may cause the problem?