I'm attempting to pass a string to the DownloadFile function in Visual Basic in place of the URL. The url will change according to what the current month and year is. Ex: http://example-website.com/092015
For this reason, I've done the following to make sure the url is updated every month:
Public Sub Main()
Dim A As String = "http://example-website.com/"
Dim B As String = Format(Month(Now), "00")
Dim C As String = Year(Now)
Dim registrySite As String = A & B & C
End Sub
The problem that I'm having is, the DownloadFile function requires a string.
Public Sub DownloadFile (
address As String,
destinationFileName As String
)
Is there a workaround for this or another way to do what I'm trying to do? I should note that the website cannot be cached.
Related
This question already has answers here:
How to make Excel VBA variables available to multiple macros?
(3 answers)
Closed 6 years ago.
I am trying to practice embedding my code. I created a subroutine called JrnlHeader to declare variables I will use in another subroutine. I am currently only concerned with one variable named Header. I would like to know why Header is empty in subroutine PrintToTextFile and how I can fix it to be able to use strings declared in JrnlHeader.
Private Sub JrnlHeader()
Dim Header As String
Dim SeqNo As String
Dim SeqVar As String
Dim Bu As String
Dim BuVar As String
Dim JrnlID As String
Dim JrnlIDVar As String
Dim JrnlDate As String
Dim JrnlDateVar As String
Dim Descr As String
Dim DescrVar As String
Dim Ledger As String
Dim LedgerVar As String
Dim Source As String
Dim SourceVar As String
Dim CurEff As String
Dim Reverse As String
Dim AutoLn As String
Dim AdjEnt As String
Header = "<JRNL_HDR_IMP>"
SeqNo = "<SEQNO>" & SeqVar & "</SEQNO>"
Bu = "<BUSINESS_UNIT>" & BuVar & "</BUSINESS_UNIT>"
JrnlID = "<JOURNAL_ID>" & JrnlIDVar & "</JOURNAL_ID>"
JrnlDate = "<JOURNAL_DATE>" & JrnlDateVar & "</JOURNAL_DATE>"
Descr = "<DESCR254>" & DescrVar & "</DESCR254>"
Ledger = "<LEDGER_GROUP>" & LedgerVar & "</LEDGER_GROUP>"
Source = "<SOURCE>" & SourceVar & "</SOURCE>"
CurEff = "<CUR_EFFDT>" & JrnlDateVar & "</CUR_EFFDT>"
Reverse = "<REVERSAL_CD>N</REVERSAL_CD>"
AutoLn = "<AUTO_GEN_LINES>N</AUTO_GEN_LINES>"
AdjEnt = "<ADJUSTING_ENTRY>N</ADJUSTING_ENTRY>"
End Sub
Sub PrintToTextFile()
Dim FileNum As Integer
JrnlHeader
FileNum = FreeFile ' next free filenumber
'Open "C:\Temp\TEXTFILE.TXT" For Output As #FileNum ' creates the new file
Open "C:\temp\TEXTFILE.TXT" For Append As #FileNum
Print #FileNum, Header
Close #FileNum ' close the file
End Sub
You have defined Header to be a local variable in JrnlHeader. This means its scope does not extend to other subroutines/functions.
You can define the scope of the variable to be "module" level, by placing the Dim Header As String statement prior to the first subroutine/function within the code module. Then its value will be available when execution resumes in PrintToTextFile.
Alternatively, you could change your code to pass the variable as a parameter between the two functions:
Sub PrintToTextFile()
Dim Header As String
'...
JnrlHeader Header
'...
Print #FileNum, Header
End Sub
Sub JrnlHeader(Header As String)
'... (but don't include any declaration of Header!)
Header = "<JRNL_HDR_IMP>"
'...
End Sub
But, judging by how many variables are being set up in JrnlHeader, I think you will want to go with using a module-level scoped variable approach.
The two subroutines have different scope. Variables defined JrnlHeader are not available in PrintToTextFile. If you want header to be available in PrintToTextFile the change it to PrintToTextFile(header as string) and call PrintToTextFile(header) from JrnlHeader.
I have a string which needs converting to "dd/MM/yy". I am converting it to a Date datatype and then formatting it back to a string. I was wondering if there is an easier/elegant way of doing it.
Here is an example of how I am achieving this now
Dim strValue As String = "17/08/2016"
Dim dteValue as Date = Convert.ToDateTime(strValue)
strValue = dteValue.ToString("dd/MM/yy")
What I am looking for is a way of converting without casting it to a Date datatype, is it possible?
Without casting it to Date I would suggest:
Dim strValue As String = "17/08/2016"
strValue = strValue.PadLeft(6) & strValue.PadRight(2)
It takes my computer 1 ms to process above code 10.000 times, but 22 ms to process yours.
Edit: if the date has different lengths, you could split on the forwardslash and rebuilt it:
Dim arrSplitValue() As String = Split("17/08/2016", "/")
Dim strValue As String = arrSplitValue(0) & "/" & arrSplitValue(1) & "/" & arrSplitValue(2).PadRight(2)
You could use an extension method:
public static string ToMyDateString(this string stringDate)
{
return DateTime.Parse(stringDate).ToString("dd/MM/yy");
}
And then use it like so:
string strValue = "17/08/2016".ToMyDateString();
or
string strValue = "17/08/2016";
string strValueDate = strValue.ToMyDateString();
Just realised you're in VB.NET so pretty sure it'll translate over. If I get a moment I'll try and do it in VB.NET.
VB.NET version
<System.Runtime.CompilerServices.Extension> _
Public Shared Function ToMyDateString(stringDate As String) As String
Return DateTime.Parse(stringDate).ToString("dd/MM/yy")
End Function
Warning - this has just been put through a converter, so may need to be tweaked.
I'm trying to save into a variable the parameters list that I receive in a Function. For example:
Function fTest(xVal1 as Integer, xVal2 as Integer) as String
wListParams = "xVal1:" & xVal 1 & "#" & "xVal2:" & xVal2
End Function
I want to use this list if an error occurs and send a mail.
What I'm looking it's a way for to build this String without writing every case in every function (more than 1000).
Please help!
Thanks!!!
Do you want to concatenate all the parameters together into one string? If so, try this.
Imports System.Text
Public Function BuildParametersString(ByVal ParamArray parameters() As Integer) As String
Dim sb As New StringBuilder()
For i As Integer = 0 To parameters.Count() - 1
sb.Append(String.Format("xVal{0}:{1}#", i + 1, parameters(i)))
Next
Return sb.ToString()
End Function
Private Sub test()
Dim param1 As Integer = 1, param2 As Integer = 2
' passing individual parameters
Dim s1 As String = BuildParametersString(param1, param2)
' passing paramaters in an array
Dim s2 As String = BuildParametersString({param1, param2})
End Sub
I am having issues.
The script runs and builds the String and can Parse the Results, It receives a Accept Automatically then it searches for a value for RedirectURL.
How do i add a URL value to be build in the build version of my script.
Friend Overrides Function Filter() As String
Dim sb As New StringBuilder()
Return sb.ToString() '' Returning an empty string allows the applicant to be passed to the lender; otherwise the applicant is not sent
If app.BankAccount = String.Empty Then
sb.Append("Bank Account must be present;")
End If
If app.BankSortCode = String.Empty Then
sb.Append("Sort Code must be present;")
End If
If app.LoanAmount = String.Empty Then
sb.Append("Loan Amount must be present;")
End If
End Function
Friend Overrides Function Build() As String
Dim sb As New StringBuilder()
sb.AppendLine("&first_name=" & app.FirstName)
sb.AppendLine("&surname=" & app.LastName)
sb.AppendLine("&dob=" & app.DOB.ToString)
sb.AppendLine("&email=" & app.Email)
sb.AppendLine("&home_phone=" & app.LandPhone)
sb.AppendLine("&mobile_phone=" & app.MobilePhone)
sb.AppendLine("&postcode=" & app.PostCode)
sb.AppendLine("&affid=" & app.affID)
sb.AppendLine("&subid=" & lendertier.Credential1)
sb.AppendLine("&leadid=" & lendertier.Credential2)
sb.AppendLine("&bk_no=" & app.BankAccount)
sb.AppendLine("&bk_s=" & app.BankSortCode)
sb.AppendLine("&ar=" & app.LoanAmount.ToString)
Return sb.ToString()
End Function
Friend Overrides Function ParseResult(ByVal sResult As String) As Boolean
app.Outcome.RedirectURL = sResult
AcceptLead()
Return True
End Function
This question isn't overly clear but I'll give it a shot...
I think you are after retrieving the application version number and including it in the paramaterse in which case you will need something like:-
Dim versionNumber As Version
versionNumber = Assembly.GetExecutingAssembly().GetName().Version
You will have to import System.Reflection for this to work.
Afternoon,
How can I tailor the below function in VB.net to pickup the first 8 characters of the second part of the filename in the array?
Private Function GetFormattedDateFromFileName(ByVal fileName As String) As String
Dim parts() As String = fileName.Split("-")
If parts.Length = 3 Then
Dim dt As DateTime
If DateTime.TryParseExact(parts(1), "yyyyMMdd", Nothing, Globalization.DateTimeStyles.None, dt) Then
Return dt.ToString("MM/dd/yyyy")
End If
End If
Return ""
End Function
I was using this function before because the filename would be split in 3 parts so this worked perfect for me. File name looked like below before:
542656-20130402-FTO Disclosure.pdf
548872-20120501-Funds a.pdf
848581-20110215-Alpha.pdf
Problem now is that the filename structure has been changed and will now be in 2 parts like so:
542656-20130402.pdf
548872-20120501.pdf
848581-20110215 Alpha.pdf
652162-20120711 a.pdf
So how would I go about to tailor the above function to still split the filename with the “-“ but instead of trying to parse the date exactly, for it to just look at the first 8 characters of the second part (being the date) and continue on as it has been with the rest of the code?
Kindly advise.
A
If you change your split call and length check to:
Dim parts() As String = fileName.Split("-"c, "."c, " "c)
If parts.Length > 2 Then
Then the existing code should work correctly on both versions.
Edit in response to comment:
In order to make this work on files like "12764-20120124b.pdf", you'll need to just extract the substring directly:
Private Function GetFormattedDateFromFileName(ByVal fileName As String) As String
Dim parts() As String = fileName.Split("-")
If parts.Length > 1 AndAlso parts(1).Length >= 8 Then
Dim dt As DateTime
Dim dtStr = parts(1).Substring(0, 8)
If DateTime.TryParseExact(dtStr, "yyyyMMdd", Nothing, Globalization.DateTimeStyles.None, dt) Then
Return dt.ToString("MM/dd/yyyy")
End If
End If
Return ""
End Function