I retrieved a HTML file that I want to save using a textstream object from FileSystemObjects and it produces an empty file.
When I use MsgBox to display the stream right before the write command it shows all the HTML code I want, but it doesn't write the code to the file. Any suggestions?
Dim FSO As FileSystemObject
Dim FSOFile As TextStream
Dim FilePath As String
FilePath = "C:\myhtml.html"
Set FSO = New FileSystemObject
Set FSOFile = FSO.OpenTextFile(FilePath, 2, True)
FSOFile.Write myContent ' String object holding the HTML textstring
FSOFile.Close
I basically am trying to export my gmail inbox folder which I want to use as an event listener (so I can tell another machine what it is supposed to do.) .
On your comments so far:
- riteLine does not make any difference
- OpenTextFile creates non-existing files and it also creates the file and writes any other string that I manually type in, s.a. FSOFile.WriteLine "Nothing works"
- For completeness: CreateTextFile also did not help.
Any likely problem of writing the HTML code using the FSO stream?
Try WriteLine instead of just Write.
See Example: http://msdn.microsoft.com/en-us/library/aa242706%28v=vs.60%29.aspx
Related
In my Access database, I have a button on a form to open an external file.
Here is the code that I am using for that.
Private Sub btn_OpenFile_Click()
Dim a As New Shell32.Shell
Dim strPath As String
strPath = Me.Attachment
strPath = Chr(34) & strPath & Chr(34)
Call a.ShellExecute(Me.Attachment)
'Call CreateObject("Shell.Application").ShellExecute(strPath)
'MsgBox strPath
End Sub
The problem that I have is if I actually put in the value of the variable (Me.Attachment) it works fine and opens the program and the file.
For Example, If I put in Call a.ShellExecute("C:\Docs\Some File.pdf") it will open. But if I use the variable in it's place it won't open and tells me it cannot find the file. I have verified with the msgbox that it is receiving the correct information. I have tried to wrap it in quotes and have used the Chr(34) as shown above but nothing works.
How can I get that variable to work in the ShellExcute command?
I have looked through all the forums and it seems like everyone is using a string but not a variable. I don't want to use just the shell command as I don't want to track down all of the different apps people use to open different types of files. There will be different file types that will need to be opened and I thought this would be easier than it actually is.
Thank you for the help.
I'm pretty sure that ShellExecute expects a Variant parameter, not a String.
So try this:
Call a.ShellExecute(CVar(strPath))
or use a Variant variable from the start.
I had the same problem here.
Both of the following work for me:
Dim a As New Shell32.Shell
Dim strPath As String
strPath = Me.Attachment
Call a.ShellExecute(strPath)
Dim a As Shell
Dim strPath As String
strPath = Me.Attachment
Set a = CreateObject("Shell.Application")
a.ShellExecute strPath
Even referencing Attachment directly.
a.ShellExecute(Me.Attachment)
We use PcSchematic for our electric drawings. And we use PDM to manage the revision. I would like to write into the pcschematic file with the current revision from PDM.
A pcSchematic file can be opened with a notepad. so what I would like to do is:
Dim filePath as string = "C:\16368.04.PRO"
Open file with notepad Process.Start("notepad", filePath)
find and replace a line
save
close
I have tried this:
Dim objFSO
Dim objTS
objFSO = CreateObject("Scripting.FileSystemObject")
objTS = objFSO.OpenTextFile(filePath, 2)
From this Text file in VBA: Open/Find Replace/SaveAs/Close File
but it only seems to work for .txt files
Can anyone point me in the right direction?
I am automating a web page extraction and writting the contents to a text (HTML) file.
For that I set up a File System Object like this
Dim myHTMLfilepath As String
myHTMLfilepath = "C:\temp\MyFile.html"
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim myHTMLFile As Object
Set myHTMLFile = fso.createtextfile(myHTMLfilepath)
When I try to write the extracted content to the file sometimes I get an error 5 (invalid parameter). Here is th code:
myHTMLFile.writeline objIE.document.getElementsByClassName("cool-box")(0).innerHTML
It breaks when the length of the innerHTML is somewhere between 25800 and 28000 (I haven't yet figured the exact limit).
Does anyone know if the WriteLine limit can be increased or advise on a different way to do this?
Assuming the .innerHTML can successfully be read into a string (split up reading/writing to find out), you should be able to use an ADODB.Stream to write it to the file. WriteLine is intended to write a single line of text to a file, not a whole entire document.
Dim contents As String
contents = objIE.document.getElementsByClassName("cool-box")(0).innerHTML
With CreateObject("ADODB.Stream")
.Open
.Type = 1
.Write contents
.SaveToFile myHTMLfilepath, 2
.Close
End With
Hello I am using SendGridMessage() object with VB.net to send emails through SendGrid SMTP server.
I have a .mht file that i want to send in the mail body...
I know that is possible to send pure html in a mail body but when i read the MHT file and put it on the mail body, it appears all messed up like this:
And i wanted to look it like this:
This is my code:
Dim myMsg As New SendGridMessage()
myMsg.AddTo("email#email.com")
myMsg.From = New MailAddress(ApiEmail, ApiUserName)
myMsg.Subject = "Test with MHT file"
myMsg.Html = ""
Dim fso As New FileSystemObject
Dim ts As TextStream
'Open file.
ts = fso.OpenTextFile(sPath)
'Loop while not at the end of the file.
Do While Not ts.AtEndOfStream
myMsg.Html += ts.ReadLine
Loop
'Close the file.
ts.Close()
Dim credentials = New NetworkCredential(ApiUser, ApiKey)
Dim transportWeb = New Web(credentials)
transportWeb.DeliverAsync(myMsg)
You need to convert the .MHT file to regular HTML first to use it in this way. MHT contains metadata and is structured differently than HTML, so you can't use it in a parameter that expects HTML. MHT is more like a MIME message. If you want to deal with MIME via MHT, then sending over SMTP will be easier.
In Excel 2010 VBA, I'm using the FileExists property of the FileSystemObject to check whether a file exists. On my computer it works fine. But on another Excel 2010 computer, it reports that the file is not there when in fact we see in Windows Explorer that the file is there. In both cases, the file being checked is on the local hard drive.
I'm checking for the file right after using Shell to unzip the file. I use DoEvents after the unzip. The file is being extracted to the same folder that the zip file is in.
Here's the code:
Dim oShell As Object, oZippedFile As Object, oUnzipTargetFolder As Object
Dim FSO As Object, oFile As Object, oFolder As Object
Dim strZipfileFullpath As Variant, strTargetFolderpath As Variant, strTargetFullpath As Variant 'NOT AS STRINGS!! (Shell error if strings)
Dim bFileCheckFsoFileExist As Boolean
Set oShell = CreateObject("shell.application")
Set FSO = CreateObject("scripting.filesystemobject")
strZipfileFullpath = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)
Set oFile = FSO.GetFile(strZipfileFullpath)
Set oFolder = oFile.ParentFolder
strTargetFolderpath = oFolder.Path & Application.PathSeparator
strTargetFullpath = strTargetFolderpath & oShell.Namespace(strZipfileFullpath).Items.Item(0).Name
oShell.Namespace(strTargetFolderpath).CopyHere oShell.Namespace(strZipfileFullpath).Items.Item(0) 'this zip has only one file.
DoEvents
bFileCheckFsoFileExist = FSO.FileExists(strTargetFullpath)
Any ideas why this works fine on one computer, but on another computer reports that the file is not there even though it we see that it clearly is?
UPDATE:
I thought I'd add this note in case it may be helpful for others running into the same snag.
The basic underlying issue was that when using Shell to extract a zipped file, the Name property of the zipped file object does not include the extension If Windows Explorer is hiding extensions and the file has a registered extension. For example:
Dim strGofnZipfileFullpath As Variant
Dim oShell As Object, oShellZipfile As Object, oShellZippedFileInZipfile As Object
Dim strShellZippedFileInZipfileFilename As Variant 'NOT AS STRINGS!! (Shell error if strings)
strGofnZipfileFullpath = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)
Set oShell = CreateObject("shell.application")
Set oShellZipfile = oShell.Namespace(strGofnZipfileFullpath)
Set oShellZippedFileInZipfile = oShellZipfile.Items.Item(0) 'assumes only 1 file in zip.
strShellZippedFileInZipfileFilename = oShellZippedFileInZipfile.Name
If Windows Explorer is set to hide extensions and the zipped file has a registered extension, strShellZippedFileInZipfileFilename does not include the extension.
However, the zipped file object (oShellZippedFileInZipfile) also has a Path property which does include the extension. So you can get the filename including extension like this:
strShellZippedFileInZipfileFilename = Right(oShellZippedFileInZipfile.Path, InStr(StrReverse(oShellZippedFileInZipfile.Path), Application.PathSeparator) - 1)
One thing to keep in mind is the folder options within Windows explorer. This may not help you in this case, but I have had a number of fso interactions that have a huge issue with how files are displayed in Windows Explorer. Something as simple as your system is displaying the file extension, and the other is not, can sometimes be the culprit in looking for files.