I am using VBA to create folders automatically. One of the folder names that I need to create contains the character č (c with caron). When I use MkDir in VBA the folder is created with a "c" instead of a "č".
Sample code:
root_folder = "C:\customers\"
folder_name = "háček" 'I do not think you can enter this into the VBA editor, but I am getting the folder_name from an external source
full_folder_path = root_folder & folder_name & "\"
MkDir full_folder_path
attachment.SaveAs full_folder_path & attachment.filename
This will create a folder called "C:\customers\hacek\" instead of "C:\customers\háček\", which then causes the save operation to fail, because it tries to save in "C:\customers\háček\, which of course does not exist. VBA seems to be able to read and handle the characters correctly, because I can read it from my data source and save it to a text file without issues. The problem just seems to exist when it comes to creating folders.
Is there a way to make VBA create the folder with the name that I have actually specified?
edit: formatting
If you use FileSystemObject, you are able to create the Folder with the proper name:
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CreateFolder "C:\customers\hac" & ChrW(269) & "ek" 'ChrW(269) prints č
Related
I use Foxit Phantompdf, the complete version and ACCESS.
In our program, we have to save multiple pdf files, some of them should be merged in single files when saved.
Here is the code I use;
Dim phApp As PhantomPDF.Application
Dim n1 As String
Dim n2 As String
n1 = "c:\Temp\F3769-190136-GROUPE OCÉAN.pdf"
n2 = "c:\Temp\f3769-190136-GROUPE OCÉAN- facture.pdf"
Set phApp = CreateObject("PhantomPDF.Application")
Dim phCreator As PhantomPDF.Creator
Set phCreator = phApp.Creator
***'Call phCreator.CombineFiles("c:\Temp\F3769-190136-GROUPE OCÉAN.pdf|c:\Temp\f3769-190136-GROUPE OCÉAN- facture.pdf", "c:\Temp\F3769-190136-GROUPE OCÉAN.pdf", COMBINE_ADD_CONTENTS)***
Call phCreator.CombineFiles("""c:\Temp\" & n1 & "|'" & n2 & """" & ", " & """c:\Temp\F3769-190136-GROUPE OCÉAN.pdf"""" &", COMBINE_ADD_CONTENTS)
phApp.Exit
When I try it with the complete files names (in bold) the code works perfectly.
However, when I try to use variables, I get a
"Argument not optional"
error.
Can somebody help me ?
Thanks
Your string definitions in call line is incorrect.
You have defined n1 and n2 with the c:\temp already, and in your string conversion you add this again. I do not know if this is the route cause to your issue.
Furthermore I do not know the actual needed syntax for this phcreator.combine()
But is it not possible using only:
call pHcreator.combine(n1 & "|" & n2, …
The 'Argument not option' might imply you should add another input to your pHcreator, I would guess it could have something to do with FOXIT's combine function page settings. Try adding a input variable integer at the end of the function maybe?
But the fact that it works when writing strings in clear text would maybe suggest that the string manipulations is not correct?
I'm not a vba professional, but interested in the outcome, working myself with Foxit and also want to combine with vba. I'm currently not using version 9 som I seem to be out of luck, and only upgrading it I know what I want to do is possible.
I tried it, but got the same error message. So I took advantage of the fact that the function works when the file names are in plain text. I copied the files to be merged in a temporary folder and rename them. The renamed files are the then used in the merge function. It works perfectly, but Foxit adds a table of content page, and I don't know, yet, how to remove it. Here is my solution:
Private Sub Command4_Click()
Dim addi As String 'file to be merged to main file
Dim princi As String 'main file
Dim phApp As PhantomPDF.Application
'A temporary folder, in this case c:\t2, should be present
'In this example c:\Temp is the working folder
addi = "c:\Temp\filetomerge.pdf" 'full path of file to be merged
princi = "c:\Temp\mainfile.pdf" 'full path of main file
'fadd,pdf and fmain.pdf are the temporay files used in Foxit's function
FileCopy addi, "c:\t2\fadd.pdf" 'temporary file to be merged in temporary folder
FileCopy princi, "c:\t2\fmain.pdf" 'temporary main file in temporary folder
'Merge action
Set phApp = CreateObject("PhantomPDF.Application")
Dim phCreator As PhantomPDF.Creator
Set phCreator = phApp.Creator
Call phCreator.CombineFiles("c:\t2\fmain.pdf|c:\t2\fadd.pdf", "c:\t2\fmain.pdf", COMBINE_ADD_CONTENTS)
phApp.Exit
'Save merged file in working folder under main file name
Kill princi
FileCopy "c:\t2\fmain.pdf", princi
'delete temporary files
Kill "c:\t2\fadd.pdf"
Kill "c:\t2\fmain.pdf"
End Sub
I am trying to create a text file that stores a folder path. This text file is then referenced via a vba sub. The path I want to use is something like:
"C:\Users\" & Environ("username") & "\AppData\Roaming\Microsoft\Templates"
This works fine in the sub but I've tried all kinds of variations in the text file but none of them get recognised and trigger error 52 - bad file.
Is there a way to make this work? I'm trying to allow people to set a different file path without needing to modify the code.
If you are trying to provide a path to the folder where user templates are stored then you could try
ActiveDocument.AttachedTemplate.Path
as an alternative (returns the path to the folder where the current template is stored for the user).
Otherwise store the path template as something like
"C:\Users\###UserName###\AppData\Roaming\Microsoft\Templates"
Which gives you a single string to retrieve. Then you can use the VBA Replace function to change the ###UserName### to the value of Environ("UserName)"
my_user_path = replace(my_path_template, Environ$("UserName"))
You might also want to explore using either a CustomDocumentProperty, or a Variables, to store your path template as this keeps the path template string as part of the Document or Template and not in a separate file.
so I have been trying to create a script that will create a folder on my desktop, then create a spreadsheet, and save the spreadsheet to said folder. here is the script...
set folderName to "Tester"
set FileName to "MayDay"
set SaveName to FileName & ".numbers" as string
set FilePath to "Macintosh HD:Users:Ryan:Desktop:" & FileName & ":"
tell application "Finder" to make new folder at desktop with properties {name:folderName}
tell application "Numbers" to activate
tell application "Numbers" to make new document with properties {name:FileName}
tell application "Numbers"
tell document 1
tell table 1 of sheet 1
merge range "B8:C8"
end tell
end tell
delay 1
tell application "Numbers" to activate
end tell
tell application "Numbers" to (save document 1) in FilePath
end
It has no problem making the folder, opening the spreadsheet, changing its name, and merging the cells, the problem comes in at the end when I try to save it.
I have tried adding (as numbers) no parenthesis after the save command, that errors out. After I run the script, and clear the crash, I can close out numbers and it will prompt me to save it in the "Tester" folder, but it won't do it on its own. insert grey hairs and headaches here.
I am really new to applescript, and I am hoping this is just a simple syntax thing, any help would be appreciated.
Thanks in advance!
R
Here's working code. A key is to use POSIX path and POSIX file and also the "save ... in ..." takes a file reference, not a string.
set folderName to "Tester"
set fileName to "MayDay.numbers"
set FilePath to (path to desktop as string) & folderName & ":" & fileName
set posixPath to POSIX path of FilePath
try
tell application "Finder" to make new folder at desktop with properties {name:folderName}
on error errMsg
end try
tell application "Numbers"
set myDoc to make new document with properties {name:fileName}
tell myDoc
tell table 1 of sheet 1
merge range "B8:C8"
end tell
end tell
delay 1
save myDoc in POSIX file posixPath
end tell
I have been using a snippet of VBA for years to zip all files in a folder. Today we tried this on a co workers computer and the code appears to go through its iterations but there is not any files output. The only difference is the new machine is 32 bit and the old code is for 64 bit. Is there any reason the 32 bit file would not work with VBA?
Sub ZipIndividualFiles1()
Dim file As Variant
Const source = "C:\Users\co01\Desktop\TEST"
Const DEST = "C:\Users\co01\Desktop\Zipped"
Const PATH_TO_7Z = "C:\Program Files\7-Zip\7z.exe"
For Each file In CreateObject("Scripting.FileSystemObject").GetFolder(source).Files
Shell PATH_TO_7Z & " a -tzip """ & DEST & "\" & file.Name & ".zip"" """ & file.Path & """"
Next
End Sub
7 zip exists at the PATH_TO_7Z path. We even tried re-installing it. The program runs to completion without error.
Hi i found this code that will do the job, that you need to place it the folder you need to zip and just run it and you can save as batch file execute via VBA.
I learned this recently so i like to indicate the changes that can be made.
If you need the script to zip all files as individual zip then you need to modify "%CurrDirName%.zip" to "%%a.zip"
If you need the script to zip all contents in to one you can change "%%a.zip" to "%CurrDirName%.zip
If you need to provide a name the simple you can hard coded the name there "Hardcoded.zip"
If you need to zip only certain file types you can add them in set extension
If you need to zip excluding certain file types you -x!*.bat, here .bat is what i am excluding
Hope it helps
#echo off
cd /d %~dp0
rem 7z.exe path
set sevenzip=
if "%sevenzip%"=="" if exist "%ProgramFiles(x86)%\7-zip\7z.exe" set sevenzip=%ProgramFiles(x86)%\7-zip\7z.exe
if "%sevenzip%"=="" if exist "%ProgramFiles%\7-zip\7z.exe" set sevenzip=%ProgramFiles%\7-zip\7z.exe
if "%sevenzip%"=="" echo 7-zip not found&pause&exit
for %%I in (.) do set CurrDirName=%%~nxI
set extension=.*
for %%a in (*%extension%) do "%sevenzip%" a "%CurrDirName%.zip" "%%a" -x!*.bat
pause
[/CODE]
In Access VBA, I have a procedure I've put together to do this:
Allow the user to select zip file(s)
Extract any files from the zip files to the same directory (In this
specific use-case instance, it is ALWAYS extracting Excel files from
Zip files, never any change, and always using the same password)
Then I want the code to Delete the Zip file after extracting the
.xls file.
Everything works beautifully except the delete file portion. The issue is that when I tell it to delete "FileName.Zip", it is deleting "FileName.Zip" AND "FileName.xls"
Is there any way to make sure that he kill command ONLY deletes what I want it to delete? I've used it before on various occasions, and never had this happen before.
Here is the code I am using:
Dim fd As FileDialog
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Variant
Set db = CurrentDb
Set rs = db.OpenRecordset("tblProjectPath")
Set fd = FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = True
fd.Title = "Select TOC Return File(s) to process"
fd.InitialFileName = rs.Fields("ProjectPath") & "\FilingReports\*.zip"
fd.Show
For Each i In fd.SelectedItems
'Debug.Print i
Debug.Print '------------------------'
Debug.Print i
Unzip (i) 'The bit calling the command line unzip utility to unzip the file - just telling it to extract all files to the current folder.
Debug.Print i
'Kill i
'had to take out the kill bit, b/c it was deleting both the .zip and .xls files which is not desired nor expected
If InStr(i, ".zip") Then
Kill i 'Tried to specify only .zip files even though think I shouldn't need to, but it's still deleting .xls files
End If
Next i
Edit: Add Unzip code to post:
Unzip code:
Sub Unzip(Path As String)
Dim strUnzip As String
Dim QU As String 'quotation mark
QU = Chr(34)
strUnzip = QU & "c:\program files (x86)\winzip\wzunzip" & QU & " -s" & _
"ZipPassword " & _
Path & " " '& _
Call Shell(strUnzip)
End Sub
At this point, I don't really think a "real" answer will come about. However, I'll post what I've decided to do with the particular process I'm writing this code for anyway.
I'm going to use a folder structure to divide up the files:
1. Place zip file(s)
2. Unzip files to a 2nd folder
3. After processing Excel files in 2nd folder, move to a 3rd "complete" folder.
This will get around the deleting wrong files bit.
Also, it appears that the cause for the issue is related to something to do with the call to the WinZip Command Line Unzip utility (wzunzip) in the Unzip code above, or else something with the tool itself. I thought that maybe it was b/c the tool was asking me if I wanted to overwrite existing files, but that wasn't the case, b/c I had the same issue when there were no files to overwrite.
Anyway, I'm attempting to close this one up at this point. Thanks to Wayne G. Dunn for his assistance on this.