Really this is curious. Let me try to explain what is going on. I wrote a macro in word that is calling a macro of an excelfile
Set excel = CreateObject("excel.application")
excel.Visible = True
excel.workbooks.Open FileName:=tablePath
IDcolumn = excel.Application.Run("main", generatedExelPath)
the "main" function being called from the word macro is "simply" generating a new excel-file by copying specific data from the called excel file.
So we got 1 word and 2 excel documents. some sheets of excel1 have to be copied to excel2.
therefore I use:
For i = 0 To UBound(copySheets)
Windows(srcWinName).Activate
Sheets(copySheets(i)).Activate
Set AcSh = ActiveSheet
Windows(destWinName).Activate
If copySheets(i) = "config" Then
AcSh.Copy Before:=Sheets(1)
Else
AcSh.Copy After:=ActiveSheet
End If
Next i
where copySheets contains the sheets that needs to be copied. But the copy commands throws an error. Something like "can't copy from source to destination cause the destination has less rows and columns than the source file"
Atm excel1 is an xlsm
as it was an xls
file this error didn't occur.
I wonder if this line, which adds the excel2 is the prob
Workbooks.Add Template:="Workbook"
maybe i should use another template but I don't know which one.
If i create an empty xlsm
file, save it and then use open this empty xlsm
instead of
the command before the error doesn't occur. Hope someone can help me.
Ahh and one thing I should mention is: if I run the excel macro as above from the word macro the error occurs but when I close Word and manually execute the excel macro it works.
Edit: It works
I just changed the excel-options as Alain said. here his post.
Oh I know why that's happening. You have to change your default excel format: Orb > Excel Options > |Save| > Save files in this format: "Excel Workbook (.xlsx)". Your current setting will be Excel 97-2003 Workbook (.xls). – Alain Aug 29 at 12:09
Workbooks.Add
once but it didn't help at all – lorenz albertcan't copy from source to destination cause the destination has less rows and columns than the source file
typically occurs when you try to save a sheet created in axlsx(m)
workbook into axls
workbook. This can't be down becausexlsx(m) (XL 2007/2010)
files have a differenct structure (more rows) thanxls (XL 2003)
. It seems at times in your explanation you mention this, so be sure you are copying either fromxls
toxlsx(m)
or from 'xlsx(m)` to 'xlsx(m)` – Scott HoltzmanWorkbooks.Add Template:="Workbook"
the new workbook is added as xls ? Some idea how to change it? It works with my workaround but this way I have an empty file on my harddisk just to be sure it ll be opened as xlsm file. – lorenz alberttablePath
workbook for yourexcel2
and then loop through the sheets and delete any sheet not in thecopySheets
array. Probably faster (and maybe more efficient) than moving sheets over? – Scott Holtzman