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.
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.
This question already has answers here:
FileSystemObject.CreateFolder to create directory and subdirectories
(3 answers)
Closed 1 year ago.
Top macro works, bottom one doesn't unless I run the top one first. How do I create folder & subfolder concurrently?
I'm trying to use FileSystemObject.CreateFolder to make a folder and subfolder at the same time. The top macro works and the bottom one dosen't. The bottom macro will successfully run if I run the top one first.
Go ahead guys, make me look like an idiot.
Sub MakeDir1() ' this macro works
Dim DocPath As String
DocPath = "E:\#Workorders\test\" ' E:\#Workorders\ is an existing folder
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
If Not FSO.FolderExists(DocPath) Then
FSO.CreateFolder (DocPath)
End If
End Sub
Sub MakeDir2() ' this macro throws an error at "FSO.CreateFolder (DocPath)"
Dim DocPath As String
DocPath = "E:\#Workorders\test\test2\"
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
If Not FSO.FolderExists(DocPath) Then
FSO.CreateFolder (DocPath)
End If
End Sub
This works:
Sub MakeDir3() ' this macro works
Dim DocPath As String, DocFolder1 As String
DocPath = "E:\#Workorders\test\"
DocFolder1 = "test2"
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
If Not FSO.FolderExists(DocPath) Then
FSO.CreateFolder (DocPath)
FSO.CreateFolder (DocPath & DocFolder1)
End If
End Sub
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!
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:
I'm writing a script where I wish to write an HTML doc to a string from sharepoint.
Dim Content As String
Dim strShare As String: strShare = "\\link\to\share.html"
Dim iFile As Integer: iFile = FreeFile
Open strShare For Input As #iFile
Content = Input(LOF(iFile), iFile)
Close #iFile
However, I find I get a "path/file access error" every time I run the script for the first time upon boot. Once I visit "\link\to\share.html" in IE for the first time, the path begins to resolve in the VBA script.
My only thought is that IE is performing some sort of "DNS Cache" that VBA can't do. Currently my workaround is to catch the error and force the URL to open in IE the first time the script is run. After that, every other HTML file under that share loads fine.
As a test, I tried switching between from what I understand is http:// formatting (forward slash) and WebDAV formatting (\\ formating), and only the backslash separated paths ever work. I also tried to resolve the share to an IP and try it that way, but that never worked.
My last thought is to try mapping the share to a drive letter name and then specifically accessing the share with G:\link\to\mapped\share.html. But I don't see this as an elegant solution, and wonder if it will receive the same error any way.
Is there something blatant that I do not understand about WebDAV, Windows file handling, and VBA file inputs? There's something weird going on under the hood with resolving that shared domain, and I can't seem to debug it.
See if this helps here and an example below that I used.
2 things though: I only worked with Excel files on Sharepoint and I was already logged in there.
Dim oFSO As Object
'Dim oFolder As Object 'if needed
'Dim oFile As Object 'if needed
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("\\sharepoint.site.com#SSL\DavWWWRoot\sites\")
'For Each oFolder In oFolder.SubFolders 'loops through folders
' For Each oFile In oFolder.Files 'loops through files
' 'do stuff
' Next oFile
'Next oFolder
I'm a bit confused about what you want to do. Do you want to check out files from SP and check files back into SP?
Sub testing()
Dim docCheckOut As String
'docCheckOut = "//office.bt.com/sites/Training/Design Admin/Training Plan/adamsmacro.xlsm"
docCheckOut = "http://your_path_here/ExcelList.xlsb"
Call UseCheckOut(docCheckOut)
End Sub
Sub UseCheckOut(docCheckOut As String)
' Determine if workbook can be checked out.
If Workbooks.CanCheckOut(docCheckOut) = True Then
Workbooks.CheckOut docCheckOut
Else
MsgBox "Unable to check out this document at this time."
End If
End Sub
Or...do you want to list files in a SP folder?
Sub ListFiles()
Dim folder As Variant
Dim f As File
Dim fs As New FileSystemObject
Dim RowCtr As Integer
Dim FPath As String
Dim wb As Workbook
RowCtr = 1
FPath = "http://excel-pc:43231/Shared Documents"
For Each f In FPath
'Set folder = fs.GetFolder("C:\Users\Excel\Desktop\Ryan_Folder")
'For Each f In folder.Files
Cells(RowCtr, 1).Value = f.Name
RowCtr = RowCtr + 1
Next f
End Sub
Sub test()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\Excel\Desktop\Ryan_Folder")
'Set colSubfolders = objFolder.SubFolders
'For Each objSubfolder In colSubfolders
Cells(RowCtr, 1).Value = f.Name
RowCtr = RowCtr + 1
'Next
End Sub