VBA script issue specifying command line - excel

So I'm fairly new to VBA, using it within some spreadsheets so forgive me if this is a super obvious fix, I'm sure it is but have spent hours on with with no success.
The following piece of code will lie inside of an if statement - not sure if thats worth mentioning or not.
I want to simply move, and rename files from one location to the other
It needs to utilize cmd.exe, and use the 'move' argument - the source path and source destinations will be changeable depending on the pc - so it will need to use the '%temp% - as the initial file will be stored in this folder, and %userprofile% for the destination path.
Here is the code so far - I think the issue is just to do with the formatting -
Sub movefile ()
Dim Origpath As String
Dim NewPath As String
OrigPath="%temp%\newfile.txt"
NewPath="%userprofile%`Documents\newfile.vbs"
<<next code inside of an if statement>>
moveFile="C:\windows\system32\cmd.exe /C move " & OrigPath & " " & NewPath
End Sub
Maybe the " and & arent placed correctly? But the code works fine up until the line 'movefile...'
Probably a super easy way of doing it - help please!!

Try using Name:
name OrigPath as NewPath
Alternatively you can use use fdo movefile, as explained in this answer.

Related

Uploading Excel file using VBA to SharePoint

Im struggling to resolve an upload problem direct from excel to sharepoint. I have the right permissions and can upload no problem when hardcoding the URL. Issue I have is all the SharePoint URL's are by Project Number and Description for example:
https://teamzzz.sharepoint.com/sites/Projects/NL001%20%20TEST/10.%20Dispatch
The project is called NL001 in this case. Another example of a URL could be:
https://teamzzz.sharepoint.com/sites/Projects/ZNP001%20%20ANOTHERTEST/10.%20Dispatch
Basically at the point of attempting to upload using my Add in I know the first part of the PATH:
https://teamzzz.sharepoint.com/sites/Projects/
The project id I can find from a cell reference:
NL001
The last part of the URL will always be the same as in:
10.%20Dispatch
My problem is the description after NL001 i.e. "%20%20TEST" (see below):
https://teamzzz.sharepoint.com/sites/Projects/NL001%20%20TEST/10.%20Dispatch
Is there anyway I can use some form of wildcard in the URL for example:
https://teamcde.sharepoint.com/sites/Projects/NL001*/10.%20Dispatch
OR
https://teamcde.sharepoint.com/sites/Projects/NL001%/10.%20Dispatch
The above 2 example dont work but was demonstrating the example I require. Here is my code thus far:
Public Sub CommandButton39_Click()
ProjectFileName = Range("B2").Value
SharePointBasePath = "https://teamcde.sharepoint.com/sites/Projects/"
SharePointEndPath = "10.%20Dispatch/"
ActiveWorkbook.SaveAs Filename:=SharePointBasePath & ThisFile & ".xlsm" & SharePointEndPath
End Sub
Is is possible to utilise a wildcard as the description is not known at time of upload.
Thankyou

VBA code that executes two .bat files one after another

I'm trying to write a code that would create and execute two batch scripts, one after another. The 1st makes a given number of copies of my pdf file (makecopies.bat) and the 2nd one changes the names of the copies according to a list of names. Thus, it is crucial that the execution of the 1st goes to completion before the 2nd one runs. That is why I refrain from using Call Shell("cmd.exe /k cd " & CurDir & " && makecopies.bat", vbHide) - I tried it and it would result in overlapping execution of scripts.
Instead, I tried to make it work with WScript.Shell, because that method allows setting waitOnReturn value. However, the problem is as follows (I need to execute two scripts, I use one as an example):
the part below works fine on my own laptop and when I try to run the code on my company computer (the one I intend to use the code at), it gives "Run time error -2147024894(80070002) Automation error The file cannot be found"
Dim wsh As WshShell
Set wsh = New WshShell
Dim makecopies As String
Dim script_makecopies As Long
Open CurDir & "\makecopies.bat" For Output As #FileNumber
Print #FileNumber, Range("B2").Value
Close #FileNumber
makecopies = CurDir & "\makecopies.bat"
script_makecopies = wsh.Run(makecopies, windowStyle:=0, waitOnReturn:=True)
I tried to change my code in order to overcome the problem - and it kind of worked, creating another problem in turn :D I replaced the last line with wsh.Run "cmd.exe /k makecopies", 0, True. This gets the job done, however at this point Excel stops responding and the rest of code (i.e. the second batch file) is not executed. I noticed that when removing /k Excel works fine and the code that follows this line is executed, but the batch (makecopies.bat) itself is not.
Does anyone have any idea how can I make it work? (Thank you in advance!)

Loop through to Import Multiple Excel Files and convert each workbook in a file to .dta

I have a file that contains over a 60 excel workbook that I would like to convert each of them to .dta file in stata. I search the net but could not find a decent way of doing it over a loop. I have written a code that needs expert help. In a directory and want to create a loop to save them as .dta files. The code goes as follow
forvalues i=1/60{
import excel "D:\Okay\""`i'.xlsx", sheet("Sheet1") firstrow clear
save "D:\Okay\""`i'.dta"
}
We can't try out your code because it's specific to your computer. Please study https://stackoverflow.com/help/mcve before posting questions.
But it's evident that
\Okay\""`i'.xlsx"
is unlikely to help. As documented many times over -- e.g. [U] 18.3.10 within http://www.stata.com/manuals14/u18.pdf and http://www.stata-journal.com/sjpdf.html?articlenum=pr0042 -- the backslash you want to use under Windows (it's best not to assume everyone recognises your OS) also has a role in Stata as an escape character.
That command would be better off ending
\Okay/`i'.xlsx"
and similar comments apply to the other command lines mentioning files: change the backward slash before a local macro reference to a forward slash, and remove the unnecessary double quotation marks.
In fact all this is totally avoidable. Consider
cd "D:\Rami Chehab\University Degrees & Courses\PhD in Labour Economics\Data\Data 2016\UNCTAD\Okay"
forvalues i=1/60 {
import excel `i'.xlsx, sheet("Sheet1") firstrow
save `i'.dta
}
Once you cd to work within a directory or folder, you can keep file names to the bare minimum.
There was plenty of errors I did over there; however, I believe I figured it out. This what you have to code in order to work
forvalues date=1/57{
import excel "D:/Rami Chehab/University Degrees & Courses/PhD in Labour Economics/Data/Data 2016/UNCTAD/Okay/`date'.xlsx", sheet("Sheet1") firstrow clear
save "D:/Rami Chehab/University Degrees & Courses/PhD in Labour Economics/Data/Data 2016/UNCTAD/Okay/`date'.dta"
}

livecode and mergext dropbox sync

i am using mergext dropboxsync to synchronise my data from the ipad.
my question is.
how we get the file from the special folder specialfolderpath
and how we check if the file exist into the dropbox.
i am no using fields for folder or the files i want the procedure to be hidden from the user
to get my folder and path i use the simple bellow code.
put specialfolderpath("documents") & "/myfile.sqlite" into myPath
and here is the code from the button i use
on mouseUp
goToParent --<command
repeat for each line tempitem in fld "sFolders" --<hide field
add 1 to t
if tempitem ="hairaid-backup" then
put 1 into fExist
end if
end repeat
--!! if folder exist
if fExist is a number then
else
try
mergDropboxCreateFolder (hairaid &"-"&backup)
catch e
answer e
end try
end if
end mouseUp
I would not really recommend synchronising a sqlite database over dropbox. You are likely to have better results creating a tree for files for dropbox to sync as one large file is likely to end up with conflicted versions.
However, answering your question in the general sense you would need to do something like this:
put url ("binfile:"&myPath) into myData
mergDropboxWriteFile relativePath,myData
Then to get the data you:
put merDropboxGetFile(relativePath) into myData

Lotusscript search windows directory for subdirectories and files. Recursion?

I'm trying to write a LS agent to scan a directory in windows eg:'C:\' for any files and sub-directories. For each sub-directory, it will go inside and search for more files and sub-directories and continues until there's no more to look for. I'm used to write recursive code to replace or remove characters in a long string but for this one I'm totally lost. Below is my code (it's a mix of code from the domino help file and one I found in IBM site):
Sub Initialize
Dim pathname As String, filename As String
pathname = "C:\*.*"
filename = Dir(pathname, 16)
Print "Begin scan"
Do While filename<>""
If IsDir(pathname+filename)=True Then
Print pathname+filename+" is a directory"
'look for more directories and files in here
Else
Print filename+" is a file"
End If
filename=Dir()
Loop
Print "Finish scan"
End Sub
Function IsDir(Path As String) As Integer
Dim Void&
Dim Result As Boolean
On Error GoTo ErrorHandler
Void=FileLen(Path)
Result=False
GoTo Over
ErrorHandler:
Result=True
Resume Over
Over:
IsDir=Result
End Function
What do I need to change to make the code recursive at the commented part? ('look for more directories and files in here). I'm not just trying to find a specific file or directory. I want all that's available withing one. If I'm able to do that, then I can retrieve them and save into a NotesDocument.
I've used this solution a couple of times and it works a treat:
First of all, you don't want to call Initialize recursively. You need a function that you're passing a pathname into.
Secondly, because of the stateful way that the DIR function works, I think you have to do this with two loops. In the first loop, you process your regular files and you put the folder names ino a list. Then in the second loop, you go through the list of folders and call the recursive function passing in the path to each one.

Resources