Open saveas dialog box for another workbook - excel

I am currently working on a temporary solution but run into a problem....
The situation is:
I have a converter built in Excel, that opens up a XLS File, does some tinkering so the file can be uploaded to an Oracle Database.
The problem is, the XLS file I opened I cannot get the Saveas Dialog box to that file, it always saves a copy of the converter instead.
What I need to happen is to open the Saveas Dialog box in a filepath set the file filter for CSV, for the file that i have opened.
RRBOOK.Activate The RR Book is the DIM'd file i opened
Dim FileName As String
FileName = Application.GetSaveAsFilename("\\FILEPATH\", FileFilter:="CSV (Comma delimited) (*.csv), *.csv")
If FileName <> "False" Then
ActiveWorkbook.SaveAs FileName
End If
Any help would be appreciated!

Avoid using ActiveWorkbook and .Activate.
Instead directly save the desired workbook by
RRBOOK.SaveAs FileName
Also you should specify the file format you wish to save in according to XlFileFormat enumeration and Workbook.SaveAs method.
So for saving as CSV you should eg use
RRBOOK.SaveAs FileName, xlCSV
So you would end up with something like
Dim FileName As Variant
FileName = Application.GetSaveAsFilename("\\FILEPATH\", FileFilter:="CSV (Comma delimited) (*.csv), *.csv")
If VarType(FileName) = vbBoolean And FileName = False Then
'user pressed cancel
Exit Sub
Else
RRBOOK.SaveAs FileName
End If

Related

Hyperlink to open pdf file with variable

I'm currently trying to open an pdf file with variable
There a list of product name, you click on the name and it will open the appropriate pdf file
Its mean that you have to access in a folder, then from the name of the product, you find the pdf file and open it
Here is my code
Sub OpenFile()
Dim filename As String
filename = ActiveCell.FormulaR1C1
'If cell is empty, it don't open nothing
If filename = "" Then
Exit Sub
Else
ActiveWorkbook.FollowHyperlink ("Y:\Logistics\MSDS" & WorksheetFunction.Text(filename) & ".pdf")
The file name i want to open is "1.MSDS-BETACLEAN™ 3510_VN_VN"
The error is in WorksheetFunction.Text(filename), i try to replace it with Workbooks.Open filename and Dir(filename, vbDirectory) but it still didn't work

Automatical repair of number of corrupted .xlsx files via VBA script

I have a number of corrupted .xlsx files in a directory.
I want to open every single file for repair and save it with the same name via VBA script.
I`ve tried following piece of code to solve this problem:
Sub ProcessFiles()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = ActiveWorkbook.Path & "\output\"
Filename = Dir(Pathname & "*.xlsx")
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename, CorruptLoad:=xlRepairFile)
wb.Close SaveChanges:=True
Filename = Dir()
Loop
End Sub
But this code only repairs first file and opens windows explorer to save file manualy.
Is there a way to perform repair and save all files with the same name in the same folder automatically?
I haven't touched VBA in years, but there is an explicit wb.SaveAs method you can call.
Have you set Application.DisplayAlert = False? Your codes seems fine. You just have to turn it on later.

Export into a copied file

I will first tell you what I want to do, maybe that makes finding a solution easier. I want to export a file (after changing it) into a file that uses macro buttons etc on it's own. The problem is I cant delete said file.
My current solution is as follows: I use a "open file" Dialog so the user tells me the Excel that should be used as a template. VBA then should copy this file (the destination is once again given by a save file dialog) and then delete the used range in the new file and copy the values in this new file.
So there are 3 files Involved.
A: Current file with changed data and the macro I am doing right now.
B: Template File with macros.
C: New File that should be created with the macros from B and the
Data from A
So far I have this code that opens B and and copies it (i guess) it. My primary issue is how do I access the newly created file now. I assume I can somehow store it as a variable. Something like X = Path the user just selected in the save file dialog?
The deleting and inserting Data afterwards should be rather simple I guess/hope.
I hope you guys can help me and thanks a lot in advance :)
Here is my code so far:
Dim StandardPfad As String
Dim ExportBlatt As Worksheet
Dim NeueDatei As Workbook
StandardPfad = "C:\XYZ"
' ExportBlatt = Worksheets("Blatt1")
Set NeueDatei = Workbooks.Add
ThisWorkbook.Sheets("Blatt1").Copy Before:=NeueDatei.Sheets(1)
Application.DisplayAlerts = False
Sheets(2).Delete
Application.DisplayAlerts = True
' displays the save file dialog
StandardPfad = Application.GetSaveAsFilename(FileFilter:= _
"Exceldateien (*.xlsx), *.xlsx", Title:="Vorlagen Datei auswählen", _
InitialFileName:="")
NeueDatei.SaveCopyAs Filename:="C:\Test\CopyBook.xls" 'Hier Speicherort angeben
NeueDatei.Close savechanges:=False
Either I`m missing something in your question, or... Isn't this StandardPfad enough?
It needs a bit more error handling if the user cancel's the selection, but that's your X = Path the user just selected in the save file dialog
' displays the save file dialog
StandardPfad = Application.GetSaveAsFilename(FileFilter:= _
"Exceldateien (*.xlsx), *.xlsx", Title:="Vorlagen Datei auswählen", _
InitialFileName:="")
NeueDatei.SaveCopyAs Filename:=StandardPfad

Excel, VBA, Macro, File - Open or Create, Dialog

I am writing a macro in MS excel using VBA. I need to open or create a file to write to.
Potentially the file may have a different extension (i.e. .cal) but internally it just contains text.
I have looked over a lot of examples that create a file by explicitly stating the path for the new file (here's one I found):
strFileName = "C:\test.txt"
Open strFileName For Output As #iFileNumber
Other examples open a file which already exists.
I would like to have a popup/dialog which allows the user to "either" open an existing file "or" create a new one. I assume this is possible.
I have played around with the Application.FileDialog(....) function using strings/paths and objects without much success so far.
With Application.FileDialog(...) your user should be able to create a new text file as they would in Windows Explorer (by right-clicking and selecting New->Text File), they can then select that file to output data to.
The below SelectFile(...) function returns the path to a selected file (or an empty string if no file was selected). Using this function as-is it is only possible for the user to select one file, but given the context I would hope this isn't a problem.
Public Sub SelectOrCreateFile()
Dim strFileName As String
Dim iFileNum As Integer
iFileNum = FreeFile
strFileName = SelectFile
If strFileName <> "" Then
Open strFileName For Output As #iFileNum
'### WRITE YOUR DATA ###
Close #iFileNum
End If
End Sub
'Returns File Path of file selected with file selection dialog
Public Function SelectFile(Optional DefaultPath As String = "", _
Optional FileType As String = "All Files", _
Optional FileExtension As String = "*.*") As String
Dim F As Object
Set F = Application.FileDialog(msoFileDialogFilePicker)
'Set up FileDialog properties
F.Filters.Clear
F.Filters.Add FileType, FileExtension
F.AllowMultiSelect = False
F.Title = "Select File"
F.ButtonName = "Select"
If DefaultPath <> "" Then F.InitialFileName = DefaultPath
'FileDialog.Show returns False if the user cancels
If F.show Then
SelectFile = F.SelectedItems(1)
Else
MsgBox "No File Selected", vbInformation, "Cancelled"
SelectFile = ""
End If
Set F = Nothing
End Function
Hope this helps!

EXCEL Macro to open XML files as a XML table rather than a read only workbook

I'm using the following code to open multiple xml files, however they are opening as a read only workbook, however I require it to open as an XML table, any suggestions?
Code:
Sub AllFolderFiles()
Dim wb As Workbook
Dim TheFile As String
Dim MyPath As String
MyPath = "C:\Documents and Settings\"
ChDir MyPath
TheFile = Dir("*.xml")
Do While TheFile <> ""
'Call Logs 'This calls for Macro2 to run
Set wb = Workbooks.Open(MyPath & "\" & TheFile)
MsgBox wb.FullName
'wb.Close
TheFile = Dir
Loop
End Sub
You need to use Workbooks.OpenXML instead
Set wb = Workbooks.OpenXML(Filename:=MyPath & "\" & TheFile, LoadOption:=xlXmlLoadImportToList)
I'm not exactly which LoadOption you want to use, but you can choose from:
xlXmlLoadImportToList Automatically creates an XML List and imports
data into the list.
xlXmlLoadMapXml Loads the XML file into the XML Source task pane.
xlXmlLoadOpenXml Open XML files in the same way that Excel 2002
opens XML files (for backwards compatibility only).
xlXmlLoadPromptUser Prompts the user and lets them choose the Import
method.

Resources