I am trying to pass a collection of Folder objects in a VBA module, but it consistently performs an implicit cast of the objects to String type
Function GetFolderFiles(ByRef dir As Object) As Collection
Dim files As Collection
Set files = New Collection
For Each file In dir.files
Debug.Print TypeName(file)
files.Add (file)
Next frile
For Each subfolder In dir.SubFolders
Dim sf As Object
Set sf = subfolder
Debug.Print TypeName(sf)
Dim tmpfiles As Collection
Set tmpfiles = GetFolderFiles(sf)
For Each File In tmpfiles
files.Add (File)
Next File
Next subfolder
Set GetFolderFiles = files
End Function
Here is my test code:
Function TestGetFiles()
Dim fso As Object
Dim d As Object
Dim files As Collection
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetFolder("C:\Users\X\Documents\My Web Sites")
Set files = GetFolderFiles(d)
For Each f In files
Debug.Print TypeName(f)
Next f
End Function
As you can see, I'm printing out the TypeName() call's results. When I step through the functions, I get the expected type "File" within GetFolderFiles, but when I iterate over the returned collection, the type of the objects is "String".
1) How do I get a collection of File objects back from my function?
2)More generally, How do I prevent the implicit conversion of Objects to Strings?
There's a difference between f and (f), as illustrated in the code below:
Function TestGetFiles()
Dim fso As Object
Dim d As Object, f As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetFolder("C:\_Stuff\test")
For Each f In d.files
Debug.Print TypeName(f), TypeName((f)) '>> File String
Next f
End Function
f is the file itself, but wrapping f in parentheses causes that expression to be evaluated (resulting in a string: I'm guessing the default property of File is its Name)
and the result of the evaluation is what gets added to your collection.
Fix: don't use
files.Add (File)
but
files.Add File
Hint: any time the VB editor puts a space before your opening ( means you need to be sure you meant to use those parens. Basically be careful when using parens in a method call unless you're using the Call keyword.
Related
In my never ending story to learn VBA I am trying to create a macro that deletes files based on the files starting characters and unsure how to proceed.
I have an excel file with numbers in column a ,these numbers are either 4,5 or 6 digits.
I have a file folder with files which may or may not start with these digits from a range from excel file. These files in folders are of different types
But I reckon this may not be an issue still,the naming convention is as follows : ie. 4563_listofitems.pdf,65475_skusdec.doc etc.
My goal is to loop through files and check if the starting characters of the file are on included in the A range of the excel sheet,if so (there may be up to 6 files starting with such number) create a folder named with the found starting characters and move the files starting with these characters into the folder,else if file doesn't start with fixed characters from the list then just delete (kill) that file. My issue is idk how to check the files names against the list.
My code as now for looping trough
Sub loopf
Dim filen as variant
Filen =dir("c:\test\")
While filen <>""
If instr(1,filen,10000)=1 then
'Here I want check against the values from range but unsure how ,should I somehow loop through the range ?
Filen=dir
End if
Wend
End sub
To check if a value is contained within a known list, I like using the Dictionary Object. It has the function Exists which checks if a value is listed within the Dictionary.
So before you loop through the files, you just need to add every one of your accepted numbers into the dictionary. Then while looping though the files check if Dictionary.Exists(Value). If it exists, then the value is good, if not then Kill.
Here's how I would set that up:
Sub loopf()
Dim AcceptedPrefixes As Object
Set AcceptedPrefixes = CreateObject("Scripting.Dictionary")
Dim PrefixRange As Range
Set PrefixRange = ThisWorkbook.Sheets(1).Range("A1:A5")
Dim Cell As Range
For Each Cell In PrefixRange.Cells
If Cell <> "" And Not AcceptedPrefixes.exists(Cell.Value) Then
AcceptedPrefixes.Add CStr(Cell.Value), 0
End If
Next
Dim Directory As String
Directory = "c:\test\"
Dim filen As Variant
filen = Dir(Directory)
While filen <> ""
Dim FilePrefix As String
FilePrefix = Split(filen, "_")(0)
If Not AcceptedPrefixes.exists(FilePrefix) Then
Kill Directory & filen
End If
filen = Dir
Wend
End Sub
Sub Files()
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\test")
For Each oFile In oFolder.Files
'do somthing
Next oFile
End Sub
I have a macro files with file extension of .DO Files(.DO). I Open a file through vb.net
application and to also open a macro. by using that macro I need to format that word Document. I
tried a lot but no use . I try to Select some area of word file but it will leads to error:
Object reference not set to instance of object
Private sub beginFormatting
ls_inipath = System.Windows.Forms.Application.StartupPath & "\"
ls_Document = GetIniValue("Remove_Pages", "doc_name", txtFileName.Text)
Dim what As Object = Word.WdGoToItem.wdGoToLine
Dim which As Object = Word.WdGoToDirection.wdGoToLast
Dim SelectionOne As Selection
Dim returnValue As Range = SelectionOne.GoTo(what, which, Nothing,
Nothing)
SelectionOne.EndKey(WdUnits.wdStory, WdMovementType.wdMove)
end sub
In MS Outlook (2016) I am working on a VBA procedure to more quickly archive certain, individually selected e-mails into certain folders in my e-mail archive.
I have a procedure that does the trick when I address the target folder manually:
'[...]
Dim MoveToFolder As Outlook.MAPIFolder
'[...]
Set MoveToFolder = ns.Folders("Archive").Folders("Projekte-Archiv").Folders("P03_NetRef")
'[...]
With this the procedure knows what folder to move pre-selected e-mail to.
Now my problem:
I am trying to set the "MoveToFolder" folder object through a string variable giving it all the necessary data.
Why do I want to do this: Handing over the folder data as a string variable would allow me to use the same procedure for as many folders in as many hierarchy levels I want.
Here is what I came up with, using the CType function:
'[...]
Dim MoveToFolder As Outlook.MAPIFolder
'[...]
Set MoveToFolder = CType("ns.Folders(""Archive"").Folders(""Projekte-Archiv"").Folders(""P03_NetRef"")", Outlook.MAPIFolder)
'[...]
(The idea is of course in a next step to insert the string through a variable, not in plain writing like the example.)
This does not work. The object type 'Outlook.MAPIFolder' results in an error on compiling ("method or data object not found").
Later insight
As I understood later on, the CType() function is not available in VBA (as opposed to VB.net).
Untested:
Set MoveToFolder = GetFolder(ns, "Archive|Projekte-Archiv|P03_NetRef")
A function to parse the path:
Function GetFolder(root, fpath)
Dim f As Object
Dim arr, i
arr = Split(fpath, "|")
For i = 0 To UBound(arr)
If i = 0 Then
Set f = root.Folders(arr(i))
Else
Set f = f.Folders(arr(i))
End If
Next i
Set GetFolder = f
End Function
I am trying to optimize a previous vba automation in microsoft word that i wrote which loops through files (scientific articles) of some type (rtf /doc/docx) and extract a list of all the words in each file, then it compares this list of words with another list of commonly used words (6000 words or so) in order to exclude the common words in those files and obtain the less frequent ones, then the user has the choice to export and/or highlight these less common words see the pic below:
now, i wrote recursive function that list files types (doc or docx or rtf) in a folder using shell object since i read its faster than file system object tho i haven't tested the performance of both , the code below shows the function when i use early binding which works fine
Sub test_list()
Dim t As Double
t = Timer
Call ListItemsInFolder("C:\Users\Administrator\Desktop\", False)
Debug.Print Timer - t
End Sub
Function ListItemsInFolder(FolderPath As String, LookInSubFolders As Boolean, Optional ByVal SearchedFileType As String = ".docx")
Dim PathsDict As Object
Set PathsDict = CreateObject("Scripting.Dictionary")
Dim ShellAppObject As New Shell
Dim fldItem As ShellFolderItem
Dim i As Long
i = 0
'----------------------------------------------------------------------------------------------------------------------
'Shell's Namespace object holds onto many different and useful properties that can be used to extract information
'In this code we have used its FileSystemObject equivalents
'----------------------------------------------------------------------------------------------------------------------
With ShellAppObject.NameSpace(FolderPath)
For Each fldItem In .Items
'----------------------------------------------------------------------------------------------------------------------
'The code tends to error when it comes across a zip file which in turn may contain a folder. The code then gives you
'an RTE so to bypass this possibility we use following check of verifying .zip
'----------------------------------------------------------------------------------------------------------------------
'vbTextCompare ==> negelct case sensitivity
Select Case InStr(1, fldItem.Parent, ".zip", vbTextCompare)
Case 0 'its not a zip file
'check if the current item is a folder
If (fldItem.IsFolder) Then 'the item is a folder
'to get the folder path use
'Debug.Print fldItem.Path
'to get the folder name use
'Debug.Print fldItem.Name
Else 'the item is a file
'check if the file is (docx/doc/rtf/txt) accoriding to func input
Select Case InStr(1, fldItem.Name, SearchedFileType, vbTextCompare)
Case Is > 0
'add those files to the dictionary
PathsDict.Add Key:=i, Item:=fldItem.Path
i = i + 1
'to get the parent folder path
'Debug.Print Left(fldItem.Path, InStrRev(fldItem.Path, fldItem.Name) - 2)
'to get the file name
'Debug.Print fldItem.Name
'to get the file path
'Debug.Print fldItem.Path
Case 0
'neglect other file types
End Select
End If
'pass the folder item as a subfolder to the same function for further processing
If fldItem.IsFolder And LookInSubFolders Then ListItemsInFolder fldItem.Path, LookInSubFolders
Case Else 'its a zip file
'do nothing and bypass it
End Select
Next fldItem
End With
ListItemsInFolder = PathsDict.Items
Set ShellAppObject = Nothing
Set PathsDict = Nothing
End Function
now, when i try to use the late binding, i get an error "object variable or with block variable not set" ... the error appears at the last line of the following :
Function ListItemsInFolder(FolderPath As String, LookInSubFolders As Boolean, Optional ByVal SearchedFileType As String = ".docx")
Dim PathsDict As Object
Set PathsDict = CreateObject("Scripting.Dictionary")
Dim ShellAppObject As Object
Set ShellAppObject = CreateObject("Shell.Application")
Dim fldItem As Variant 'used to loop inside shell folders collection
Dim i As Long
i = 0
'----------------------------------------------------------------------------------------------------------------------
'Shell's Namespace object holds onto many different and useful properties that can be used to extract information
'In this code we have used its FileSystemObject equivalents
'----------------------------------------------------------------------------------------------------------------------
With ShellAppObject.NameSpace(FolderPath)
and the variable "fldItem " is empty. What am I missing?
As far as I can see it is because the index to NameSpace is not actually defined as a String. FolderPath is already a string, and using
"" & FolderPath & ""
does not add quotation marks around it - to do that in VBA, you would need
""" & FolderPath """
What NameSpace really seems to want is a Variant (although the Object viewer does not spell that out), and if you use
With ShellAppObject.NameSpace(FolderPath)
it doesn't seem to get one. If you do anything to the string as you pass it, e.g.
With ShellAppObject.NameSpace(FolderPath & "")
or
With ShellAppObject.NameSpace(cStr(FolderPath))
VBA seems to allow it.
Or you could do
Dim v As Variant
v = FolderPath
With ShellAppObject.NameSpace(v)
Your string variable is the problem...for ShellAppObject.NameSpace to work the path needs to be a folder path with quotations ... "C:\Windows" rather than C:\Windows which is what is being passed with the string variable. Also I think you need to instantiate the folder object before using in With ... End With.
Working script below:
Sub test_list()
Dim t As Double
t = Timer
Call ListItemsInFolder("c:\windows", False)
Debug.Print Timer - t
End Sub
Function ListItemsInFolder(FolderPath As String, LookInSubFolders As Boolean, Optional ByVal SearchedFileType As String = ".docx")
Dim PathsDict As Object
Dim ShellAppObject As Object
Dim objFolder As Object
Dim fldItem As Object
Dim i As Long
Set PathsDict = CreateObject("Scripting.Dictionary")
Set ShellAppObject = CreateObject("Shell.Application")
Set objFolder = ShellAppObject.Namespace("" & FolderPath & "")
i = 0
'----------------------------------------------------------------------------------------------------------------------
'Shell's Namespace object holds onto many different and useful properties that can be used to extract information
'In this code we have used its FileSystemObject equivalents
'----------------------------------------------------------------------------------------------------------------------
With objFolder
For Each fldItem In .Items
'----------------------------------------------------------------------------------------------------------------------
'The code tends to error when it comes across a zip file which in turn may contain a folder. The code then gives you
'an RTE so to bypass this possibility we use following check of verifying .zip
'----------------------------------------------------------------------------------------------------------------------
'vbTextCompare ==> negelct case sensitivity
Select Case InStr(1, fldItem.Parent, ".zip", vbTextCompare)
Case 0 'its not a zip file
'check if the current item is a folder
If (fldItem.IsFolder) Then 'the item is a folder
'to get the folder path use
'Debug.Print fldItem.Path
'to get the folder name use
'Debug.Print fldItem.Name
Else 'the item is a file
'check if the file is (docx/doc/rtf/txt) accoriding to func input
Select Case InStr(1, fldItem.Name, SearchedFileType, vbTextCompare)
Case Is > 0
'add those files to the dictionary
PathsDict.Add Key:=i, Item:=fldItem.Path
i = i + 1
'to get the parent folder path
'Debug.Print Left(fldItem.Path, InStrRev(fldItem.Path, fldItem.Name) - 2)
'to get the file name
'Debug.Print fldItem.Name
'to get the file path
'Debug.Print fldItem.Path
Case 0
'neglect other file types
End Select
End If
'pass the folder item as a subfolder to the same function for further processing
If fldItem.IsFolder And LookInSubFolders Then ListItemsInFolder fldItem.Path, LookInSubFolders
Case Else 'its a zip file
'do nothing and bypass it
End Select
Next fldItem
End With
ListItemsInFolder = PathsDict.Items
Set ShellAppObject = Nothing
Set PathsDict = Nothing
End Function
I've tested your code on my side - and I get the same error if the folder does not exist
When this happens, the type that ShellAppObject.NameSpace(FolderPath) returns is Nothing instead of a ShellFolderItem or Object/Folder3
You can use the following check to prevent the "With" block from working with a "Nothing" object:
If ShellAppObject.NameSpace(FolderPath) Is Nothing Then
Debug.Print FolderPath & " does not exist! (or insufficient access permissions)"
Else
With ShellAppObject.NameSpace(FolderPath)
' Your original code here...
' ...
End With
End If
Hope this helps.
I found a problem with the VBA Locals Window while debuggin a macro in Excel 2013. I used the Locals Window to track the value of a string.
If the string exceed a certain length (about 100 caracters) it is not possible to copy it out of the Locals Window to examine it somewhere else (eg. in notepad++).
Is there a way to access the full content of a string variable at runtime ?
You can also dump the output to a txt file.
"response" is the variable to output in the below example.
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Set oFile = fso.CreateTextFile("C:\Development\DebugOutput.txt")
oFile.WriteLine response
oFile.Close
Set fso = Nothing
Set oFile = Nothing