How to open a file in an active workbook? VB.NET 2008 - vb.net

I have a program that filters data and then outputs it to a tabg delimited file. I then use Excel Interop to open this file in Excel because directly outputting to a worksheet takes too long. So currently I am using this code:
AD.DF.WriteToFile(vbTab)
Dim WB As Workbook = ExcelApp.Workbooks.Open(AD.DF.DatafileInfo.WriteToFileLocation)
ExcelApp.Visible = True
The first line takes the filtered data and outputs to a tab delimited file. The second opens that same file in a new workbook in Excel. And obviously the third makes Excel visible. Here is my problem though: right now when this code is run there are two open workbooks. I already have an active workbook and I would just like to open this file to that workbook.
If this is possible, how do I do it?
Thank you.

Look at the GetObject function.
Dim MyXL As Object
MyXL = GetObject(, "Excel.Application")
should get you a reference to the currently running instance of Excel.

In the code I created an object that is an Excel Workbook. I then set the created workbook as the ExcelApp.ActiveWorkbook. Then I was able to open the file without another workbook being created.

Related

Change linked excel file via VBA in PowerPoint

I have multiple slides with multiple charts having sample data. I want to update these charts with actual data which is present in excel files located in the same folder. Can I do this via VBA inside ppt and how?
I read this answer edit chart data in powerpoint but this does not specify where I can mention the excel file and select the specific data.
Please help!
On the question you linked to, the top answer provided the following code:
With ActivePresentation.Slides(sl).Shapes(sh).Chart.ChartData
.Activate
.Workbook.Sheets(1).Range("A1").Value = "test_data"
.Workbook.Close
End With
One of the comments on the answer pointed out that once you Activate the ChartData, you're basically dealing with Excel. You can also launch an actual instance of Excel inside the Powerpoint VBA, then just open your workbooks, extract the data from them and put it into your ChartData objects like you would do if you were working in VBA in excel.
In order to launch an instance of Excel in powerpoint VBA try the following:
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open "C:\lol\Book1.xlsx", True, False
'once Book1 is open you can extract data from Book1 and move it to
'the appropriate place in .ChartData.Workbook
Set xlApp = Nothing
(code above sourced from this answer)

VBA - Copying cells across Workbooks

I'm writing a VBA program that changes the visuals of an excel database. At the end, I need to add a "header" (5 rows of text) at the top. The problem is, I cannot insert the text with VBA, since it contains letters (for ex. á, é...) that aren't compatible with VBA, I need to insert that text from another excel file.
The macro I have is stored in a standalone excel workbook, that also contains the cells of the header I need to copy into my database. The problem is, the name of the excel files I am working with varies. Is there a way I could switch between those 2 files and copy cells across them. Can I store the name of the excel file I am working with and later use it in the VBA code to switch between the workbooks?
Not sure if this 100% answers your question but hope it helps, you can open and store both workbooks as objects using:
Dim wb as Workbook, wb2 as Workbook
Set wb = Workbooks.Open("C:\User\Sample_Workbook_File_Path_1.xlsx")
Set wb2 = Workbooks.Open("C:\User\Sample_Workbook_File_Path_2.xlsx")
From there you can call values from either workbook using things like:
'to get the second workbooks excel file name into a worksheet: "Sample_Workbook_2"
wb.Worksheets("Sample_Worksheet").Range("A1").Value = wb2.Name
'to copy files
wb2.Worksheets("Second_Workbooks_Worksheet").Range("A2:A100").Copy _
wb.Worksheets("Sample_Worksheet").Range("A2")
'Alternatively you can store the entire workbooks path name instead of the file name using:
wb.Worksheets("Sample_Worksheet").Range("A1").Value = wb2.Path

Open and update an existing workbook with 2 worksheets

This may be simplistic to most of you. I just started using VBA to create and update excel workbooks. I found some code on the internet to open and update an existing workbook and worksheet. Like I said, I am brand new at this. Does this code even make sense? I just need to know how to open an existing workbook and all the examples I have found aren't working in our environment.Thanks for any help I can get
Dim wbSource, xlApp, srcWorksheet
'initialize
Set xlApp = CreateObject("Excel.Application")
'open source and target files
Set wbSource = lApp.Workbooks.Open("X:\GCIXCycleCompare_test_auto.xlsx")
set srcWorksheet = wbSource.Worksheets("NewCycle")
srcWorksheet.sheets("NewCycle").Activate
srcWorksheet.Rows("1:1").Delete
If you are in Excel VBA, this isn't quite what you want. This code was written for an external app (say written in VB6) to open Excel remotely and then do stuff to that copy of Excel. If you are already in Excel/VBA you obviously don't need to do that.
In VBA, the equivalent code would be something like this:
Public Sub MyCode()
Dim wb as Workbook
Dim ws as Worksheet
Set wb = Application.Workbooks.Open("X:\GCIXCycleCompare_test_auto.xlsx")
Set ws = wb.Worksheets("NewCycle")
ws.Rows(1).Delete
End Sub
If you run this code (by click F5 from inside VBA ... or by run macro in Excel) it should open up the test file off the X: drive, and then delete the first row of it.

Weird VBA (Bug?)- Copy Paste Loop Crashes, unless Messagebox is added

I have got a weird problem with VBA. I have programmed a sub that is supposed to create a Word report. I basically have e.g. 10 variablles, a word template document with template charts, and for each variable three CSV files with data. I loop over all variables, and for each variable I create page, access the chartdata workbook, and copy the external CSV data into the chartdata workbook.
Sub createRep()
'open Word-template
for page=1 to 10
'Open external csv
'Open chartdata workbook
'Copy external data into chartdata workbook
'Close external csv files and chartdata workbook
Next page
End Sub
It will work in the beginning, but at a certain page I will get an error because the pasting does not work. I cannot copy anything into the chartdata workbook manually either, it seems like it crashed and that is why the pasting failed. However, the following code will work:
Sub createRep()
'open Word-template
for page=1 to 10
MsgBox page
'Open external csv
'Open chartdata workbook
'Copy external data into chartdata workbook
'Close external csv files and chartdata workbook
Next page
End Sub
That is the message box fixes it somehow. But why?
This is probably due to your code running ahead of Word opening. While the document tries to load, you are already accessing it before the application is ready.
Do fixes to this you could try are just adding DoEvents or if this is not enough, using your reference to the WordApplication do:
While WordApplication.Busy = True
DoEvents
Wend
Hope this helps
-JDB

Vba closing excel applications/workbooks freezes

I have a problem with the following code:
Dim excelapp as object
set excelapp = CreateObject("excel.application")
dim ws as object
dim wb as Workbook
wb= excelapp.Workbooks.Open(path)
ws= wb.Sheets(1)
'in the code i send the worksheet object around by reference in order to read the
'worksheet and manipulate data, i dont create other instances of excel apps or
'workbooks
then i try :
wb.Close
and i have also tried :
excelapp.Quit
Neither have worked, they both freeze and say they are waiting on OLE actions, and i have multiple excel processes opening if i do not call these, when i try to open the excel files i had opened via code, i can only open them as read-only because theyre checked out to me.
I also tried executing a shell script that closes all applications "Excel.Exe" but it closes...the actual excel file where the vba is being executed, so thats not a good solution.
Thank you in advance.
It might be that the Excel app has detected that the workbook has changed and is putting up a dialog box (which is invisible because the app is not visible). Try:
wb.Close False
Which tells Excel to ignore any changes to the workbook.