Macro to save copy of Personal.xlsb - xlsb

I want a macro to save backups of my Personal.xlsb -- Why won't this work for me?
Workbooks("Personal.xlsb").SaveCopyAs "C:Users\Tom\Documents\Test.xlsb"
I get runtime error 1004 saying Excel cannot access the file "C:Users\Tom\Documents\Test.xlsb" -- which I wouldn't think it would need to access but instead to create.
(I know I can manually copy Personal.xlsb from one place to another.)

As provided by my teacher Leila Gharani, the key is: Even using a different name, a copy of the open Personal.xlsb file can only be saved to the xlstart folder. So the code to save a copy of Personal.xlsb in another folder (with User Name substituted for Tom):
Workbooks("Personal.xlsb").SaveCopyAs _
"C:\Users\Tom\AppData\Roaming\Microsoft\Excel\xlstart\Personal(1).xlsb" _
'can only save it to this folder
Name "C:\Users\Tom\AppData\Roaming\Microsoft\Excel\xlstart\Personal(1).xlsb" As _
"C:\Users\Tom\Documents\Personal-Test.xlsb" _
'can name as (and thus move to) any folder & name

I use a seperate program like SycBackFree to make a backup for the personal.xlsb because then it is also possible to save it to a other computer,nas or USB storage.

Thanks #TommyExcels. This helped me.
To build on the post a little, there's a variable that you can use for the XLSTART folder that means you don't have to put the user name in the path....
Workbooks("Personal.xlsb").SaveCopyAs _
Application.StartupPath & "\Personal(1).xlsb"
'can only save it to this folder
Name Application.StartupPath & "\Personal(1).xlsb" As _
"C:\temp\Personal-Test.xlsb"

Related

VBA accessing files from a remote directory (error 76 - path not found)

I have a remote server I have read permission (in Windows I labeled it as my X: drive)
The correct path to a file I need to access is:
"X://some dir/some file"
The file that holds the Macro that is running exists in the C: drive. The code below checks to see if the path exists
If dir("X://some folder/some file", vbDirectory) = "" Then
Debug.Print "dir does not exist"
End If
Running that above enters the branch telling me the file does not exist. My questions are:
Do I have to put the name of the drive shorcut? (eg X: or can I put //: instead?)
How can I debug in Excel if I'm even allowed to enter the drive?
First, use backslashes for Windows paths. Second, don't use double backslashes when referring to a mapped drive. (Labelling the drive is meaningless to VBA.) eg:
X:\some folder\some file
If you didn't actually create a mapped drive, you will need to use the UNC or IP (and then you do use the double backslash) . EG:
\\remotehost\path\to\somefolder\somefile.txt
or
\\127.0.0.1\path\to\some folder\some file.txt
I think Tim is correct.
How can I debug in Excel if I'm even allowed to enter the drive?
If you RECORD macro opening a file in your X drive. You will see all the code that you need to put your original code working ;) . Every VBA coder do that kind of tricks.

Copy folder structure and merge sub directories

enter image description hereI'm not sure where best to post this, hopefully someone can advise if this isn't right, I've been a long time browser but this is my first post. Anyway..
I need to automate copying a load of folders to a new location and merging sub directories.
I will use ABC as an example text string which represents the company name and is a constant value.
The current structure is like:
2012 Jobs
ABC12 001-099
ABC12-001
jpgs
raw
web
tiffs
ABC12-002
ABC12-003
...
ABC12 100-199
ABC12 200-299
2013 Jobs
...
2014 Jobs
...
What I need to do is copy this structure to a new location, but merge the jpgs and web folders in to one named jpgs for example and the raw and tiffs in to another folder named tiffs.
I created an AppleScript to create the folder structure but these are obviously all just blank folders, would be much easier to just copy what is there already and start again, open to any suggestions, you're the experts! Thanks
Image shows structure before and how I want it after:
Before and After
The script bellow does partially what you need (I think !).
It looks for every "raw" in the top folder you selected (i.e. the folder containing all the "2012 jobs", 2013 Jobs",...)
For each "raw" folder found, it looks if, at same level a "tiffs" folder already exists (if not it creates it).
Then it moves all the files from "raw" to the "tiffs". Finally, it deletes the empty "raw".
set TopFolder to choose folder with prompt "Select parent folder to process"
tell application "Finder"
set RawsFolder to every folder of entire contents of TopFolder whose (name is "raw")
repeat with aFolder in RawsFolder
set ParentFolder to container of aFolder -- get upper folder
set TiffFolder to (ParentFolder as string) & "tiffs"
if not (exists TiffFolder) then make new folder in ParentFolder with properties {name:"tiffs"}
set FilesRaw to every file of aFolder --get all files from raw folder
move FilesRaw to folder TiffFolder -- move them to tiffs folder
delete aFolder -- delete the raw folder
end repeat
end tell
To complete, you must do same thing again, but with "web" folders to be copied into "jpgs". You have 2 ways to do that :
1) move the "tell Finder" block into sub routine with 2 strings parameters (raw, tiffs), adjusting the script to replace each occurrence of raw/tiffs by the parameter. then you call this routine with ("raw", "tiffs") and again with ("web","jpgs")
2) you just copy/paste the tell finder block and change "raw"/"tiffs" manually to "web" / "jpgs"
Up to you...
Warning : don't forget to run this on a full copie of your parent folder !

what is the correct path to the iMovie Projects folder to use in an AppleScript

I'm working on a simple AppleScript to make a copy of an iMovie project file. I have added this property to the script:
property iMovieProjects : alias (home directory of (system info) as string) & "Movies:iMovie Projects"
This gives me the error
File alias Macintosh HD:Users:my user name:Movies:iMovie Projects of wasn't found
What's the correct path? I tried iMovie Project.localized but that doesn't work either.
Try this, looks like you need to use the Finder. Also i added a little simplification on getting the home path.
property iMovieProjects : ""
tell application "Finder" to set iMovieProjects to alias ((home as string) & "Movies:iMovie Projects")
EDIT: Heres a one line solution that does not use the Finder, based on ideas from mklement0 & regulus6633
property iMovieProjects : alias ((path to movies folder as text) & "iMovie Projects.localized")
It appears this folder is some kind of special bundle type folder and thats why we were needing the finder to parse it correctly. Using its full .localized name resolves it.
UPDATE: As adamh discovered, the reason for the alias error is because the real name of the folder is "iMovie Projects.localized". Get info on the folder and you will see it.
I would add that an easier method to get to a folder is using the "path to" command. Using that you can get to virtually every known folder. In your case we can get directly to the Movies folder. You can look in the applescript dictionary of the standard additions to see all of the folders it knows. As such I would reference that folder as follows.
set iMovieProjects to alias ((path to movies folder as text) & "iMovie Projects.localized:")
Finally you'll notice that I did not use a property for iMovieProjects. That's because when you compile a script a property will hard-code the path into the script... meaning that the script will only work for this particular user. If the script is used by another user it will still point to the Movies folder of the person at compile time. Thus we don't use a property and the script will work for any user.
Good luck.
Update2 (thanks, #adamh): Even though the iMovie projects folder name appears as "iMovie Projects" in Finder in English-language locales, the actual - locale-independent - name is "iMovie Projects.localized". (If you want to refer to the folder by its localized name, prefix the set command with tell application "Finder", as in #adamh's answer's first snippet.)
You simply need to change how you parenthesize - the string to pass to alias must be built in its entirety inside parentheses:
property iMovieProjects : ""
set iMovieProjects to alias ( ¬
(home directory of (system info) as string) & "Movies:iMovie Projects.localized" ¬
)
Update1: #regulus6633 makes the excellent point that you shouldn't initialize a property with a specific user's path, as it will get compiled into the script. Thus, the above snippet initializes the property to an empty string and then assigns a value dynamically - which will then reflect the current user's path - in a separate set ... to statement.
Your original statement inadvertently creates a list with 2 elements, whose first element is a an alias of your home directory and whose second element is the string "Movies:iMovie Projects".
Simplified version with the path to command demonstrated in #regulus6633's answer (the same need to parenthesize applies):
property iMovieProjects : ""
set iMovieProjects to alias ( ¬
(path to movies folder as text) & "iMovie Projects.localized" ¬
)

Delete file in windows 7 using VB.NET

I have written following code in vb.net to delete the file.
If File.Exists(strPath & "\ReportEng.ini") = True Then
File.SetAttributes(strPath & "\ReportEng.ini", FileAttributes.Normal)
File.Delete(strPath & "\ReportEng.ini")
End If
File.Copy("\\192.168.0.1\SAP_Shared\AddonExtra\ReportEng.ini", strPath & "\ReportEng.ini")
This code works perfectly in windows xp. But in Windows 7,I can not delete it. This OS is hectic OS from developer's point of view. Some or other problem occurs and Microsoft has not considered the developer while building this OS.
How do I delete file in Windows 7 ?
It's so easy to do so;
If My.Computer.FileSystem.FileExists("C:\somefile.ext") Then 'Check whether file exists
My.Computer.FileSystem.DeleteFile("C:\somefile.ext") 'Delete the file!
End If
Have a nice day!
You don't need to delete the file: there is an overload File.Copy Method (String, String, Boolean) which allows overwriting.
You didn't say what error you get. I suspect it is because the user doesn't have write access to the directory. You should probably be using a subdirectory of the directory returned by Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) or maybe .LocalApplicationData, and definitely not the directory containing the program.
Also, using Path.Combine(strPath, "ReportEng.ini") is how you're meant to combine paths - it'll take care of, e.g., the trailing path separator for you.
The preferred method for interfacing with the Windows file system uses the following namespace:
Imports Microsoft.VisualBasic.FileIO.FileSystem
To delete a file:
Dim FileLocation As String = strPath & "\ReportEng.ini"
If Not GetDirectoryInfo(FileLocation).Exists Then
GetFileInfo(FileLocation).Delete()
End If

Getting file list in a folder

I have created a tool which picks up a file from a specific location, copies it, zips it and then puts it at another location. The user has to select the required folders from the location.
Is there any way through which I can create an option in the tool so that the user can see the list of available folders at that location, or some way to direct the user directly to that location? I only need the folder names.
I tried it with cmd but since the location is not on my computer (it's on another computer with shared property) I dunno how to access that location. Any help, any hint is very much appreciated. My tool is in VBScript and ASP.
You can use a FileSystemObject to get the contents of a directory.
set fso = CreateObject( "FileSystemObject" )
set my_folder = fso.getFolder( "C:\Example" )
Then, use the Folder object to get its contents.
set sub_folders = my_folder.subFolders
for each f in sub_folders
wscript.echo( f.name & VBNEWLINE )
next

Resources