473,395 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Saving excel with VB

18
Hi,

I have a java code that populates data on my XLSM file, the problem is, the java code can't handle the execution of Macro, so what I'm just doing is to populate the excel file with java and then open the excel and save it manually and then the macro will be invoked.

I wanted to do it automatically, so I created a VB script that will open the file and will save it as well but the problem is, even though it saves and closes the file, it seems that it didn't invoked the Macro.

@edit:

The macro is executed the moment I hit save (Ctrl + S) or when I click the close button and save.

Any ideas on how can I do this? I'm just new with Visual Basic.

Expand|Select|Wrap|Line Numbers
  1. Set objExcel = CreateObject("Excel.Application")
  2.     objExcel.Visible = True
  3. Set objWorkbook = objExcel.Workbooks.Open("C:\Users\USER\Desktop\Sample.xlsm")
  4.     objWorkbook.Save
  5.     objExcel.Quit
  6.  
Thanks.
Jul 5 '11 #1

✓ answered by Guido Geurs

This will open Excel, run the macro, save the file and close the workbook.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_Open()
  2.    Call macro1
  3. End Sub
Expand|Select|Wrap|Line Numbers
  1. Sub macro1()
  2. Dim MIN As Integer
  3. Dim SEC As Integer
  4.    On Error GoTo EXITMACRO '§ press Esc to exit macro and let workbook open
  5. '§ Enter here your code:
  6.  
  7.  
  8.  
  9.    With ThisWorkbook
  10.       .Save
  11.       .Close
  12.    End With
  13. Exit Sub
  14. EXITMACRO:
  15. End Sub
Because this is a macro who runs on opening the file and closes immediately after, it's not possible to see the file !
The only way to let the file open and see the results is to press Esc on opening and let the macro generate an error=> exit macro.
Attached is an example with a code to fill the time in the corresponding cell(just for testing).

12 4526
Guido Geurs
767 Expert 512MB
To start a macro at the opening of an excelfile:
put this in the workbook code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_Open()
  2.    Call macro1
  3. End Sub
When this is the macro:
Expand|Select|Wrap|Line Numbers
  1. Sub macro1()
  2. Worksheets("sheet3").Activate
  3. Range("D10").Activate
  4. End Sub
The file will opening at sheet3 and range "D10" will be selected.
Jul 5 '11 #2
irGed
18
Hi,

Thanks for the reply but after reading your reply and knowing that invoking a Macro is by means of "Call" method, I have searched all the "Microsoft Excel Objects/Worksheet codes" for a word "Call" and found nothing. I don't know now if the Macro or just the VBA method is the one responsible for filling out some of the columns on my excel sheet. if it's just the VBA method, is there a method or keyword that reacts when you're pressing the "Ctrl + S" or "Save and Exit" button?


@edit: I have found a method on the code "Workbook_beforeSave()", I copied it and changed it to "Workbook_Open()". and now, everytime I open the document, it runs the Macro/Method that fills some of the columns. And now I'm trying to convert it into a Macro so that I will just call on the Macro to do the filling of some columns.

I think for my next task, I think I need to append/add a certain function(like copying the content of Workbook_beforeSave() and create Workbook_Open() with the same function on an excel file so that it will still behave the same way like my previous excel. Is this attainable through VB Script?


Thanks.
Jul 5 '11 #3
Guido Geurs
767 Expert 512MB
This will open Excel, run the macro, save the file and close the workbook.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_Open()
  2.    Call macro1
  3. End Sub
Expand|Select|Wrap|Line Numbers
  1. Sub macro1()
  2. Dim MIN As Integer
  3. Dim SEC As Integer
  4.    On Error GoTo EXITMACRO '§ press Esc to exit macro and let workbook open
  5. '§ Enter here your code:
  6.  
  7.  
  8.  
  9.    With ThisWorkbook
  10.       .Save
  11.       .Close
  12.    End With
  13. Exit Sub
  14. EXITMACRO:
  15. End Sub
Because this is a macro who runs on opening the file and closes immediately after, it's not possible to see the file !
The only way to let the file open and see the results is to press Esc on opening and let the macro generate an error=> exit macro.
Attached is an example with a code to fill the time in the corresponding cell(just for testing).
Attached Files
File Type: zip Saving excel with VB_1.0.zip (7.9 KB, 147 views)
Jul 7 '11 #4
irGed
18
Hi,


Thanks for the reply and all the help. And now, I have a separate question, should I start a new topic again or not?

The scenario is, I have a new excel now, but the difference with this excel file is I am not the one who generated/created this file, it comes from another source. And I want to perform a macro on it every time I ran a VB Script on it. If I just simply "hard-code" the macro on the VBA Excel, it wouldn't become a "practical solution" because this excel file is always changing, but the method of macro isn't changing.

The question is, what is the best approach for this one?

1. Should I VB Script that will create a Macro and will be stored on the Excel file so that my VB Script will simply invoke it.

2. Create a VB Script that behaves like the Macro and simply invoke that VB Script?

And how can I achieve this or where I should start first?

Thanks.
Jul 8 '11 #5
Guido Geurs
767 Expert 512MB
It's not neccesary to start a new call because it's still about the problem of starting a macro in Excel from a VBscript.

If I understand the question: it's starting a macro via a script and each time in a different Excel file.

I have to think about it and do some tests because this is also new for me.
Jul 8 '11 #6
irGed
18
Hi,

Thanks for the prompt reply. Yes, you are correct about the understanding of the question.
Jul 8 '11 #7
Guido Geurs
767 Expert 512MB
I think it's possible to start the macro with the two methods:
- write the macro in VBS and send all commands to the new sheet.
- write the macro in Excel and call the macro in the new sheet with VBS.

The last is the best way because it's essayer to test the macro when it's created in Excel.

But there is a 3th option: write an application in VB6 and call the macro in the new sheet!
This is a flexible option because it's always a new file and with VBS you have to rewrite the script each time with the new filename.
In the VB6 application you have only click the file and start the macro.

Are there different macro's ?
Because its also possible in VB6 to change to the macro you want!

I have attached an example of such an application.
It's still possible to adapt it to any needs in selections, commands, ...
Attached Files
File Type: zip Saving excel with VB_1.3.zip (38.4 KB, 133 views)
Jul 13 '11 #8
irGed
18
Hi,

Thanks for all the help, although I paused for finding a solution for this problem because I got a task that has higher priority to finish. I think I would go for the second method, correct me if I'm wrong. First, I will write the macro on a separate excel ExcelB(an excel file that doesnt change no matter what), then since my main excel ExcelA file is always changing, I will invoke the macro on ExcelB that will affect ExcelA. Is that correct?

Thanks.
Jul 14 '11 #9
Guido Geurs
767 Expert 512MB
Yes, it's also the method I have used in VB6.
This is the code for VB6.
Maybe you can use some of the commands in VBS:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Dim objexcel As Excel.Application
  3. Dim FILEidx As Integer
  4.    For FILEidx = 0 To File1.ListCount - 1
  5.       If File1.Selected(FILEidx) Then
  6.          Set objexcel = CreateObject("Excel.Application")
  7.          With objexcel
  8.             .Visible = True
  9.             With .Workbooks
  10.    On Error GoTo Error_File
  11.                .Open App.Path & "\Saving excel with VB_MACROS_1.0.xls", , True
  12.                .Open File1.Path & "\" & File1.List(FILEidx)
  13.             End With
  14.             .Run ("'" & App.Path & "\Saving excel with VB_MACROS_1.0.xls'!macro_test")
  15.             .SaveWorkspace File1.Path & "\" & Left(File1.List(FILEidx), InStrRev(File1.List(FILEidx), ".") - 1) & "_calc_" & ".xls"
  16.             DoEvents
  17.             .Quit
  18.          End With
  19.          Exit Sub
  20.       End If
  21.    Next
  22.    MsgBox "Select a file"
  23. Exit Sub
  24. Error_File:
  25.    MsgBox "Error opening file"
  26.    objexcel.Quit
  27. End Sub
Jul 14 '11 #10
irGed
18
Hi,

I have a few codes here that I've tried but all of them failed.

Expand|Select|Wrap|Line Numbers
  1. Set objExcel1 = CreateObject("Excel.Application")
  2. Set objWorkbook1 = objExcel1.Workbooks.Open("C:\Validation.xlsm")
  3. Set objExcel2 = CreateObject("Excel.Application")
  4. Set objWorkbook2 = objExcel2.Workbooks.Open("C:\Original.xlsm")
  5.     objExcel1.Application.Run ("ValidateExcel")
  6.     objWorkbook2.Save
  7.     objExcel1.Quit
  8.     objExcel2.Quit
  9. Set    objExcel1 = Nothing
  10. Set    objExcel2 = Nothing
  11.  
  12.  
**Invokes the ValidateExcel Macro that is stored on Validation.xlsm but doesnt affects Original.xlsm plus it gets runtime error when the macro is executed
because the macro is not meant to be executed on that workbook.


Expand|Select|Wrap|Line Numbers
  1.  Set objExcel1 = CreateObject("Excel.Application")
  2. Set objWorkbook1 = objExcel1.Workbooks.Open("C:\Validation.xlsm")
  3. Set objExcel2 = CreateObject("Excel.Application")
  4. Set objWorkbook2 = objExcel2.Workbooks.Open("C:\Original.xlsm")
  5.     objExcel2.Application.Run ("ValidateExcel")
  6. On Error Resume Next
  7.     objWorkbook1.Save
  8.     objWorkbook2.Save
  9.     objExcel1.Quit
  10.     objExcel2.Quit
  11. Set    objExcel1 = Nothing
  12. Set    objExcel2 = Nothing
  13.  
**Macro failed to execute because there's no ValidateExcel.

Expand|Select|Wrap|Line Numbers
  1. Set objExcel1 = CreateObject("Excel.Application")
  2. Set objWorkbook1 = objExcel1.Workbooks.Open("C:\Validation.xlsm")
  3. Set objExcel2 = CreateObject("Excel.Application")
  4. Set objWorkbook2 = objExcel2.Workbooks.Open("C:\Original.xlsm")
  5.     objExcel2.Application.Run ("C:\Validation.xlsm!ValidateExcel")
  6. On Error Resume Next
  7.     objWorkbook1.Save
  8.     objWorkbook2.Save
  9.     objExcel1.Quit
  10.     objExcel2.Quit
  11. Set    objExcel1 = Nothing
  12. Set    objExcel2 = Nothing
  13.  
Got an error that Validation.xlsm is locked for editting, and then the Macro failed to execute because there's no ValidateExcel.

What am I missing? I have also tried to set them into Visible, but still, it failed.

I tried the procedure manually, opening the Original.xlsm and the Validation.xlsm and invoke the ValidateExcel Macro from Original.xlsm and it performed the macro procedure.

Thanks.
Jul 15 '11 #11
irGed
18
Hi,


I have already solved the invoke macro from another excel sheet. Thanks for the help.

For future reference, this is the code:

Expand|Select|Wrap|Line Numbers
  1. Set objExcel1 = CreateObject("Excel.Application")
  2. Set objWorkbook1 = objExcel1.Workbooks.Open("C:\Validation.xlsm")
  3. Set objWorkbook2 = objExcel1.Workbooks.Open("C:\Original.xlsm")
  4.     objExcel1.Run ("Validation.xlsm!ValidateExcel")
  5.     objWorkbook1.Save
  6.     objWorkbook2.Save
  7.     objExcel1.Quit
  8. Set objExcel1 = Nothing
  9.  

Thanks.
Jul 16 '11 #12
Guido Geurs
767 Expert 512MB
For me (XP SP3 and Office 2003) it's working with:
Expand|Select|Wrap|Line Numbers
  1. Set objExcel1 = CreateObject("Excel.Application") 
  2. Set objWorkbook1 = objExcel1.Workbooks.Open("D:\EXCEL Saving excel with VB\Validation.xls") 
  3. Set objWorkbook2 = objExcel1.Workbooks.Open("D:\EXCEL Saving excel with VB\Original.xls") 
  4. rem    objExcel1.Visible=True '§ can be deleted or set REM
  5.     objWorkbook1.Activate
  6.         objExcel1.Application.Run ("Original.xls!ValidateExcel") 
  7.         objWorkbook1.Save 
  8.         objWorkbook2.Saved=True '§ no need to save => no msgbox for SaveAs
  9.         objExcel1.Quit
  10.  
Jul 16 '11 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Vikram | last post by:
Hi, Given below is my task. An user can open the xls file from my website (loaded on the top frame). After filling the Excel, he can click a send button at the bottom frame. By clicking the...
1
by: Arjun Diwan | last post by:
hi! Could anyone please help me out as to how we can save the excel workbook in the database and read it back anytime we want. I was able to convert the text files and image files into binary...
0
by: dd | last post by:
I am kind of puzzled with this problem. I am trying to open an xls file from C# and then save it as text or comma delimited. Here is the code: Excel.ApplicationClass oExcelApp = new...
2
by: Amy B | last post by:
Hi, I've been trying to figure out how to fix this for several days but nothing I have done so far seems to work. I have posted this message in a Google Excel Programming group, but did not...
1
by: Mark | last post by:
Hello - I'm opening/saving an Excel workbook from ASP.Net. When I try to save the workbook I get the error: System.Runtime.InteropServices.COMException: The remote procedure call failed. ...
0
by: Kewal | last post by:
Hi All, I have a requirement of saving Excel files as HTML using C#. I have managed to write code for saving as HTLM file. however this seems to be not working in the once perticular case . ...
2
by: DPK | last post by:
Hello friends!!! I made a prog. to make the excel. It is running well but when it saves the excel file it open the save dialog box to save the file becasue I am using SAVE function here instead of...
0
by: Speilman_54 | last post by:
Hi, I'm converting an excel Macro into visual basic 2005 express, as I don't have a copy of VB 6 and trying to make and executable from it, I know this version doesn't have the save file as .exe,...
1
by: johndoyle | last post by:
I've seen a few posts on other sites discussing how to save an excel file, word file, or other office doc as a pdf document. In general, seems the solutions have been pretty complicated or...
3
by: RobT | last post by:
Ok, here's what I've got. I have a form that exports to an Excel worksheet (sheet1) and it works great, no issues there or anything. It's not a template, just a regular .xlsx spreadsheet, but I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.