I am working on a vb.net 2.0 application and trying to read HTTP headers. I am able to get header values through Request.Headers.Get("HTTP_VARIABLE_NAME"). I would like to get all header name/value pairs using Headers property and display on a separate page under a button click event from a given page.
How can I loop and write all name/value pairs please?
Taken directly from MSDN, so all credit goes to the poor Microsoft employee who was given the rough task of documenting the system.net namespace. Although I do feel like I could write a better example myself...
The following code example displays the names and values of all headers in the HTTP request
Dim loop1, loop2 As Integer
Dim arr1(), arr2() As String
Dim coll As NameValueCollection
' Load Header collection into NameValueCollection object.
coll=Request.Headers
' Put the names of all keys into a string array.
arr1 = coll.AllKeys
For loop1 = 0 To arr1.GetUpperBound(0)
Response.Write("Key: " & arr1(loop1) & "<br>")
arr2 = coll.GetValues(loop1)
' Get all values under this key.
For loop2 = 0 To arr2.GetUpperBound(0)
Response.Write("Value " & CStr(loop2) & ": " & Server.HtmlEncode(arr2(loop2)) & "<br>")
Next loop2
Next loop1
I don't know what you mean by "write to a page", but this should get you started.
Is there a reason that forces you to use .NET 2? .NET 4 is still supported on Windows XP SP3 and up and offers many advantages over previous versions. Just putting it out there.
Related
I need to collect the names of all the authors making revisions or adding comments to a Word document. I do something like this:
Public Function collectAuthors() As String
Dim cmt As Word.Comment
Dim r As String: r = vbCr
Dim t As String
Dim i As Long: i = 0
Dim rev As Word.Revision
For Each cmt In ActiveDocument.Comments
t = cmt.Author
If InStr(r, vbCr & t & vbCr) = 0 Then r = r & t & vbCr
Next cmt
For Each rev In ActiveDocument.Revisions
t = rev.Author
If InStr(r, vbCr & t & vbCr) = 0 Then r = r & t & vbCr
Next rev
...
Recently t = rev.Author started to fail with
Run-time error '-2147467259 (80004005)':
Method 'Author' of object 'Revision' failed
This perhaps has something to do with the size of the document. I am using Word 2016, 64-bit version on Windows 7.
I also tried a loop where the member of the collection is indexed explicitly as in
t = ActiveDocument.Revisions(i).Author
and it stops after a few (maybe 10) iterations.
What is the cause of this error and can it be eliminated by a different coding approach?
Or should I forget about this and extract the author names from word\document.xml and word\comments.xml?
Thanks.
In the end, I read the author names from the XML directly. There are several obstacles which are not difficult:
The relevant XML needs to be extracted from the .docx file. I used
7-Zip as it's free and easy to automate, plus will work well when it
comes to write the XML back to the document.
The resulting files are UTF-8. UTF-8 is not supported directly by
VBA.
The author names need to be extracted from the XML. It is simple
enough to do this with a simple linear search through the XML. I did
not do any XML parsing or lexical analysis.
I had no control over the input documents, thus was in no position to break it up into more manageable parts. However reading the XML directly sidestepped the errors completely. Lastly, this is much better than attempting to process RTF.
I need to extract specific text from outlook in bulk (through 9000 emails)
I was wondering would something like this work
Dim Folder as Outlook.MAPIFolder
Dim sFolders As Outlook.MAPIFolder
Dim iRow As Integer, oRow As Integer
Dim MailBoxName As String, Pst_Folder_Name As String, Destination As String
ThisWorkbook.sheets(1).Cells(1,1) = "Destinations"
For iRow = 1 To Folder.Items.Count
ThisWorkbook.Sheets(1).Cells(oRow, 1) = Folder.Items.Find(Destination)
I have only some experience in VBA from years ago and I need am trying to create system like this for my job so I can pull out the information needed from the Body of an Email instead of scanning through thousands of emails seprately.
Does anyone know some good source/tutorials I can look at? as every one keeps leading me back to the same place
Thankyou
Is this what you are trying (Tested from within Outlook)? Please amend it to run from MS-Excel.
Sub Sample()
Dim myFilter As String, SearchString As String
Dim OutlookTable As Table
Dim OutlookRow As Row
'~~> This is your search string. Change as applicable
SearchString = "Siddharth"
'~~> Create Query
myFilter = "#SQL=" & _
Chr(34) & _
"urn:schemas:httpmail:textdescription" & _
Chr(34) & _
" ci_phrasematch '" & _
SearchString & _
"'"
Set OutlookTable = Application.ActiveExplorer.CurrentFolder.GetTable(myFilter)
Do Until OutlookTable.EndOfTable
Set OutlookRow = OutlookTable.GetNextRow
'~~> Print Subject (For example) of that email
'~~> which has the search string
Debug.Print OutlookRow("Subject")
Loop
End Sub
Does anyone know some good source/tutorials I can look at?
Tutorial: See this MSKB Article
The Outlook object model provides the Find/FindNext, Restrict, GetTable and AdvancedSearch methods for filtering items in Outlook. I'd suggest using the Restrict emthod in your case. The Find or FindNext methods are faster than filtering if there are a small number of items. The Restrict method is significantly faster if there is a large number of items in the collection, especially if only a few items in a large collection are expected to be found.
You can read about them and find the sample code in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
Advanced search in Outlook programmatically: C#, VB.NET
The Filtering Items section in MSDN describes all possible methods in depth.
Application.ActiveExplorer.CurrentFolder.GetTable(myFilter)
Don't ever use multiple dots in the single line of code. It may bring another issue into your code. I always recommend breaking the chain property and methods calls and declaring them on separate lines of code. Thus, you will be able to see under the debugger what each property and method returns and find the cause of the issue easily (if any).
I have scoured the net for days trying to figure this out, but apparently my gaps in Access are too severe and the answer eludes me. Someone has apparently already answered this question, however I'm not able utilize the information.
My specific situation:
Table1 has 30,000+ rows and multiple columns. "Photo Path" is a text field with the path and filename of an image. "Photo" is an OLE Object field currently empty.
What I would like to do is store the image specified in "Photo Path" as an OLE object in "Photo".
Table1 Current State:
Name - Photo Path - Photo
Impala - C:\Cars\Impala.jpg -
Jeep - C:\Cars\Jeep.jpg -
Table1 Desired Result:
Name - Photo Path - Photo
Impala - C:\Cars\Impala.jpg - LONG BINARY DATA
Jeep - C:\Cars\Jeep.jpg - LONG BINARY DATA
I don't know how to execute FileToBlob() against my entire database using the generously provided code. The authors seem to expect me to use a form, which I was unable to get to work as well.
What I think I want is an SQL statement that will execute against every row in Table1 using FileToBlob() or something close to it.
I've tried variations of the following statement in the SQL Query to no avail.
SELECT Table1.[Photo Path], FileToBlob(Table1.[Photo Path],Table1.Photo) As Photo
FROM Table1;
Thank you for taking the time to read this and providing an answer.
Had to figure this one out for myself as there were no responses. For those may follow looking for an actual answer, here it is.
I modified the code that that I found to fit my specific problem.
Create a new module and put the code below in it. If by chance the code does not work, you can try going to Tools-->References and if not already selected, select "Microsoft DAO X.x Object Library" where X.x is the latest library. If it still doesn't run you'll have to check to see if you need to select any other references.
There are so many records to go through, I felt better doing this through code instead of a query that may take a long time to execute and one won't know what is going on. In the code I have it writing to the status bar in Access so you know where you are at (but if the files are small it will probably fly by, but at least you know it is working).
To run the code, just put your cursor anywhere in the routine and I first like to press F8 which steps into the code just to make sure I'm in the right routine. Then press F5 to run the rest of the code. If you want to create a form to run the code instead you can do that too. Just create a button and on the "on click" event add the code:
call Load_Photo()
If you want to see the status updates, make sure the main access window is visible before you run the code (If you run from a form, it will already be there).
Note I renamed the field "Name" in Table1 to "strName" because "Name" is a reserved word. I'd suggest not using "Name" as a field name. You might be OK, but you could run into issues at some point, especially when referencing the field through code. If you choose not to change the field name, change the code.
Also note that the sample code provided stored as a binary. So if you create an Access form to show the records, the image will not automatically appear - there is some other manipulation necessary that I am not familiar with off hand.
Without further ado, here's the code to solution I was looking for:
Option Compare Database
Option Explicit
Public Sub Load_Photo()
On Error GoTo LoadFileError
Dim strSQL As String
Dim rstTable As DAO.Recordset
Dim strStatus As String
Dim count As Integer
Dim strFile As String
Dim nFileNum As Integer
Dim byteData() As Byte
Dim varStatus As Boolean
'
' In case something happens part way through the load, just load photos that have not been loaded yet.
'
strSQL = "Select [strName], [Photo Path], [Photo] from Table1 Where [Photo] is null"
Set rstTable = CurrentDb.OpenRecordset(strSQL)
If rstTable.RecordCount > 0 Then
rstTable.MoveFirst
count = 0
Do While Not rstTable.EOF
strFile = rstTable![Photo Path]
If Len(Dir(strFile)) > 0 Then
nFileNum = FreeFile()
Open strFile For Binary Access Read As nFileNum
If LOF(nFileNum) > 0 Then
count = count + 1
'
' Show user status of loading
'
strStatus = "Loading photo " & count & " for " & rstTable![strName] & ": " & rstTable![Photo Path]
varStatus = SysCmd(acSysCmdSetStatus, strStatus)
DoEvents
ReDim byteData(1 To LOF(nFileNum))
Get #nFileNum, , byteData
rstTable.Edit
rstTable![Photo] = byteData
rstTable.Update
Else
MsgBox ("Error: empty file, can't load for Name = " & rstTable![strName] & " and Photo Path = " & rstTable![Photo Path])
End If
Close nFileNum
Else
MsgBox ("Error: File not found for Name = " & rstTable![strName] & " and Photo Path = " & rstTable![Photo Path])
End If
rstTable.MoveNext
Loop
End If
LoadFileExit:
If nFileNum > 0 Then Close nFileNum
rstTable.Close
strStatus = " "
varStatus = SysCmd(acSysCmdSetStatus, strStatus)
Exit Sub
LoadFileError:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error on " & strFile
Resume LoadFileExit
End Sub
I'm wordering if there is a way (directly or using VBA) to get a list of all the building blocks as they appear in the Building Blocks Organizer, that is, the names of the building blocks, the Gallery, the Category, the Template, the Behavior, etc. I don't want to extract auto text parts or anything like that. I just want to be able to get and print the complete list of Bilding Blocks and the rest of the info dispayed in the Building Blocks Organizer.
Thanks a lot!
D
Building block entries are stored within several Word template files. If you want to iterate over all available building blocks, you must therefore iterate over all loaded Word templates. You can do so using the following macro:
Sub ListBuildingBlocks()
Dim oTemplate As Template
Dim oBuildingBlock As BuildingBlock
Dim i As Integer
For Each oTemplate In Application.Templates
For i = 1 To oTemplate.BuildingBlockEntries.Count
Set oBuildingBlock = oTemplate.BuildingBlockEntries.item(i)
Debug.Print oBuildingBlock.Name + vbTab _
+ oBuildingBlock.Type.Name + vbTab _
+ oBuildingBlock.Category.Name + vbTab _
+ oTemplate.FullName
Next
Next
End Sub
Using Attachmate, I am trying to write a VBA script that reacts when a specific phrase occurs and automatically executes commands via inline commands. Essentially, when a phrase appears, an inputbox appears asking the user for a specific quantity and the VBA code takes that quantity, inserts it into the terminal and then jumps around different menus to create an internal label. However, my problem is that I don't know how to have the VBA code react to the different strings that may be returned by the host. Sometimes it says "enter to continue" and sometimes it says "select user". So what I want it to do is based on the statement it receives to do a certain action, but I don't know what the command is for capturing what the terminal is receiving from the host. I've tried "waitforstring" and "readline" but it is obvious I am not using them correctly. Below is the code I have built thus far, please be gentle as it is still very unfinished. I have commented out several parts of it in attempts to troubleshoot my problems:
'variable declarations
Dim count As Long 'var used to indicate how many times code should loop (how many labels should be print)
Dim drugname As String
Dim qtyinput As Long
Dim CR As String ' Chr(rcCR) = Chr(13) = Control-M
Dim LF As String ' Chr(rcLF) = Chr(10) = Control-J
Dim strcheck As String
'assign values to variables
count = 0
CR = Chr(Reflection2.ControlCodes.rcCR)
LF = Chr(Reflection2.ControlCodes.rcLF)
qtyinput = InputBox("Number of items being sent", Quantity)
drugname = .GetText(22, 15, 22, 46) ' StartRow:=22, StartColumn:=15,EndRow:=22, EndColumn:=46 'copies text from screen
' Press EditCopy (Copy the selection and put it on the Clipboard).
'.Copy rcSelection, rcAsPlainText -- not needed
.Transmit qtyinput & CR
.Transmit CR
'strcheck = .readline("00:00:01")
'MsgBox strcheck
'If .WaitForString("Press " & Chr(34) & "RETURN" & Chr(34) & " to continue, " & Chr(34) & "^" & Chr(34) & " to stop: ") Then .Transmit CR
'Select Case strcheck
' Case strcheck Like "to continue"
' .Transmit CR
'Case strcheck Like "*Select CLIENT*"
' .Transmit CR
'End Select
.Transmit "^MED" & CR
.Transmit "3" & CR
.Transmit "10" & CR
First of all, Attachmate is the company, and they have a few products for accessing Mainframe sessions from Windows including EXTRA! and Reflections, both of which share a common scripting language, which is nice and easy to use from within VBA.
However, EXTRA! tends to have fewer commands available to use than Reflections, which is the more expensive product, so you have to get a little creative with your VBA.
I think you are using EXTRA!, so the command you are looking for is "GetString"
I use VBA to interact with a mainframe session in EXTRA!, and I know that my mainframe command is successful when three stars appear on the screen in a certain position.
The mainframe command can take anywhere between 1 second and 5 minutes to complete, so I use "GetString" to poll the mainframe session every second, waiting for the three stars before I continue:
Do Until Sess0.Screen.GetString(14, 2, 3) = "***"
Application.Wait (Now + TimeValue("0:00:01"))
Loop
The syntax for "GetString" is: GetString(Row, Column, Length)
In my case the stars appear at row 14, column 2, and I know there will always be 3 of them, so I set my string length to 3.