Passing values from C# to VBA code? - asp.net-mvc

I am pretty new to VBA, sorry if this is simple question or already answered... I tried by searching on MSDN but did not get any thing to implement this.
I am in need of sending a string (Host name in URL, e.g. xyzserver:4500/Home/GetExcelData) to VBA code before downloading the Excel to User.
I am calling MVC action method from Excel VBA code, getting some data and displaying in Excel.
My problem is Host name (xyzserver:4500) is different in different servers, I need to update the URL dynamically based on the server URL that the user is accessing from.
Basically i need to send server name to VBA code to update the URL in module. Is there any way to send and maintain values from C# (MVC Action) to VBA?
Session/ Application Cache kind of things are available in VBA?
Thanks in advance

You can pass a parameter from c# to VBA in this manner:
Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook wb = app.Workbooks.Open("your file here which contains the macro");
string url = "blah blah";
app.Run("name of macro here", url);
If you want to edit the vba code programmatically, then you would need to reference the VBIDE. Have a look at Chip Pearson's excellent website: http://www.cpearson.com/Excel/vbe.aspx

Related

What does "URL=" in a URL Mean?

May be a dumb question, but it's been bugging me recently. I see "URL=" inside alot of URL's, such as this one:
http://www.tierraantigua.com/search-2?url=http%3A%2F%2Flink.flexmls.com%2Fwws30ham
What exactly is this used to do? Is it part of the iFrame functionality? I know the last part of the URL (after the URL=) is the part being displayed in the iFrame, but I'm unsure of why it is included in the primary URL as well.
Thanks!
The url you see here is just a standard query parameter wit the name url and the encoded value http%3A%2F%2Flink.flexmls.com%2Fwws30ham which decodes to http://link.flexmls.com/Fwws30ham. Most of the times it is used for determining redirection or source information by the application you are using. It is entirely domain-specific and can have any meaning the website developer would like to use.
PHP GET
Description ΒΆ
An associative array of variables passed to the current script via the URL parameters.
$url = $_GET['url'];
echo $url; // http%3A%2F%2Flink.flexmls.com%2Fwws30ham

ASP.NET MVC Redirect to file://

I have string data in the form
http://site.com/location
file://server/folder/
I am showing the data as links so clicking on the link takes you to the appropriate web site or file location. Clicking on a link built from 'http://...' data works fine but I get an 'unable to display web page' message when I click on a link built from 'file://' data.
I'm building the link using Html.ActionLink, passing the data as a parameter to a HandleLink method on the controller. The HandleLink method just does Redirect(data). I know the data is coming in correctly because I can copy the incoming value in the debugger and paste in in the address bar of my browser and that works as expected.
How can I make the 'file://' links work correctly?
EDIT: I botched the first question I asked here -- I hope they have a badge for that. The 'file://...' data items are to a folder, not a specific file. Does that make any difference?
As mentioned above, you can't use file:\ to get files on a remote server however you can serve the files using something like the following:
public FileContentResult GetFile(int fileID)
{
string fileName = GetFileNameFromID(fileID); //Retruns path and filename on server
string contentType = "text/csv"; //or other appropriate type
return new FilePathResult(fileName, contentType);
}
Much more info here

How to get current page Url in MVC

I am writing a web app that has to deal with urls containing hash character ("#").
I am using MVC 1 with ASP.NET 3.5 (VS 2008).
My urls are like this one:
www.mysite.com/token/?name1=value1&#&name2=value2
My issue is that I cannot find a method to get the original URL, but only get the substring before the hash character:
www.mysite.com/token/?name1=value1&
I used the MVC methods provided from the class HttpRequestBase.
Anyone can suggest me an alternate method to get the entire url?
Thank you, this is my very first question!
PS: I think maybe I have to encode my hash character, isn'it?
You cannot access anything after the # from the server-side - this is all Client-side. You will need to find another way to pass the information you want through to the server.
If you are posting, you can do this with hidden fields. If you are using ajax posts, you can pass the data within the model.

Designing a service for reading files

Conceptual Overview
Store templated text files on the file system. Read text file into a templating service, get the string, send the email.
I have 2 services so far:
ITemplateService - this is the templating engine (accepts model and the string for templating).
IEmailService - Send email.
Can I have an example of how a file reader service would look, how I can mock or unit test it. Thanks.
I basically want to pass a path/filename to a method and let it read the file and return a string.
System.IO.File.ReadAllText(string filePath) returns the contents.
You may also checkout Postal.

asp.net MVC Read from a http URL

i am trying to use this code:
<%= File.ReadAllText(Server.MapPath("Members/newsletters/welcome.html"))%>
which works great but now the welcome.html file has moved onto another server so i need to read it from an external URL .
any suggestions?
Have a look at the HttpWebRequest class in .NET (there's an example at that URL or have a look at this blog).
try this:
WebClient WebClient = new WebClient();
string YourContent = WebClient.DownloadString(YourUrl);
You need to be more specific. Are you trying to read it from an UNC path or over the web? Is the other on your network?
If UNC path, probably easiest to map a drive on your local server so it looks like a local file.
Of course, I'm wondering why you're doing it this way at all. Why not replicate the content to all web servers? Reading over network is expensive. Also, that logic should probably be encapsulated in a helper method which can handle caching. You might consider moving the logic to retrieve that content to your controller. A view should render the model data given to it.

Resources