Word 2007: Get a list of building blocks? - vba

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

Related

Word VBA: Static Status dialog window

I've created a form that works well with macros running in the background to validate data and then print the document to a specific printer on the network.
The key element of this process is a production number value which I would like to keep a running log of and display in a static status dialog window. In other words, a popup window similar to a MsgBox that would not interfere with other actions on the form, but float on top of the document.
Visual concept of this would be...
User could shift the window away from their work if needed. Close the window if they desired, but pragmatically I want to re-pop/refresh the data in the window each time the background macro completes.
I can't use MsgBox, because it forces a closure of the window before the user can continue working on the document. I just want this visible to the user so they know what was last worked on and the few prior to that.
Any idea what control I might be able to use, or switch to MsgBox that would allow the user to continue working?
Ken...
PS: I found this and am trying to find a way to make this work for me. So far I have managed to get to function in the manner I want, but the lingering issue is how to call this PS script and include the information I need to display.
Alternatives to MsgBox in VBScript - StackOverflow
PPS: I opted to go a slightly different route and release the form with a MsgBox that is displayed at the end of the macro. I describe this in the solution noted below.
Instead of using a MsgBox, please consider using a VBA Userform. They're not much more complicated to use than a MegBox, but you can set them to be Modeless. Modeless dialogs remain open on-screen while you work on the Word document. Here'is Microsoft's page on setting dialogs as Modal or Modeless: Show method
If you search on VBA modeless dialog, you'll find many other helpful pages on the subject.
After doing much research, I've come back to revising my macro to incorporate static variables and a MsgBox at the end to report the last 5 production numbers that have been printed.
To provide a means of bringing up this MsgBox for reference, between printing runs, I created an OnlyNum variable as string and replaced the MsgBox I had for letting the users know they were only to use numbers in this field with that message. The end of that trap diverted the flow to the bottom of the macro (where the MsgBox that displayed the last five print jobs has been placed).
So, when the status MsgBox is displayed as a result of printing it only shows the last five events. If the trap captures it, it shows the message letting the user know to only use numerals and then displays the last five events.
Code reference:
Private Sub CommandButton1_Click()
Dim Prod As String
Dim Temp As String
Dim OnlyNum As String
Static ProdNum1 As String
Static ProdNum2 As String
Static ProdNum3 As String
Static ProdNum4 As String
Static ProdNum5 As String
'Check for only numeric value of TextBox1.Text
If Not IsNumeric(TextBox1.Value) Then
OnlyNum = "only numbers allowed" & vbCrLf & vbCrLf
Cancel = True
GoTo NotToday
End If
'Remove any spaces from TextBox1.Text
Prod = Replace(TextBox1.Text, " ", "")
'If the resulting lenght is equal to 7 Print it.
If Len(Prod) = 7 Then
ActiveDocument.PrintOut
'Update recent production numbers (5 in total)
ProdNum5 = ProdNum4
ProdNum4 = ProdNum3
ProdNum3 = ProdNum2
ProdNum2 = ProdNum1
ProdNum1 = Prod & " - " & Now() ' Insert a new production number with timestamp
TextBox1.Text = "" 'Clear the value of TextBox1.Text to prepare for the next Production number
Else
MsgBox ("Production Numbers must be 7 digits and contain only numerials.")
End If
NotToday:
Application.ActivePrinter = Temp
MsgBox (OnlyNum & ProdNum1 & vbCrLf & ProdNum2 & vbCrLf & ProdNum3 & vbCrLf & ProdNum4 & vbCrLf & ProdNum5)
OnlyNum = "" 'Reset value of OnlyNum
End Sub

Method 'Author' of object 'Revision' failed

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.

Is there a way to use Items.find() in VBA to extract certain text from Outlook?

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).

How can I get a list of supported languages, map language IDs to names, and show language selector in PowerPoint presentations?

I am working on a "fix language in entire document" script with a proper GUI for language selection. However, I am unable to programmatically generate a list of all languages PowerPoint knows including the language names in the user's own language.
For this reason, I am looking for the following:
A way to programmatically enumerate msoLanguageIds
A way to programmatically map msoLanguageIds to language names in the user's own language
In Word, I could use the Language object, but that doesn't seem to exist in PowerPoint.
Alternatively, a way to show the user a dialog that will set the DefaultLanguageID would be sufficient (I could grab the desired language from there).
I couldn't even find a way to set that via the GUI. Showing a similar language selector and getting the result would obviously do the job, too.
The target platform is Office 2007.
What about using Word Languages collection which contains Language objects in PowerPoint macro? So you can get the Language-Name for the MsoLanguageID Enum values. Here you can find the languages Office is available in.
' Powerpoint code
' add reference to word lib.
Public Sub test()
Dim wordAppliacation As New Word.Application
wordAppliacation.Visible = False
On Error Resume Next
Dim languageId As MsoLanguageID
For languageId = msoLanguageIDArabic To msoLanguageIDSpanishPuertoRico
Debug.Print languageId & ", " & wordAppliacation.Languages(languageId).Name & ", " & wordAppliacation.Languages(languageId).NameLocal
Next languageId
On Error GoTo 0
wordAppliacation.Quit
Set wordAppliacation = Nothing
End Sub
Or maybe just like this in PowerPoint 2016:
Dim lng As Word.language
Dim lid As Long
For Each lng In wordAppliacation.Languages
lid = lng.id
Debug.Print lid & ", " & wordAppliacation.Languages(lid).Name & ", " & wordAppliacation.Languages(lid).NameLocal
Next lng

How to loop and write all HTTp Header name/value pairs

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.