Vb2010 Excel Not saving - vb.net

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)

Related

Opening workbook from file and updating link in original workbook

I'm trying to write a macro in VBA, that will open another Workbook using a PathFile specified in a cell (this works), updates link in workbook in which macro is used (doesn't work) and closes the PathFile workbook (works)
This is a code:
Sub UpdateRaw()
Dim CurrWb As Workbook
Dim FilePath As String
Dim book As Excel.Workbook
Set CurrWb = ActiveWorkbook
FilePath = Range("I1").Value
Dim app As New Excel.Application
app.Visible = True 'so we can see whether correct file is being opened
Set book = app.Workbooks.Open(FilePath)
CurrWb.Activate
Worksheets("Raw_vs_Actual").EnableCalculation = False
Worksheets("Raw_vs_Actual").EnableCalculation = True
book.Close SaveChanges:=False
app.Quit
Set app = Nothing
End Sub
Going step by step I found that command CurrWb.Activate doesn't take me back to my original Workfile. My suspicion is that by opening new Excel Application I can't get back to the CurrWb (ActiveWorkbook). Is there a workaround? I need this so my INDIRECT function doesn't return #REF.
I'm using Excel 2010 in case it's important.
I think Set book = app.Workbooks.Open(FilePath) shall be enough, but if not refresh the workbook:
book.RefreshAll
for opened workbook. For the workbook that contains the macro, use
ThisWorkbook.RefreshAll

Referencing Excel from a Word Macro when Excel and the file are already open?

I'm sorry but after three days I Give Up... So I'm asking what should be a very simple question but every example I find opens an Excel file or opens a read Only version of the same file, to do something where as in my case the file has already been opened from another macro using the code below, I just can't seem to figure how to adapt it for a file already open??
I'm also trying to figure out how to move excel to the active window from within the code? I would really appreciate any help
Dim oApp As Object
Dim x As Variant
Dim sPath As String
Dim oExcel As Excel.Application
Dim oWB As Workbook
Dim oSheet As String
sPath = "E:\Special Folders\WWWRoot\temp.xlsx"
oSheet = "--Keywording--"
On Error Resume Next
Set oExcel = New Excel.Application
Set oWB = oExcel.Workbooks.Open(sPath)
oExcel.Visible = True
Sheets(oSheet).Select
Range("A1:G1000").Clear
Range("A1").Select
Sheets(oSheet).Cells(1, 1).Select
Sheets(oSheet).PasteSpecial (xlPasteAll)
Range("A1").Select
Sorry, but can't you just turn on the Macro Recorder and get what you want?
Sub TryThis()
Windows("SecondaryWorkbook.xlsb").Activate
Range("A1").Select
Windows("PrimaryWorkbook.xls").Activate
Range("A1").Select
End Sub
I believe you need to be in the same instance of Excel, or this wonr't work. AFAIK, different instances of Excel don't communicate with each other...at all.

Run an Excel macro from an Outlook macro?

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

Excel VBA - Cannot Close New Workbook created using Workbook.Add

I have the following code that creates a new workbook and populates it with some data.
Dim wb as Workbook
Set wb = Workbooks.Add
wb.Range("A1") = "Dummy Text" 'Some code that populates data
wb.Activate 'wb.Activate or ThisWorkbook.Activate has no effect
The new workbook opens fine. I am able to minimise it, use the menus, etc. but am unable to click the close button.
If I go back to the original workbook that has the macro and come back to the new workbook, I can close.
How do I overcome this problem? Out of ideas unfortunately.
My desidred use case
Step 1 : User clicks a button -> A new workbook opens up with data
Step 2 : User reads the information in the new workbook and decides to save / close
But the close button in the new workbook won't work until the user goes back to the original workbook and comes again to the new workbook.
after your code
Dim wb as Workbook
Set wb = Workbooks.Add
wb.Range("A1") = "Dummy Text"
insert
set wb = nothing
this will release your new workbook and you can close using the "X" in the top right.
P.S.: Sorry my english
Sub Button1_Click()
Dim wb As Workbook
Set wb = Workbooks.Add
wb.Sheets("Sheet1").Range("A1") = "Dummy Text" 'Some code that populates data
wb.Activate 'wb.Activate or ThisWorkbook.Activate has no effect
End Sub
That worked fine on my excel. I click the close button and it has asked me if i want to save or not.
I think you need to add this line:
Workbooks("source").Close
if you want to close the current workbook , try this :
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ActiveCell.Value = "CLOSE" Then
ThisWorkbook.Saved = True
ThisWorkbook.Close
End If
End Sub
When displaying your userform, make sure you use ".Show 0".

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.