we have created a vcs file using the following code.
Dim mstream As New MemoryStream
Dim writer As New StreamWriter(mstream)
writer.AutoFlush = True
GetvCalendarText(writer)
Response.Clear()
Response.AppendHeader("Content-Disposition", "attachment; filename=Event" & eventID & ".vcs")
Response.AppendHeader("Content-Length", mstream.Length.ToString)
Response.ContentType = "application/download"
Response.BinaryWrite(mstream.ToArray)
Response.End()
GetCalendarText method
Dim body As String = <b>New event</b>
writer.WriteLine("BEGIN:VCALENDAR{0}", vbCrLf)
writer.WriteLine("VERSION:1.0{0}", vbCrLf)
writer.WriteLine("BEGIN:VEVENT{0}", vbCrLf)
writer.WriteLine("DTStart:{0}{1}", DateTime.Now.ToString("yyyyMMddTHHmm00Z"), vbCrLf)
writer.WriteLine("DTEnd:{0}{1}", DateTime.Now.AddHours(2).ToString("yyyyMMddTHHmm00Z"), vbCrLf)
writer.WriteLine("DESCRIPTION:{0}", body)
writer.WriteLine("X-ALT-DESC;FMTTYPE=text/html:{0}", body)
writer.WriteLine("SUMMARY;ENCODING=QUOTED-PRINTABLE:{0}{1}", Test event, vbCrLf)
writer.WriteLine("PRIORITY:3{0}", vbCrLf)
writer.WriteLine("END:VEVENT{0}", vbCrLf)
writer.WriteLine("END:VCALENDAR{0}", vbCrLf)
It is generating the vcs file and upon opening the file it is opening in Outlook 2010 with subject , start time and end time with correct values.
The description we have given is in html format but it is shown as plain text.
So how we can show the html description as such.
I tried to add html description in vCalendar format and can't find any proper method to do this.
Finally changed our vCalendar format to iCalendar and it allows to show html description.
We removed the description line ie writer.WriteLine("DESCRIPTION:{0}", body)
and now it showing html formatted description.
Following parts also changed.
Response.AppendHeader("Content-Disposition", "attachment; filename=Event" & eventID & ".ics")
and
writer.WriteLine("VERSION:2.0{0}", vbCrLf)
Related
I am trying to save a created, PDF file to a location specified by the user. I'm using Visual Studio Community 2019. Essentially, I am taking a screenshot of this form:
And by using the PdfSharp external library, I create a PDF file and then save that PDF to some file location specified by the user. Here is the UI for the user to select their preferred file location:
The issue arises once the program tries to save the PDF file to the location given by the user. Here is the error I get from Visual Studio:
System.NotSupportedException: 'No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.'
I looked online for this specific error, but I don't really understand it nor what to do with it, it's very confusing. I'm still a bit of beginner when it comes to working with Visual Basic. Here's the code that tries to do it:
Dim fileLocation As String
fileLocation = folderBrowseBox.Text
GetFormImage(True).Save(fileLocation & "\" & RemoveWhitespace(filename) & "_" & RemoveWhitespace(collectionPeriod) & ".jpg", ImageFormat.Jpeg)
' Create new pdf document and page
Dim doc As New PdfDocument()
Dim oPage As New PdfPage()
' Add the page to the pdf document and add the captured image to it
doc.Pages.Add(oPage)
Dim img As XImage = XImage.FromFile(fileLocation & "\" & RemoveWhitespace(filename) & "_" & RemoveWhitespace(collectionPeriod) & ".jpg")
'Create XImage object from file.
Using xImg = PdfSharp.Drawing.XImage.FromFile(fileLocation & "\" & RemoveWhitespace(filename) & "_" & RemoveWhitespace(collectionPeriod) & ".jpg")
'Resize page Width and Height to fit image size.
oPage.Width = xImg.PixelWidth * 72 / xImg.HorizontalResolution
oPage.Height = xImg.PixelHeight * 72 / xImg.HorizontalResolution
'Draw current image file to page.
Dim xgr = PdfSharp.Drawing.XGraphics.FromPdfPage(oPage)
xgr.DrawImage(xImg, 0, 0, oPage.Width, oPage.Height)
End Using
doc.Save(fileLocation & "\" & RemoveWhitespace(filename) & "_" & RemoveWhitespace(collectionPeriod))
img.Dispose()
The second to last line of code ("doc.Save(fileLocation & ...)") is where the error occurs. The folderBrowseBox.Text (very first line of code) comes from the textbox you see from my second screenshot. Any help/advice will be greatly appreciated!
Try adding this line before writing the PDF file
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance)
Got the idea from here
I need help getting the hyperlink to work as well as getting the value stored in YestFile.FileName to show up.
I have a list of objects of class FileFromYesterday (Collection2) with 3 properties. Two properties are just Strings (FileName and Entity) but one property (URL) contains a URL as a String. I am trying to hyperlink the URL to FileName in the main.
I have a feeling that I need to use HTML to format the email somehow but I have no Idea where to begin
Dim HyperLink As String
Dim FileLine As String
Dim NewBody As String
Dim message As New EmailMessage(EWS)
message.Subject = "Monthly Financial Imported into Docushare on " & String.Format("{0:MM-dd-yyyy}", Yesterday)
NewBody = "Total of " & Collection2.Count & " Files Imported." & "\n"
For Each YestFile In Collection2
HyperLink = YestFile.FileName.Replace(".pdf", "")
FileLine = HyperLink & "Entity: " & YestFile.Entity & Environment.NewLine
NewBody = NewBody & FileLine
Next
You should be creating a HTML document in your NewBody varible so you need the pre and post tags for a HTML document eg
<html>
<body>
</body>
</html>
the body content you want should go within between the Body Tags and for thing like NewLines you need to use the appropriate HTML tags such as
<br />
Eg what ever you end up creating in you NewBody variable you should be able to write out to a file or test in any Online HTML validator https://validator.w3.org/#validate_by_input to see if its valid and how it will look before trying to send it.
I am still a beginner with VBA, so if you need further explanation or I am not describing my problem correctly please let me know.
I am trying to achieve the following:
Upload a file from my computer to a website (You can only try to upload if you login, so sadly I cannot share a link)
In order to achieve this I need to do three things:
1) Click the "Upload" Button
2) Insert the Filepath into the search field of the PopUp Window
3) Click the "Open" Button
The website looks like this:
The PopUp Window looks like this:
The HTML code of the upload field is the following:
<div class="button-wrapper">
<input class="design-file-input" type="file">
<a class=" button prio1" href="javascript:void(0);">Design hochladen</a>
</div>
I guess there might be two solutions two my problem, however, I am not able to realize my plans.
IDEA 1
Somehow get the filepath into the input field and the page to download it
Therefore I tried the vba following VBA codes:
objIE.document.getElementsByClassName("design-file-input")(0).Value
objIE.document.getElementsByClassName("design-file-input")(0).innerText
And then try to somehow make the website submit my entry.
IDEA 2
Click the "Design Hochladen" Button.
objIE.document.getElementsByClassName("button-wrapper")(0).Click
But then the PopUp window comes up and I don't know how to control it with VBA
I am happy to hear and try your suggestions!! If you need any further details, just let me know! Thank you so much if you can give me any advice
Directly assigning the file path to the value of that specific HTML element does not work. A while ago, I had the same issue (automatically passing a file to an upload file dialog). After a long googling session, I found following solution. Unfortunately, I could not find the link from where I took this answer. In case I come accross the website, I will share the link with you:
Dim FilePath As String
Dim FileName As String
FilePath = Environ$("temp") & "\"
FileName = "test_file_for_upload" & ".xlsx"
UploadFile DestURL, FilePath & FileName, "file" 'Usage
'******************* upload - begin
'Upload file using input type=file
Public Sub UploadFile(DestURL As String, FileName As String, _
Optional ByVal FieldName As String = "File")
Dim sFormData As String, d As String
'Boundary of fields.
'Be sure this string is Not In the source file
Const Boundary As String = "---------------------------0123456789012"
'Get source file As a string.
sFormData = GetFile(FileName)
'Build source form with file contents
d = "--" + Boundary + vbCrLf
d = d + "Content-Disposition: form-data; name=""" + FieldName + """;"
d = d + " filename=""" + FileName + """" + vbCrLf
d = d + "Content-Type: application/upload" + vbCrLf + vbCrLf
d = d + sFormData
d = d + vbCrLf + "--" + Boundary + "--" + vbCrLf
'Post the data To the destination URL
IEPostStringRequest DestURL, d, Boundary
End Sub
'sends URL encoded form data To the URL using IE
Sub IEPostStringRequest(URL As String, FormData As String, Boundary As String)
'Create InternetExplorer
Dim WebBrowser: Set WebBrowser = CreateObject("InternetExplorer.Application")
'You can uncoment Next line To see form results
WebBrowser.Visible = True
'Send the form data To URL As POST request
Dim bFormData() As Byte
ReDim bFormData(Len(FormData) - 1)
bFormData = StrConv(FormData, vbFromUnicode)
WebBrowser.navigate URL, , , bFormData, _
"Content-Type: multipart/form-data; boundary=" + Boundary + vbCrLf
Do While WebBrowser.Busy
' Sleep 100
DoEvents
Loop
'WebBrowser.Quit
End Sub
'read binary file As a string value
Function GetFile(FileName As String) As String
Dim FileContents() As Byte, FileNumber As Integer
ReDim FileContents(FileLen(FileName) - 1)
FileNumber = FreeFile
Open FileName For Binary As FileNumber
Get FileNumber, , FileContents
Close FileNumber
GetFile = StrConv(FileContents, vbUnicode)
End Function
'******************* upload - end
The third argument "file" denotes the ID of the HTML element which needs to be triggered.
Hope this solution workds for you as well
I'm not sure what is wrong with my code. It reads the PDF file, and grabs all the text, but every item is combined together into one string with no separator of any kind.
Sample:
"Houses: 2
Bedrooms: 3
Bathsroom 4"
will get read as "Houses: 2Bedrooms: 3Bathsroom 4"
I've searched through all of the examples to no avail. I've also tried LocationTextExtractionStrategy to no avail. I've tried using the .split method and no help.
Public Shared Function ParseAllPdfText(ByVal filepath As String)
Dim sbtxt, currenttext As String
sbtxt = ""
Try
Using reader As New PdfReader(filepath)
For intPages As Integer = 1 To reader.NumberOfPages
currenttext = PdfTextExtractor.GetTextFromPage(reader, intPages, New LocationTextExtractionStrategy())
currenttext = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.[Default], Encoding.UTF8, Encoding.[Default].GetBytes(currenttext)))
sbtxt = sbtxt & currenttext & vbcrlf
Next
End Using
Catch ex As Exception
MsgBox(" There was an error extracting text from the file: " & ex.Message, vbInformation, "Error Extracting Text")
End Try
Return sbtxt
Nevermind, this was an oversight on my part. I realized the lines are separated by Chr(10). Chr(10) does not create a new line in textboxes, which is where I was outputting my string. It DOES however create a new line in MsgBox. So if anyone else runs into this problem, chr(10) is the separator. :-)
I'm grabbing the HTML source code of a webpage and try to convert it to a single-line string but I can't.
This is my code:
Dim source As String = client.DownloadString("http://www.whocallsme.gr/el/master/lookup/19588")
source = source.Replace(vbCrLf, "")
I also tried using Environment.NewLine and vbNewLine instead of vbCrLf but the result remains the same.
Try this:
source = source.Replace(vbLf, "").Replace(vbCr, "")