Run an Excel macro from an Outlook macro? - vba

Greeting all,
Im working on a function where to run excel macro from outlook. my condition is to run the excel macro with the excel file is open and the outlook also open
my problem is. when i run this code. outlook will open another same excel file and ask for replace save.
what i want is, when i call the macro from the outlook. it will trigger the macro straight away from the open excel.
here is my code
Sub macro()
Dim ExApp As Excel.Application
Dim ExWbk As Workbook
Set ExApp = New Excel.Application
Set ExApp = ExApp.Workbooks.Open("C:\Users\Desktop\Production v2.7.1.xlsm")
ExApp.Visible = True
ExApp.Application.Run "'Production'!Main_function_Auto"
ExApp.Close SaveChanges:=True
End Sub

You can try this:
Sub macro()
Dim ExApp As Excel.Application
On Error Resume Next
Set ExApp = GetObject(, "Excel.Application")
If Not ExApp Is Nothing Then
ExApp.Run "'C:\Users\Desktop\Production v2.7.1.xlsm'!Main_function_Auto"
End If
End Sub

Related

VBA Code to Copy and Paste Between Separate Excel Instances

I am trying to copy data from one open instance in excel and load it into a separate open instance. I have the following code but it only copies data from the source workbook since the last save. Also this code can only be run from the destination workbook. Any help would be greatly appreciated.
Sub CollectA()
Dim oApp As Application
Dim oWb As Workbook
Set oWb = GetObject("Test two.xlsm")
Set oApp = oWb.Parent
oWb.Activate
oWb.ActiveSheet.Range("A1").Select
Selection.Copy
Workbooks("Test three.xlsm").Worksheets("Sheet1").Range("B1").PasteSpecial Paste:=xlPasteValues
End Sub
Avoid copy/paste whenever possible:
Sub CollectA()
Dim oWb As Workbook
Set oWb = GetObject("Test two.xlsm")
Workbooks("Test three.xlsm").Worksheets("Sheet1").Range("B1").Value = oWb.ActiveSheet.Range("A1").Value
End Sub
If you want the macro to be in "Test two":
Sub CollectA()
Dim oWb As Workbook
Set oWb = GetObject("Test three.xlsm")
owb.Worksheets("Sheet1").Range("B1").Value = Workbooks("Test two.xlsm").ActiveSheet.Range("A1").Value
End Sub

Excel 2016 crashed when vba use saveas method on new application

I try to use Workbook.SaveAs method to save a new workbook with a specific name. I found it runs fine within the Application which the code run on. But caused excel crash with a new application. What's the problem?
Excel 2016 on Windows 10 Pro.
Codes below:
Sub test()
Dim this_app As Application
Set this_app = Application
Dim new_app As Application
Set new_app = New Application
new_app.Visible = True
Dim wb As Workbook
Set wb = this_app.Workbooks.Add
wb.SaveAs Filename:="new_file_name1"
'above SaveAs succeed
Set wb = new_app.Workbooks.Add
wb.SaveAs Filename:="new_file_name2"
'above SaveAs cause excel crash
End Sub
If I use SaveCopyAs method (code below), both is fine. But I still want to know why SaveAs caused Excel crashed.
Sub test()
Dim this_app As Application
Set this_app = Application
Dim new_app As Application
Set new_app = New Application
new_app.Visible = True
Dim wb As Workbook
Set wb = this_app.Workbooks.Add
wb.SaveCopyAs Filename:="new_file_name1.xlsx"
'above SaveCopyAs succeed
Set wb = new_app.Workbooks.Add
wb.SaveCopyAs Filename:="new_file_name2.xlsx"
'above SaveCopyAs also succeed
End Sub
The following works for me (Excel 2013):
Option Explicit
Sub TestMe()
Dim this_app As Application
Set this_app = Application
Dim new_app As Application
Set new_app = New Application
new_app.Visible = True
Dim wb As Workbook
Set wb = this_app.Workbooks.Add
wb.SaveAs Filename:="new_file_name11"
'above SaveAs sucessed
Set wb = new_app.Workbooks.Add
wb.SaveAs Filename:="new_file_name22"
'above SaveAs cause excel crash
End Sub
I suppose that your problem was in app.Visible or that you already had a file named new_file_name2, thus it crashed. Try changing the files and always add Option Explicit on the top of your code.

Vb2010 Excel Not saving

i currently have a form and a button. when i click the button, it creates an excel spread sheet and adds "hi" to range("A1"). The problem is that i am trying to save and close the file but it still prompts me "If i would like to save", is there s way i can get rid of this message? is it not saving for some reason?
i am using the code:
xlWorkbook.save()
Excelapp.quit()
i have also tried using the code below but it still doesn't work:
me.excelapp.displayalerts=false
for some reason it does not want to be saved
Imports Excel = Microsoft.Office.Interop.Excel
Dim ExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim WB As Microsoft.Office.Interop.Excel.Workbook
Dim WS As Microsoft.Office.Interop.Excel.Worksheet
sub button1() WB = ExcelApp.Workbooks.Open("Path_To_File")
WS = WB.Worksheets("Sheet1")
'code to change some values here and there
WB.Save()
ExcelApp.Quit() end sub
The SaveAs works fine for me...
WB.SaveAs("filename.xls", True)

How to switch to open Excel spreadsheet and work on it from Word with VBA

I'm just trying to work on an open Excel spreadsheet from Word using VBA. I did a search on this and found the following code on How to get reference to an open Excel spreadsheet from Word?. The problem is, it seems to open a separate instance of Excel rather than the one I already have open, and then closes it after the action. How can I get the procedure to switch to the open Excel spreadsheet, perform the desired actions, and leave the spreadsheet open?
Sub DoStuffWithExcelInWord()
Dim xl As Excel.Application
Dim wkbk As Excel.Workbook
Dim wk As Excel.Worksheet
Set xl = CreateObject("Excel.Application")
Set wkbk = xl.Workbooks.Open("C:\test.csv")
Set wk = wkbk.Sheets(1)
Debug.Print wk.Cells(1, 1).Value 'Here's where I would like to insert my code
xl.Quit
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
Thanks for any assistance!
Thanks again to dcromley for his earlier response. In the meantime, here's what I came up after a couple more searches on the Internet. It takes care of the situation where the Excel document is already open. Hopefully that will help others with similar situations, although it's not exhaustive (for example, if several Excel documents are open).
Sub DoStuffWithExcelInWordRevised()
Dim xl As Excel.Application
Dim wkbk As Excel.Workbook
Dim wk As Excel.Worksheet
On Error Resume Next
Set xl = GetObject(, "Excel.Application")
On Error GoTo 0
If xl Is Nothing Then
Set xl = CreateObject("Excel.Application")
Set wkbk = xl.Workbooks.Open("C:\test.xlsx")
Set wk = wkbk.Sheets(1)
End If
xl.Visible = True
With xl
' Insert Excel code here
End With
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
Try:
Add xl.Visible = True
Del xl.Quit
It appears that you are trying to do this with a Workbook that is already open. Since you want to use the open workbook / application then you need to:
Set xl = GetObject(,"Excel.Application")

Open excel sheet from form

I have made a form with several command buttons which open specifics worksheets. The problem is when I open an excel file from the command button, if the form is not hidden then I'm not able to click on the opened file(Its not activated)
Even if I hide the form, I need to manually goto that file from taskbar it doesn't get activated.
The problem is:
I don't want my form to be hidden because I want the user to be able to open multiple sheets
The opened sheet doesn't get activated.
Here's my code:
Private Sub CommandButton1_Click()
Dim Wb As Excel.Workbook
Set Wb = Workbooks.Open(Filename:="D:/power system design/foo.xlsx", ReadOnly:=False)
UserForm1.Hide
Wb.Activate
Wb.Sheets("Sheet1").Cells(1, 1).Select
End Sub
This is a quick way to accomplish what you said you wanted done. It may not be the best way, but you should be able to drop it in and run with it:
Private Sub CommandButton1_Click()
Dim xls As Excel.Application
set xls = new Excel.Application
xls.Workbooks.Open "D:/power system design/foo.xlsx", ,False
xls.Visible = true
End Sub
or if you want to work with the opened workbook
Private Sub CommandButton1_Click()
Dim xls As Excel.Application
Dim wb as Excel.Workbook
set xls = new Excel.Application
set wb = xls.Workbooks.Open(Filename:="D:/power system design/foo.xlsx", ReadOnly:=False)
xls.Visible = true
End Sub
This will result in the sheet being opened in a new Excel application window.