Copy file and name it a value from the active cell - vba

I need this code to copy the Word file and call it a value from the active cell
FileCopy Application.ThisWorkbook.path & "\template.docx", Application.ThisWorkbook.path & "\exercies\ & Worksheets(1).ActiveCell.Value & "".docx"
But it gives me an error "Bad file of number".
Can someone help me?

The problem is this part of your code:
"\exercies\ & Worksheets(1).ActiveCell.Value & "".docx"
If you Debug.Print this part of the statement, you'll get:
\exercies\ & Worksheets(1).ActiveCell.Value & ".docx
which is not a valid path.
Try replacing it with this instead:
"\exercies\" & ActiveCell.Value & ".docx"
which will produce a valid path. Furthermore, I suspect you have misspelled the folder name. Shouldn't it be exercises instead?

Related

VB.NET Find if file with unknown elements exist?

I need to write an if function that finds if the a specific file exists, but the directory of the file contains unknown elements that may change from occasion to occasion. The code I have so far is:
If Dir(ProjectsFolder & ComboBox_ProjectType.Text & "\" & ProjectNumber & "\" & ProjectNumber & "_Rokasgramata\", ProjectNumber & "*User Manual*.pdf")(0) = "" Then
It returns an error and it's probably because of "*" What am I doing wrong? Could someone, please, help me with this one?
When working with paths use IO.Path.Combine
Use that path to create a New IO.DirectoryInfo(path)
This object has .Exists property to ensure that the directory exists.
Finally use .EnumerateFiles(ProjectNumber & "*User Manual*.pdf").Any() To check if any such file exists.
I was very close and figured it out on my own
If Dir(ProjectsFolder & ComboBox_ProjectType.Text & "\" & ProjectNumber & "\" & ProjectNumber & "_Rokasgramata\" & ProjectNumber & "*User Manual*.pdf") = "" Then

Why i can connect to a sharepoint site with vba from one spreadsheet but not another

I have an odd issue where I can connect and upload files to a sharepoint site using a vba script, however using practically the same vba script from another spreadsheet and uploading to the same sharepoint site I can't connect and upload files.
The weird thing with the vba script that doesn't work is that if I add the below code to it before the rest of the script, the rest of the script works.
xPath= "https://teamspace.healthcare.siemens.com/content/90002613/Documents/"
With ActiveWorkbook
Application.ActiveWorkbook.SaveAs Filename:=xPath & Name & ".xlsm"
Application.ActiveWorkbook.Close False
End With
No idea why but xPath is a valid file path when using the SaveAs command, but when I use the same path or variant of it with the "Dir" tag it doesn't work and either give me a error code "Runtime 52 Bad File name or number" or "Runtime 76 path not found". Please can someone help with this, I have been trying everything I can think of for about the last 2 days
Thanks
Edit :
this is the code that works in one of the spreadsheets
If Dir("//teamspace.healthcare.siemens.com/content/90002613/Documents/GB_Invivo_RSM/" & xWs.Name & "", vbDirectory) = "" Then
MkDir ("//teamspace.healthcare.siemens.com/content/90002613/Documents/GB_Invivo_RSM/" & xWs.Name & "")
Else
End If
With ActiveWorkbook
Application.ActiveWorkbook.SaveAs Filename:=xPath & "\GB_RSM_P" & Format(LDate, "mm") & "FY" & Format(LDate, "yyyy") & " " & xWs.Name & ".xlsx"
End With
The code in the 2nd spreadsheet wont work unless I put another SaveAs() before all of this, and save a dummy spreadsheet, then have to delete it after, because obviously I don't want it there. I can't understand why the same code would work from one spreadsheet and not another, and also its almost like the saveAs() is creating a connection or something, but this wasn't needed in the 1st spreadsheet
If your URL is "https://teamspace.healthcare.siemens.com/content/90002613/Documents/" then you should be able to use Dir() as shown below:
Sub TestWebDAVDir()
Const MY_PATH As String = "\\teamspace.healthcare.siemens.com\content\90002613\Documents\"
Dim f
f = Dir(MY_PATH & "*")
Do While Len(f) > 0
Debug.Print f
f = Dir()
Loop
End Sub

Dir function does not find a file, even after copy pasting the path as a parameter

I've tried to get the Dir() function to work some time now using a rather complicated concatenated string, as seen below:
Dim Path as String
Path = Dir("PathToSubfolder\" & Year(Date) & "\" & MonthName(Month(Date)) & _
"\Production " & MonthName(Month(Date)) & "*.xlsx")
MsgBox Path
The message box prints nothing (it's just a blank Message Box). After trying to figure out whether I had mistyped the Path somehow, I opened up the correct file, and copypasted its actual Path from options, and subsequently performed: Path = Dir("PathToSubFolder\2016\June\Production June 2016.xlsx"), i.e. without any concatenation or anything, simply just the actual filepath and -name. However, Printing MsgBox Path returned nothing (NULL) again.
Does anyone have any clue as to why this wont work? I have used Dir quite extensively the last days from the same workbook (albeit not from the same module) without any issues.
Edit: Finally found a workaround. I simply made a variable with the path to the file, pathtoFile, and subsequently:
Dim pathtoFile As String
pathtoFile = "C:\Path.to.file\"
Path = Dir(pathtoFile & "*" & MonthName(Month(Date)) & "*")
Month(Date)
Will return a number, not a name. So you are passing the following argument:
PathToSubFolder\2016\6\Production 6 2016.xlsx
Which doesn't exist, hence you get a null string returned.
Try
Dim Path as String
Path = Dir("PathToSubfolder\" & Year(Date) & "\" & MonthName(Month(Date)) & _
"\Production " & MonthName(Month(Date)) & "*.xlsx")
MsgBox Path
the MonthName() method takes a number between 1 - 12 and returns the name of that month, which is what you need for your string.

VBA Set Cell value to text containing ' not working

I'm trying to get a macro to type out a heap of formulae for me. The formula goes something like this:
=COUNTIF('other_sheet'!A:A,"hello*")
The user can specify A and other_sheet, so I select the cell and then go
ActiveCell.Value = "=COUNTIF('" & othercell & "'!" & column & ":" & _
column & """,hello*"")"
But it keeps giving me errors like:
1004: Object Defined Error
I have tried using ActiveCell.Text, ActiveCell.Formula etc. and they all don't work.
It needs to be ActiveCell.Formula and ", is the wrong way around next to Hello. It should be:
ActiveCell.Formula= "=COUNTIF('" & othercell & "'!" & column _
& ":" & column & ",""hello*"")"
Apparently you can only put proper, completed answers into a cell.
i.e. if the output turns out to be "=sum(A:A" and a bracket isn't closed, then the application throws an error just as if you typed it in manually.

VBA notation - backslash in quotations

A colleague at work made a macro and I need to edit it, but first I need to figure out how it works.
Sheet2.Pictures.Insert(importPath & "\" & partName & "\" & picName & ".png").Select
This line is (I think) where images are inserted into the excel document. I'm trying to figure out exactly how it works. I know importpath, partName and picName are variables he defined earlier. importPath is self explanatory, I don't know why part name is in there but I know what it is and picName is there because you enter the picture name in a certain cell and it searches for that name in the importPath to insert into the sheet.
Also what confuses me is the & and "\" I have no idea what these do.
Any help out there?
This is compiling a group of variables into a String that represents a path. For example, if:
importPath = "myPics"
partName = "2014"
picName = "flower"
Then the line
importPath & "\" & partName & "\" & picName & ".png"
will create the String
"myPics\2014\flower.png"
which is the full filename being passed to Sheet2.Pictures.Insert
You're constructing a full file path to an image in that line so the & concatenates all the string variables and the "\" is just manually adding the backslash characters you'd expect to see in any windows explorer or command line window.