I have an SSIS package that creates and populates an excel sheet, it creates the sheet from a template file.
One of the issue's I had was that excel would change the format of the rows. I did a work around of inserting a header row and hiding it.
However I now need to script a VB task in SSIS that opens the excel sheet and deletes that specific row.
I've seen some articles online however have not been able to find any code I can try and replicate and my VB knowledge is very limited and I am really struggling.
SO far i've figured out how to delete the row.
Sub DeleteRow1()
Rows(4).Delete
End Sub
However i need to assign file strings and get the file to open and close as well...
after some discussions with a programmer here i managed to get some script to do exactly what I needed. Posting as an answer for anyone else out there who ever needs to do this in SSIS.
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
xlApp = New Excel.Application
xlApp.Workbooks.Open(Dts.Variables("NewFileName").Value.ToString)
xlSheet = xlApp.Workbooks(1).ActiveSheet
xlSheet.Rows(4).Delete()
xlApp.Workbooks(1).Save()
xlApp.Workbooks(1).Close()
xlSheet = Nothing
'
Dts.TaskResult = ScriptResults.Success
Uses a variable in SSIS to get the file name. This works perfectly although you do need to add Microsoft.Office.Interop.Excel Reference.
Sub deleteRow1()
Dim wkbk As Workbook
'You can also use an input box like this
' Dim wkbkname As Variant
' wkbkname = InputBox("Enter Workbook Name here.")
' Use wkbkname instead of wkbk then
Set wkbk = "Filename"
wkbk.Sheets(1).Rows(4).delete
End Sub
I hope that's what you were looking for.
Related
I am entering each Barcode data to an Excel File. But I want to keep the excel file minimized and data coming from Barcode reader automatically enter in that minimized Excel sheet. Is that possible using VBA Code or using any other method? I have tried some methods but didn't get what I need. So can anyone help me? Thanks in advance.
I have tried this code to set the file Active.
Sub test()
Dim wbName As Window
Set wbName = ActiveWorkbook.Windows(1)
'You can use Windows("[Workbook Name]") as well
wbName.Visible = False
wbName.Visible = True
End Sub
But this is not I need. I need automatically enter barcode details to Excel file when it is minimized.
Don't use 'ActiveWorkbook', but refer to the workbook with its actual name.
Something like
sub test()
dim wb as workbook
set wb = Workbooks("Book1.xslx")
'insert code
end sub
difficult to explain further as we don't have any reference on what you've tried so far.
I am using Visual basic from excel to open a different workbook and pass some things from one to another. I have the second file opening fine however I cant get the second file to read the things I need.
Sub createExcel()
Cells(1, 1).Text = "va02"
Dim objExcel
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open(filepath)
End Sub
this is my code to open the second application. I have 2 variables in 2 cells in the first workbook. How would one go about getting the variables. the problem i have encountered is; the second workbook can be called from any file, in any folder.
i would try
variable = Workbooks(firstWorkbook.xlsm).worksheets(sheet1)...
however the worksheets dont have the same names either. please help
thanks
Your code is in Workbook1, and opens Workbook2. So it's Workbook1 that does everything - so " I cant get the second file to read the things I need" is not meaningful.
Sub Demo(filepath as string)
ThisWorkbook.Worksheets(1).cells(1,1).text = "This is written by initial Workook"
Dim Wb as Workbook
Set Wb = Workbooks.Open(filepath)
Wb.Sheets(1).cells(1,2) = "So is this, even though it's in a different workbook"
End Sub
I want to create a marco which enables me to add something in an excel sheet while working in Word. I managed to get something working which opens an excel file from word:
Dim excelApp As Excel.Application
Dim openExcel As Workbook
Dim var1 As Integer
Set excelApp = New Excel.Application
Set openExcel = excelApp.Workbooks.Open("C:\Documents and Settings\aa471714\Desktop\Book1.xls")
excelApp.Visible = True
But now I want to add the code which should happen in excel right after. But when I add the relevant code:
Sheets("Sheet2").Select
Range("A4").Select
This does not seem to work. Am I overlooking something?
Dear regards,
Marc
Keep in mind that in macro created in Word default application is Word Application all the time. Therefore each time you want to make any operation in Excel you need to state it by adding full references to Excel Application (or other Excel object as presented below).
Keep also in mind that you will need to add extended object hierarchy in such situation.
Back to your code- adding this kind of object references should solve the problem:
(incl. some additional re-editions)
'comments referring to Object hierarchy
openExcel.Sheets("Sheet2").Select 'OK because sheet is an object below Workbook
excelApp.Range("A4").Select 'NEW, Range is object below Application or...
openExcel.Sheets("Sheet2").Range("A4").Select 'IMPROVED, Range is object below sheet
'general- Range.Select works only for activesheet!!
Object openExcel keeps reference to Excel application which is represented by excelApp.
I'm new to VBA, so I'm struggling with this for a couple of days now.
I have a combobox in Word with contacts, I also have an excel file(contacts.xls) with one column (names of the all the contacts). When I open the Word-document the Macro has to fill in the combobox with all the names from the excel file.
Is it possible to send me a working example of this for word 2007? Seems quite simple, but just can't get this to work...
Thanks in advance!
If you intend on reading from an Excel file in Word you are going to have to open it. It will use code like this:
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
'Start a new workbook in Excel
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open("FileName.xlsx")
You will also probably want to go to Tools->References in the VB project and find whatever Excel library is on your machine (and check it of course). There are ways around this if needed, but it is easier.
You then can read the values from the workbook:
oBook.Worksheets(1).cells(1,1)
I'm not totally sure what the syntax to get it into the combo box is. Look up "combobox object members" in the vbe in word. There will be a list property, or something like that.
Sorry, I'm on a linux machine right now, so I can't debug exact code for you.
I have more full code for you now:
Option Explicit
Sub TestDropDownFromExcel()
Dim counter As Long
Dim xlApp As Excel.Application
Dim xlBook As Workbook
Dim oCC As ContentControl
Set oCC = ActiveDocument.ContentControls(1)
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Path\To\MyFile.xlsx")
If xlBook Is Nothing Then
Exit Sub
End If
oCC.DropdownListEntries.Clear
For counter = 1 To 10
oCC.DropdownListEntries.Add Text:=xlBook.Worksheets(1).Cells(counter, 1), Value:=CStr(counter)
Next counter
xlApp.Visible = True
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Be aware that there is very little error checking going on here. Also, rather than the call to .Visible at the end, you can simply call .Close if you do not want the user to see it (for the final project, this is probably preferable. The deferencing (= Nothing) is good practice to have, but VBA cleans up automatically at the end of execution. Obviously I am assuming tyou want the first dropdown (ContentCOntrols(1)) but this may not be true.
I have some VBA code that copies stuff from Excel and pastes it into Word. The problem I'm having is how to open the spreadsheet. I can open it using an absolute path reference by
Workbooks.Open "C:\path\filename.xls"
I would prefer to reference the spreadsheet using a relative path reference. I was able to find code for relative path references from an Excel workbook to another one but it doesn't seem to work if you're doing it from Word.
Add a reference to Excel object library, then create an object in code and use that object to control an instance of Excel. Just make sure to avoid things like ActiveWorkbook, just in case.
After adding the reference:
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
xl.Quit
Set wk = Nothing
Set wkbk = Nothing
Set xl = Nothing
End Sub
You can create something very similar using Excel to automate Word too, if that's more of what you're looking for.