I am trying to correct this code:
Dim ab2 As String
ab2 = Worksheets("Sheet1").Range("b2").Value
Dim ObjFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = ObjFSO.GetFolder(ab2)
Call getfiledetails(objFolder)
End Sub
My Target is to assign the value of cell b2 (which is a folder location) to my
objFolder.ObjFSO.GetFolder(ab2)
But I am having the "Path Not Found" Error.
I also used objFolder.ObjFSO.GetFolder&ab2 but I get a different error message.
Any idea on how to correct this?
Thanks!
Related
I'm trying to convert pdfs into Excel spreadsheets. I have already added the "Microsoft Scripting Runtime" reference. Help?
Option Explicit
Sub PDF_To_Excel()
Dim setting_sh As Worksheet
Set setting_sh = ThisWorkbook.Sheets("Setting")
Dim pdf_path As String
Dim excel_path As String
pdf_path = setting_sh.Range("E11").Value
excel_path = setting_sh.Range("E12").Value
Dim fso As New FileSystemObject
Dim fo As Folder
Dim f As File
......
However I keep getting a
invalid procedure call or argument error
for Dim fso As New FileSystemObject
Help?
I always use the extended syntax:
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
It has never failed for me.
I have a VBScript for zipping up old IIS log files. I keep getting this error though:
Microsoft VBScript runtime error: ActiveX component can't create
object: 'GetObject'
This is the line it errors on:
Set objIISOuter = GetObject("IIS://LOCALHOST")
I am unsure of what this means.
Tried what I found here and I wasn't able to get anything running with 32 or 64 bit.
I read somewhere that it could be a problem with a DLL not being registered but I don't know how this could be an issue here, might be wrong though.
For Each objWebOuter in objIISOuter
If LCase(objWebOuter.Class) = "iiswebservice" Then
Set objIIS = GetObject("IIS://LOCALHOST/W3SVC")
For Each objWeb in objIIS
If LCase(objWeb.Class) = "iiswebserver" Then
Call DeleteLogFiles( _
objWeb.LogFileDirectory & "\W3SVC" & objWeb.Name, _
intZipAge, intDelAge)
End If
I'm an admin so permissions aren't the issue. Any ideas?
Here are two potential approaches:
Use the FileSystemObject to get the LogFiles folder and delete files:
sLogFolder = "%SystemDrive%\inetpub\logs\LogFiles"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(sLogFolder)
For Each objSubfolder In objFolder.SubFolders
DeleteFiles objSubfolder.Path, 10
Next
Another approach:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objIIS = GetObject("winmgmts:root\WebAdministration")
Set objSites = objIIS.InstancesOf("Site")
For Each objSite In objSites
DeleteFiles objSite.LogFile.Directory & "\w3svc\" & objSite.ID, 10
Next
Both approaches use the following Sub to delete the files from a folder:
Sub DeleteFiles(p_sFolder, p_iMaxAge)
Dim objFSO
Dim objFolder
Dim objFile
Dim iFileAge
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(p_sFolder)
If objFolder Is Nothing Then Exit Sub
For Each objFile In objFolder.Files
iFileAge = Now - objFile.DateCreated
If iFileAge > (p_iMaxAge) Then
objFSO.DeleteFile objFile, True
End If
Next
End Sub
I cannot figure out why the following code is throwing a compile error with the message "user defined type not defined". It is highlighting Set fso = FileSystemObject
Sub S()
Dim fso As FileSystemObject
Dim ts As TextStream
Dim i As Integer
Dim myCell As Range
Set fso = FileSystemObject
For i = 0 To TotalColumnNumber
' last argument, True, says to create the text file if it doesnt exist, which is
' good for us in this case
Set ts = fso.OpenTextFile("column_" & i, ForWriting, True)
' set mycell to the first cell in the ith column
Set myCell = SheetName.Cells(1, i)
' continue looping down the column until you reach a blank cell
' writing each cell value as you go
Do Until myCell.Value = ""
ts.writeline myCell.Value
Set myCell = myCell.Offset(1, 0)
Loop
ts.Close
Next
Set ts = Nothing
Set fso = Nothing
End Sub
thanks
The code has many several problems:
You need to set a Reference
use: Set fso = New FileSystemObject
establish values for your variables before you use them; this includes TotalColumnNumber, SheetName, etc.
Are you referencing the correct namespaces in Tools/References?
Sub tryMethod()
Dim objTxt as textstream
Dim filename as string
fileName = "Z:\New folder\TextDoc.txt"
Set fSo = New Scripting.FileSystemObject
Set objTxt = fSo.OpenTextFile(fileName, ForReading)
str = objTxt.WriteBlankLines(1)
End Sub
No matter what number I put into the brackets after calling method writeblanklines I get the following error:
expected function or variable
I have checked documentation and do not see an example for this method. First two pages of google also didn't give me an example to work off of.
You have opened the file for reading Set objTxt = fSo.OpenTextFile(fileName, ForReading) and you are trying to write.
This is how to open it for writing:
Sub TestMe()
Dim objTxt As TextStream
Dim fso As Object
Dim filename As String
filename = "C:\Users\User\Desktop\nd.txt"
Set fso = New Scripting.FileSystemObject
Set objTxt = fso.OpenTextFile(filename, ForWriting)
objTxt.WriteBlankLines 23
End Sub
The MSDN documentation (from #braX comment) is not as good as one would expect - the ForWriting constant is present only in the example:
However, the ForWriting is present in the GitHub, maybe one day when the MSDN and the GitHub would be sync-ed it will be there as well:
Does any one know why the
.writeblanklines 3
does not work? I've tried plenty of different ways and it always seems to just overwrite the line in the textfile.
Private Sub CommandButton3_Click()
Dim fso As FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fsofolder As Folder
Set fsofolder = fso.GetFolder("U:\files")
Dim file1 As file
Dim a As Integer
Dim b As String
b = "U:\files\" & ListBox1.Value
fso.OpenTextFile(b, ForWriting).WriteBlankLines 3
fso.OpenTextFile(b, ForWriting).WriteLine (TextBox3.Value)
See: http://msdn.microsoft.com/en-us/library/aa265347(v=vs.60).aspx
If you want to append content, then use ForAppending when opening the file.