I am working on a spreadsheet in which I will keep track of everything that my department 3D prints. I have 3 folders full of one thing each for every part I print: a CAD file, an STL file, and a job folder; all of these contain the same part name which is entered into a cell in the spreadsheet and used for finding all of the files/folders. I like to have my spreadsheet hyperlinked so that I can easily open up any of the three by simply navigating to that part in my spreadsheet.
Now I have the following section of code which takes the name of the printed part from column D, and finds the matching STL in my STL folder, and then hyperlinks it into column U.
For i = 4 To Range("D" & Rows.Count).End(xlUp).Row
'follow through all entries in column D
'--STL------------------------------------------------------------------------------------------------------
If (Len(Cells(i, 4)) > 1) Then
If (Len(Cells(i, 21)) = 0) Then
strFile = Dir$(path5w & Cells(i, 4) & "*.stl")
If (Len(strFile) > 1) Then
Cells(i, 21).Hyperlinks.Add Cells(i, 21), path5 & strFile, TextToDisplay:="STL"
Else
'No file was found that matches so do nothing
End If
Else
'Already hyperlinked, skip this cell
End If
Else
'Not a valid Name, do nothing
End If
Next
End Sub
I simply copied this chunk of code again and switched the path and switched .STL the extension for my CAD files, and it works great for both of those, but I am getting stuck on the job folders... I have no idea how to get my code to find a folder instead of a file.
I have tried playing around with FileSystemObjects, but I don't fully understand how to use them, and all I can find is an example of how to list every folder inside a folder, and not how to actually search for a specific folder.
I also looked at this example: VBA to find multiple files but, again, I run into the problem of not understanding how to use this to search for a folder, rather than listing all folders.
So to help be more clear I will give an example. Lets say I process Part123.stl, when I want to save this, it will create a folder ssys_Part123 and I will save that in my folder named Job Folders. Now I want my program to check cell D4 which says Part123, then navigate to Job Folders, find the folder named ssys_Part123 and hyperlink that folder into V4.
I still don't have a very firm grasp of coding, so any help is always greatly appreciated.
If the folder name is always going to be ssys_[PartName] then you should be able to just concact the strings to link to the folder instead of trying to look up the folder name.
Related
I have created an Excel VBA Macro to connect to SAP GUI, pull data, etc. Nearly everything works, but I have one problem.
I am using the Transaction CV04N to download some documents from it. My code pastes all required Document names (or numbers) into the multiple selection here:
After executing it, we get this list:
Now my code just double clicks the list one-by-one and this opens up:
So, in most cases, there is only one PDF file in here, but sometimes there is also a TIFF file in there, which then produces an error, because the program tries to download it as a .pdf.
However I only want the PDF. But my program always just selects the first entry.
So I need a function/routine that reads what is in the first line, if it is not PDF, then take the next one. (there are never 2 PDFs in there, so taking the first PDF that shows up is sufficient)
If I just choose document, that only contain PDFs, then everything runs normal.
My current code looks like this (starts from the window shown in last picture)
For j = 0 To k - 1
On Error Resume Next
FileName = XYZ
SaveName = DlFolder & FileName
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").currentCellRow = j
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").doubleClickCurrentCell
Set Tree = Session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/cntlCTL_FILES1/shellcont/shell/shellcont[1]/shell")
Tree.selectNode " 1"
Tree.nodeContextMenu " 1"
Tree.selectContextMenuItem "CF_EXP_COPY"
'It has selected the "copy to" in context menu, now just saves it to Folder saved in "Savename"
Session.findById("wnd[1]/usr/ctxtDRAW-FILEP").Text = SaveName
Session.findById("wnd[1]/tbar[0]/btn[0]").press
'goes to next doc:
Session.findById("wnd[0]/tbar[0]/btn[3]").press
Next
So, if I could get the data from the Table, I could select the one with TIFF, for that I need to read the table.
I have tried
Tree.Text
Tree.Value
Tree.Copy (and then paste in Excel)
But nothing gives me the correct value...
When I select the entry, and press CTRL-C and paste it somewhere it gives me the whole line, so a String with all columns in this entry.
If you have a solution just to get that mentioned String into a Excel Cell, that's fine with me! From there I can set up some routine to make it work.
I hope I made it understandable what I want, if not please feel free to reach out to me!
I have query regarding creation of folder based on selection. VBA code seems to be working whoever i want to make more effortless. Here are some issues
Reference Number in Column D is actually a hyper link, which has been given a friendly or should i say face name in order to keep sheet clean for its users. VBA codes seems to be not working because i have friendly or face name for the link.
in order to run code successfully On other hand what i did i create duplicate of column D but without giving any face name to links. this allowed to run vba code successfully. however problem with this is when select cell with link i need to press create folder button to create folder each time. what i want is instead of click create button it should create folder if a cell with link is selected.
it would be great if someone could provide a fix for issue No. 1, where folder should be created, on selection of cell from column D containing link but as well as face name.
For ease of understanding, see below table to understand the structure of my sheet:
A:1 Contains Default Starting Name P195-HCT
B:1 Contains Default path (\Desktop\HCT)
A:5 to A:100 contains Revision No.
B:5 to B:100 contains Serial number for Folder
C:5 to C:100 contains formula which is (=HYPERLINK($B$1&B5&" "&"R"&A5,$A$1&B5&" "&"R"&A5)
Code:
Sub MakeSekectionDir()
For Each x In Selection
If x = "" Then
ElseIf Len(Dir(x, vbDirectory)) = 0 Then
MkDir x
End If
Next x
End Sub
What I want is this:
A folder should be created, on selection of cell from column D containing link but as well as face name.
The VBA code should be able to create a folder if any cell from C:5 to C:100 is selected.
I currently use a spreadsheet where I need to manually hyperlink 4 separate files in each row which include important information for referencing (3 are PDFs and 1 is an excel spreadsheet). Each row is full of information for a specific order number, which is conveniently part of each file that I need to hyperlink. Each of the 4 types of documents I would like to hyperlink into my excel sheet are also all packed into 4 folders path1, path2, path3, and path4, and they have their own column in the spreadsheet. Each folder will only contain one file with the order number.
I tried to automate this in excel only, using these formulas in the 4 columns:
K2 =IF(ISBLANK(C2)," ",HYPERLINK("J:path1\"&C2&".pdf",C2))
L2 =IF(ISBLANK(C2)," ",HYPERLINK("J:path2\"&C2&".pdf",C2))
M2 =IF(ISBLANK(C2)," ",HYPERLINK("J:path3\"&C2&".xlsx",C2))
N2 =IF(ISBLANK(C2)," ",HYPERLINK("J:path4\"&C2&".pdf",C2))
The formula references cell C2 which is the order number, and it fills the path with that number, which works great for the files which are consistently named. It also leaves the cell blank if there is no order number, because it happens sometimes and I need the function to not freak out when there is nothing there. The problem comes when I run into the file names that have other things tacked on the end such as a date. My formula is incapable of hyperlinking a file unless I give it the exact path to begin with.
I am wondering if anyone knows if excel is even capable of finding a file in a folder when only given part of the file name.
If there is not a way to do this in excel, I was hoping there may be a way to do this with VBA. I did some searching and found the Application.FileSearch feature in VBA, but it says "Object doesn't support this action." when I try to call it. (Which from a simple google search that seems to be the error due to Application.FileSearch not existing in excel 2007, but I am running 2013, so I'm not sure why this is happening)
I have a very novice understanding of VBA, so I am trying to slowly learn on the side. If anyone could help me come up with a code that would allow me to reference a cell, and find files containing that name so that I could print that path to a different cell, I would greatly appreciate your help.
Something like this should work (or at least point you down a path to investigate).
Sub HyperlinkFiles()
Dim strFile As String
strFile = Dir$("J:\path1\*" & Sheet1.Range("C2") & "*.pdf")
If (Len(strFile) > 1) Then
Sheet1.Range("k2").Hyperlinks.Add Sheet1.Range("k2"), strFile
Else
'No file was found that match so do nothing
'However, you could link to the folder to make manually searching easier
'Sheet1.Range("k2").Hyperlinks.Add Sheet1.Range("k2"), "J:\Path1"
End If
strFile = Dir$("J:\path2\*" & Sheet1.Range("C2") & "*.pdf")
If (Len(strFile) > 1) Then
Sheet1.Range("L2").Hyperlinks.Add Sheet1.Range("L2"), strFile
Else
'No file was found that match so do nothing
End If
strFile = Dir$("J:\path3\*" & Sheet1.Range("C2") & "*.xlsx")
If (Len(strFile) > 1) Then
Sheet1.Range("M2").Hyperlinks.Add Sheet1.Range("M2"), strFile
Else
'No file was found that match so do nothing
End If
End Sub
The caveat with this code is when there are 2 or more files that match the search pattern. For example, suppose cell C2 contains Stack and you have 2 files named stackoverflow.pdf and stackexchange.pdf. Which "stack" file do you want?
The setup
I have an Excel workbook stored on my hard drive.
The structure is such that on the first sheet I have a list of the names of the other sheets in the same workbook (...which can be created or deleted).
All the names on the list, on the first sheet, are supposed to be hyperlinks to the corresponding sheet in the workbook. So, by clicking the name on the first sheet you jump to the corresponding sheet.
When a new sheet is created a macro creates also the new name on the list on the first sheet and makes a hypelink of it. This works.
...BUT...
The links point to the stored version of the file, not to the open workbook! Clicking the links opens the stored file and not the one which is under work.
QUESTION: How to create a hyperlink that always points to the same open workbook and not to the stored copy of it?
Try this:
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
"Sheet3!", TextToDisplay:="Link to sheet #3"
Address is the URL and SubAddress is a localtion of the page (or a sheet or a range in excel workbooks).
You may try creating the hyperlink as a formula (via VBA, as you need).
I am using the parameters you posted, this may need a little adjustment.
Dim rngsrc as Range, rngtrgs as String
Set rngsrc = Worksheets("Summary").Cells(Cell.Row, 5)
Set rngtrg = "'" & sSheetName & "'!B5"
rngsrc.FormulaR1C1 = "=HYPERLINK(" & rngtrgs & "," & sSheetName & ")"
See
Official documentation
Example 1
Example 2
This (hopefully) answers your question. As a separate note, it still remains to be clarified why you see the behavior you see.
It seems that the problem comes from the following fact: My file is in a SharePoint folder. If I open it just for reading, the hyperlinks work fine. If I open the file for editing, a copy of the SharePoint file is placed on my hard disk, on a specified location. So, the path to the file is not the same as it would be if I open it read-only. Should I use hyperlink.follow to solve this?
So, this all comes down to the question: In VBA/Excel, can I create a hyperlink which always points to a location in the same opened file so that the hyperlink ignores the storage path of the corresponding file? Using empty string (or BLANK) doesn't help as the address parameter in hypelinks.add as Excel seems to automatically fill in the whole storage path.
I upgraded to Office 2013 and "PADAM": The problem vanished! It seems that there was a bug in Excel/VBA in this in the 2007 version.
I need to perform a batch modification of metadata because my father wants to place a "title" in all .jpg images in a folder.
I told him to look into the photos and write down in a Excel spreadsheet the information to write down in order. One cell one file.
So I made the loop to read the spreadsheet but I don't know which file objects I need to use with the .jpg files and which methods can read or write a metadata tag.
MSDN gave no help for this.
A bit of code
Sub seleccionaYmuesrtra()
'in the first column there are the file names.
'in the second column there are the titles written manually
Dim image As ????
???? Set image = New Bitmap("c:\FakePhoto.jpg")
Cells(1, 1).Activate 'I start in the first cell.
Dim contenido, ruta, nombre As String 'some variables
ruta = "C:\imagesToUseinFolder"
While (Not IsEmpty(ActiveCell)) 'this goes down in the first column until the first empty cell.
nombre = ruta & ActiveCell.Value & ".jpg"
contenido = ActiveCell.Offset(1, 0).Value
'------------------------------------
'HERE GOES THE ACTION I DON'T KNOW
...
'------------------------------------
ActiveCell.Offset(1, 0).Activate 'MOVE ONE DOWN
Wend
End Sub
I'm working in my laptop with windows7 but I think it should work in my parents desktop computer with XP.
Thank you for your time, I hope that was not answered yet.
You could try invoking a tool as a command-line command (if you need help invoking, see Execute a command in command prompt using excel VBA)
Exiftool is a good free, standalone executable that should do what you want:
http://www.sno.phy.queensu.ca/~phil/exiftool/