I know this has been asked before, but for the life of me I cannot get this simple code to work. I keep getting the compile error "User-defined type not defined" on Dim wbopen As Workbook
line. I know the workbook has to be open to define it, and the file path should be Z:\Manufacturing\02- Schedules\01- Buffer Prep
while the file name is the only .xls in the folder. Why is this happening? Also, this is executing in a Word file. Not sure if that matters. Thanks everyone!
Sub fileopen()
Dim wbopen As Workbook
Dim strFileName As String
Dim strFilePath As String
strFilePath = "Z:\Manufacturing\02- Schedules\01- Buffer Prep\"
strFileName = Dir(strFilePath & "*.xls")
Set wbopen = Workbook.Open(strFileName)
End Sub
It does matter that it is a Word Document. You have to add a reference to Microsoft Excel Object Library. From the top bar:
Tools -> References -> find Microsoft Excel [version number] Object Library and check it.
Related
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
I have some VBA code in Excel 2007 which creates, fills, then closes a Textstream file object (abbreviated snippet below).
Set CSV = FSO.CreateTextFile(strPathOut & Application.PathSeparator & strFileOut)
'Fill the file
CSV.Close
The code works perfectly, including the CSV.Close instruction, however if I then try to delete or modify the file (e.g. in Windows Explorer or Notepad) the system claims Excel still has the file open. Seemingly the only way to release it is to close Excel itself.
I've checked that CSV.Close is doing what it's supposed to from the VBA side; it's not causing a runtime error, and certainly the file is no longer available to be written to after that instruction.
My project is early bound to the Microsoft Scripting Runtime, scrrun.dll. I've tried removing that reference but I get the same result.
This is not a showstopper for the project, but it's a PITA during development. Anybody know what's going on?
Try this simple example - the file is available for editing through e.g. Notepad, after the code has run:
Option Explicit
Sub Test()
Dim objFSO As New FileSystemObject
Dim objStream As TextStream
Dim i As Integer
Set objStream = objFSO.CreateTextFile("D:\temp.txt")
With objStream
For i = 1 To 10
.WriteLine CStr(i)
Next i
.Close
End With
Set objStream = Nothing
Set objFSO = Nothing
End Sub
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
I am trying to copy the values of one column in a sheet to a text file. The code I currently have causes runtime error 434.
Sheets("Output to fcf.1").Columns("A").SaveToText "P:\4_Calcs\02. Flag Mapping\test_.txt"
If I try and save the whole sheet
Sheets("Output to fcf.2").SaveToText "P:\Clear Project Drive\CLE10276 AWS SMP Model Assessmnts\4_Calcs\02. Flag Mapping\test2_.txt"
I get the entire sheet converted into text rather than just the text in the sheet. Is there a simple way to do this?
Thanks in advance!
Not sure which Excel version you have but I don't see a method for SaveToText.
But this procedure should work, or at least get you started...
Sub SaveColumn(sheetName As String, columnName As String, fileName As String)
Dim cell
Dim fso
Dim file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile(fileName, True)
For Each cell In Sheets(sheetName).Columns(columnName).Cells
If cell.Value <> "" Then
file.WriteLine cell.Value
End If
Next
file.Close
Set file = Nothing
Set fso = Nothing
End Sub
To call it...
SaveColumn "Output to fcf.1", "A", "P:\4_Calcs\02. Flag Mapping\test_.txt"
This is designed to be used as a macro.
Step by step guide:
1) From excel, hit Alt+F11 on your keyboard.
2) From the menu bar, click Insert, then Module
3) Copy and paste the code provided below into the new module that opens.
NOTE: DocPath = "C:\docs\data.txt" should be wherever you want the output file saved, including the file's actual name. Remember, the folder you want the output file to be located in should ALREADY exist. This does not create the folder if it can't be found.
4) From the menu bar, click Tools, then References. Make sure both "Microsoft Office 14.0 Object Library" as well as "Microsoft Word 14.0 Object Library" are checked, and hit okay (See screenshot for details)
5) Save the document as an .xlsm file (This file type supports Macros)
6) Close the VBA editor. Back in Excel, on the ribbon click View and then Macros. Your new macro should be in the list as ExportToTXT
7) Select it and hit run.
Sub ExportToTXT()
Dim DocPath As String
Dim MsgBoxCompleted
Columns("A").Select
Dim AppWord As Word.Application
Set AppWord = CreateObject("Word.Application")
AppWord.Visible = False
Selection.Copy
DocPath = "C:\docs\data.txt"
'Create and save txt file
AppWord.Documents.Add
AppWord.Selection.Paste
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath, FileFormat:=wdFormatText
Application.CutCopyMode = False
AppWord.Quit (wdDoNotSaveChanges)
Set AppWord = Nothing
MsgBoxCompleted = MsgBox("Process complete.", vbOKOnly, "Process complete")
End Sub
Good luck, and if you have any questions, don't hesitate to ask.
NOTE: These directions might seem overly simplified for your skill level, but I wrote the answer like this to potentially help others in the future.
EDIT
Change
DocPath = "C:\docs\data.txt"
to
DocPath = "C:\docs\data.fcf"
And change
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath, FileFormat:=wdFormatText
to
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath
The output file will be .fcf format. Whether or not it will open properly is something I'm not sure of. You'd have to test in the program you're using.
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.