How will this variable respond - vba

I'm using Excel VBA but I think this is quite a generic question.
I have the following declaration
Public newReport As Excel.Workbook
Later in the code, after I've opened and named an xlsx file MyBook.xlsx I point this variable at the file:
Set newReport = Excel.Workbooks("MyBook.xlsx")
I'm getting slightly confused again re. variable behaviour.
If I later have MyBook.xlsx open and I execute ActiveWorkbook.SaveCopyAs and give it a new name say MyBookNew.xlsx will the variable newReport be pointed at the new workbook?
Example of related code is the following:
Public newReport As Excel.Workbook
Sub FileExperiment()
Set newReport = Excel.Workbooks.Add
newReport.SaveAs ThisWorkbook.Path & "\MyBook.xlsx"
Set newReport = Excel.Workbooks("MyBook.xlsx")
ActiveWorkbook.SaveCopyAs ThisWorkbook.Path & "\MyNewBook.xlsx"
newReport.Close False
End Sub

newReport will be unchanged.
Cut from the help file, regarding the command SaveCopyAs:
Saves a copy of the workbook to a file but doesn't modify the open workbook in memory.

Related

VBA EXCEL - Extract a copy of workbook

Im trying to extract a copy of my workbook to a new file in VBA but I'm getting an "Application or object defined error" and i have no clue whats wrong.
All im using is the command i found on the microsoft site?
Public Function EWbtn()
ActiveWorkbook.SaveCopyAs "C:\CRC Chart Extract.XLS"
End Function
Im very confused :L
As Michal said, the easier way is just create the file first then populate it with your current active workbook. Here is a sample that should be able to get you started
Sub try_me()
Dim workbookPath As String
Dim output_filename As String
'getting your active workbook path
workbookPath = ActiveWorkbook.Path
'pre-defined output filename
output_filename = "my_other_worksheet"
'Copy your current active workbook to the new wb
ActiveWorkbook.Sheets.Copy
'save the workbook
ActiveWorkbook.SaveAs Filename:=x & "\" & y & ".xls"
End Sub
Credit to Smitty here:
https://www.mrexcel.com/forum/excel-questions/139831-create-empty-workbook-visual-basic-applications.html
I got the same error. I solved it by creating the file. Most probably copying can be done to existing file. Add code to create the file before copying to it.
It is doubtful that a function can do this. I tried below code and it's working.
Public Sub EWbtn()
ActiveWorkbook.SaveCopyAs "C:\CRC Chart Extract.XLS"
End Sub

VBA save Excel file as .xls from Access

Hi, I have to perform Import from Excel file into Access DB, without duplicates. Only way that this can be done is by creating table where you can Import Excel data, and then append It to destination table by buiilding a Query. Problem is that this Excel file has to be .xls format, none other format is working for me (don't know why). How can I do that from Access - desirable without opening Excel workbook, just change It's extension file name ? This is what I tried (I receive error : " object doesn't support this property or method")
Dim XFile As Excel.Application
Set XFile = CreateObject("Excel.Application")
XFile.Workbooks.Open "C:\Users\Mike\Desktop\Copy.xlsx", True
XcelFile.SaveAs Filename:="C:\Users\Mike\Desktop\Copy.xlsx", FileFormat:=56
XFile.Workbooks.Close
End Sub
As you see, I need to save from .xlsx, It's default format for Excel 2013.
Any help much appreciated !!
Thanks #Porcupine911, that worked, even without opening workbook :)
Dim XcelFile As Excel.Application
Dim wb As Excel.Workbook
Set XcelFile = New Excel.Application
Set wb = XcelFile.Workbooks.Open("Filepath here")
wb.SaveAs Filename:="Filename here, with different destination and desirable extension", FileFormat:=56
wb.Close
Set XcelFile = Nothing
End Sub

Setting Document Properties while saving individual sheets

I'm not familiar with VBA at all so this has me stumped, hoping someone can help.
I have a template on a sharepoint server that is working correctly. In order to save the xlsm file with the server document properties, I have a command button which runs the following:
Sub UpdateDB()
For Each Prop In ThisWorkbook.ContentTypeProperties
If Prop.Name = "Name" Then
Prop.Value = Range("B8").Value
End If
Next Prop
Filename = Range("B59").Value
ActiveWorkbook.SaveAs Filename:=Filename, FileFormat:=52
End Sub
This is working well.
I now have another template which I want to do a similar thing but there are some complications:
I have several worksheets which I would like saved as individual files containing only the said worksheet.
The document properties for each file should different, based on a cell in each worksheet.
Now I have managed to setup point 1 and it's working OK using the following code:
Sub NewSub()
Worksheets("EMP1").Activate
Filename = Range("B1").Value
Dim wb as Workbook
Application.ActiveSheet.Copy
Set wb = ActiveWorkbook
With wb
.SaveAs Filename:=Filename, FileFormat:=52
End With
End Sub
This is working well but if I try adding in code to set the Document Properties, it doesn't work, it won't carry them through to the new file. I have tried adding the code which sets the document properties after line 2 and I have tried adding it also after line 7; it doesn't throw an error but it doesn't set any document properties either. Any help on this would be much appreciated. Thanks.

Opening and Excel file in Access using VBA and saving it to a different name and closing it properly

I have been searching for some time on how exactly to go about this, but I keep coming up with a large number of possible ways that come close, but never really give me exactly the sort of thing I'm looking for. The concept is pretty simple I need to open a certian .xls file using some VBA code in Access 2010. Once the file is opened I need to insert data and do some things to the file then save the file as a different filename and close the file. I also need it to close excel if it was not already open and if it was open I need it to leave excel alone and not save/close anything other than the template.xls file I am working with. I currently have code that will do part of this provided Excel is not already open at the time the script runs. When excel is already opened I get the following error;
"Run-time'91': Object variable or With block variable not set."
When I click debug I get the following line highlighted
x.ActiveWorkbook.SaveAs fileName:=savedfilename
Here is the code without all the junk that doesn't relate to the issue. I have cobbled together using examples from various sites.
Dim DateSampled As String
Dim strPath As String
Dim TemplatePath As String
Dim x As Excel.Application
Dim xBook As Excel.Workbook
Dim xSheet As Excel.Worksheet
DateAsString = Format(DateSampled, "MMDDYYYY")
savedfilename = strPath & "\" & TrainNum & "-" & DateAsString & ".xls"
TemplatePath = "B:\template.xls"
Set x = CreateObject("Excel.Application")
x.Visible = False
Set xBook = GetObject(TemplatePath)
xBook.Windows(1).Visible = True
Set xSheet = xBook.Worksheets(1)
'---------------CODE DOES STUFF WITH THE FILE -----------------------
x.DisplayAlerts = False
x.ActiveWorkbook.SaveAs fileName:=savedfilename
x.DisplayAlerts = True
x.ActiveWorkbook.Close
Set x = Nothing
Set xBook = Nothing
Set xSheet = Nothing

Excel macro Auto start only in XLT file and not in XLS file

I am using and XLT that contains macros, it creates an XLS by open, and
save it in a choosen directory, the problem is, the saved file contains the
same macros as the XLT. Is there any chance to modify the proc to make it
save the generated file without the macros?
best regards,
Thomas
If your question is about how to autostart a macro only when it is called from XLT and not when it is called from the generated XLS, then maybe this can help
Private Sub Workbook_Open()
If ThisWorkbook.Path = "" Then
Call myMacro
End If
End Sub
myMacro will only be called if the opened file is XLT, not XLS.
There are a couple of ways of doing this. If you only need the worksheets and none of the code, then the simplest method is probably just to copy out the worksheets to a new workbook then save that:
Private Sub workbook_open()
Dim wb As Workbook
Dim saveName As String
ThisWorkbook.Sheets.Copy
Set wb = ActiveWorkbook
saveName = Application.GetSaveAsFilename(fileFilter:="Excel Workbook (*.xls), *.xls")
If Not saveName = "False" Then
wb.SaveAs saveName
End If
End Sub
If some code is still required in the XLS then you would need to manipulate the VB environment directly to remove the parts you didn't need. This is a bit more involved and has some important restrictions/caveats, but it sounds like the simpler method illustrated above may suit your needs anyway.