As usual, I do my research in the various forums. My personal discipline is that if I have struggled for more than a day, I need to reach out for help. Because after a day, I can no longer see the wood because of the trees.
My web site has some code written in VB.NET which I use on many pages of the web site. Where possible, I try to write code only once. For obvious reasons. I include the code in a class.
However, when I use a class, I cannot access the page controls. For example, the HTTP context is available on the VB.NET code-behind but not in the class declared in the code-behind. Is there some way of achieving this result?
Another option is to have VB.NET code in a separate source file. And to include this source file into the main source code file at compile time. If I need to make a change, I can do it in the source code
“segment” once only. Right now, if I want to make a global change, I have to cut and paste through many web pages. I have read that this type of “include” was possible in VB prior to VB.NET. I can find no reference to this for VB.NET in Visual Studio. Any suggestions?
For example if this code is to be run on every web page, how would you set it up so that you only have one source? That is called on 150 pages?
Protected Sub getCookieVariable()
'
Dim myCookieName As String
Dim myCookieValue As String
'
myCookieName = "hfl3"
Dim myCookie As HttpCookie = HttpContext.Current.Request.Cookies(myCookieName)
If Request.Cookies(myCookieName) IsNot Nothing Then
myCookieValue = myCookie.Value
myCookie.Expires = DateTime.Now.AddDays(30)
Response.Cookies.Add(myCookie)
session("myCookieValue") = myCookieValue
End If
'
End Sub
Related
As usual, I do my research in the various forums. My personal discipline is that if I have struggled for more than a day, I need to reach out for help. Because after a day, I can no longer see the wood because of the trees.
My web site has some code written in VB.NET which I use on many pages of the web site. Where possible, I try to write code only once. For obvious reasons. I include the code in a class. However, when I use a class, I cannot access the page controls. For example, the HTTP context is available on the VB.NET code-behind but not in the class declared in the code-behind. Is there some way of achieving this result?
Another option is to have VB.NET code in a separate source file. And to include this source file into the main source code file at compile time. If I need to make a change, I can do it in the source code “segment” once only. Right now, if I want to make a global change, I have to cut and paste through many web pages. I have read that this type of “include” was possible in VB prior to VB.NET. I can find no reference to this for VB.NET in Visual Studio. Any suggestions?
For example if this code is to be run on every web page, how would you set it up so that you only have one source? That is called on 150 pages?
Protected Sub getCookieVariable()
'
Dim myCookieName As String
Dim myCookieValue As String
'
myCookieName = "hfl3"
Dim myCookie As HttpCookie = HttpContext.Current.Request.Cookies(myCookieName)
If Request.Cookies(myCookieName) IsNot Nothing Then
myCookieValue = myCookie.Value
myCookie.Expires = DateTime.Now.AddDays(30)
Response.Cookies.Add(myCookie)
session("myCookieValue") = myCookieValue
End If
'
End Sub
I have multiple files with the same VBA code in them, which will most likely have to be changed over time. I don't want to go one by one and c/p the code, so one obvious idea that came to mind is to have one centralized document with the code (template?) that all files refer to.
I found a few topics here at stackoverflow, but none of them work for me or are inconclusive:
Run external vba-code in MS Word
Centralized VBA code (one file) for multiple workbooks
Calling an External VBA from VBScript
Any ideas?
Yes, your idea of centralizing the code in a template and attaching that template to the various Word docs will work. These pics are using Word 2007 but I think it's pretty similar for newer versions. First create a new document and put the code in a Module (named "CommonFunctions" in the picture):
Save the doc as a macro-enabled template:
Now make a new document, save it as a *.docm (necessary for it to run code because a *.docx is macro-free), and attach the document template as shown below. (An alternative is to put the *.dotm file in the startup directory C:\Users\<username>\AppData\Roaming\Microsoft\Word\STARTUP, which loads it automatically.) If you don't put it in the Startup, you'll see the name but it won't be loaded (ie, checked), but you can load it in code, as I'll show.
Make a user form in this document:
Put this code for the button's click event:
If AddIns("c:\_b\MasterDocWithFunctions.dotm").Installed = False Then
AddIns("c:\_b\MasterDocWithFunctions.dotm").Installed = True
End If
Application.Run "CommonFunctions.Test1"
Application.Run "CommonFunctions.Test2"
Notice how the code can load the AddIn for you, because it won't be automatically loaded unless you put it in the Startup directory. This pic shows how the AddIn is referenced but not loaded. You need the checkbox to be able to call code in it. If you don't load it using code and don't put it in the Startup, then the user will have to manually put a checkmark everytime the document opens.
Now the form should work when you click the button. Notice how it can call both public and private functions. The Private keyword definitely shields one module from another, but it seems like modules that are called from an AddIn are considered to be part of the same module, not sure why? Also if you don't put Public or Private at all then VBA considers it Public, I'm pretty sure.
In Access VBA, you can access properties/methods of the running application by typing the word Application, followed by a period, followed by what you want.
For example, I can instantiate and show a FileDialog like so:
Dim openFileDialogue As Object
Set openFileDialogue = Application.FileDialog(3)
openFileDialogue.Show
If I wanted to do this in Visio VBA, what would I need to substitute Application for in the above code?
Thanks
Try this link: http://visguy.com/vgforum/index.php?topic=738.0
At the very bottom is a code sample from "Nikolay" that shows an alternative way of getting the Fi
It shows an alternative (though more complex) way of getting the File Dialog to show up in Visio
I have created a sample form as
Dim obj_CommonForm As New Form
Dim btn_Create As New Button
btn_Create.Location = New System.Drawing.Point(30, 200)
btn_Create.Size = New System.Drawing.Size(60, 15)
btn_Create.Text = "MyCommand"
obj_CommonForm.Controls.Add(btn_Create)
obj_CommonForm.Show()
Now I would like to save this form as .vb file and include it in my project.
Similar I have created in PHP where output html and saving as .php file is easy. This I have used as a initialize function as i start doing some project whose database has been created already.
Your question is not clear to me... so I try to guess.
If you want to have a .vb file then you just have to take the .vb created by Visual Studio or by your application and include it in the project at compile time.
Instead if you are talking about a generic form created by your program and load it at runtime, you have to learn something about Serialization.
In latter case, I had your same problem some time ago and following article was a good starting point:
http://community.visual-basic.it/lucianob/archive/2007/10/14/20711.aspx
It is in Italian, but I think you can translate via some tool (except for code examples, obviously) :-)
I have about 100 sub routines that I need to use..I am going to be calling them into a web browser component do get some elements after each web page has completed.
Is it possible to create one sub routine and then has say a streamreader loop through a folder and read each text file in the folder to put the sub into a string?
I would then simply call that one sub into the webbrowser component but I didn't know if this was possible?
There would be about 100 different text files in the folder.
The thinking behind this would be that if I wanted to add more website instructions to the sub or take away from the sub I could just delete a text file.
How would one begin this crazy journey?
Thanks
That isn't really something you would want to do. Its also not possible since vb.net is a compiled language it can't just read the text of the code on the fly and implement it like that.
You are better off investigating another pattern that will meet your needs.
Actually you can programmatically create a vb.net program on the fly. I created a web program which rewrites itself as I see fit. Part of that code adds new subroutines to the same vb.net program. Basically writing more sub routines to itself and then it runs itself again. You can easily store your other subs into txt files and then recall the data later if you wanted. The trick here, however, is that you have to first add the sub routines into an entirely new file, then, when all the writing is done, you can then preform the following:
File.Delete(Server.MapPath("your old file name"))
File.Copy(Server.MapPath("your new file name"), Server.MapPath("your old file name"))
It should be noted that I make web applications, so it doesn't quite work the same. Using asp.net pages, the way I get the new program to run with the new sub routines is to run on the client side code, in which I include a little timed refresh, which fires once the rewriting task is finished. The page then refreshes with the new vb.net back end code in place.
In order to do this for a desktop application or whatever might require something else which I am not aware of.