How to show the progress of the "Fields.Update"-Method in VBA (Word) - vba

I'm trying to update all the fields in a Word document using the "Fields.Update"-Method. This is the code I use:
Sub UpdateFields()
Dim varRange As Range
'Update fields in the first section of the main text
set varRange = ThisDocument.StoryRanges(wdMainTextStory)
varRange.Fields.Update
'Update fields in subsequent sections (of the main text)
While Not (varRange.NextStoryRange Is Nothing)
Set varRange = varRange.NextStoryRange
varRange.Fields.Update
Wend
End Sub
There are over 750 fields in the document (linking to cell content in an/one Excelfile) so the update takes a while so for the user it looks like the application "freezes". When I update "manually" (selecting via Crtl+A and updating via F9), I get a progress bar like this within the status bar at the bottom of the word application window:
However it does not appear using the "Fields.Update"-Methode. Is there a way to show this Bar using the Filds.Update-Method or any other Method?
I suppose I could make my own "progress"-bar via a userform. But this would require to update each field individually (e.g. through cycling through all fields indevidually rngCurrentRange.Fields(i).Update) but I would rather avoid that. It makes the code more complex, slower and it's a pain in the ass to make sure Word keeps the userform updated in real time. But if there's no other solution I'll take that as well.
EDIT:
I found a workaround, but it's not working 100%, please see my answer...
Please Note: I also posted two other questions within this context:
Update multiple fields with Excel links very slow
VBA: force user form to update in real time

I found a workaround using Fields.Update in combination with selection:
'Do Field Updates
Dim rngCurrentRange As Range
set varRange = ThisDocument.StoryRanges(wdMainTextStory)
rngCurrentRange.Select
Selection.Fields.Update
But there is still a problem with this workaround: At first it doesn't seem to show a progress bar either, but when you run it a second time it does! I'm not sure why this is, but it made it possible to make a workaround: First I update some insignificant StoryRange, which will initialize the progress bar for the following Fields.Update-methods. Unfortunately this does not work for the "first approach" (without using selection).

Related

How can I make a form instance open a specific record on creation?

I'm trying to optimize the performance of my access frontend. One of the problems I'm stumbling on is how to set a multi-window form to open immediately to a specific record, because at the moment it queries everything twice.
Essentially, I allow my users to open multiple instances of a form. That way, a user can compare multiple records by placing the windows of those forms side by side.
At the moment, my code looks like this:
Set frm = New Form_Name
frm.RecordSource = "select * from Table where id = " & ID 'ID is a variable passed to the method
I'm pretty sure back then this question was one of the building blocks I relied on.
The problem is that on the first line, access already entirely opens the form and does everything the form does when opening, such as Form_Open, Form_Current, and loading subforms. Then, when I set the recordsource, it does all (or most) of that again, which significantly slows down the process of opening the form.
Here are some of the things I've tried:
Changing the Form_Current to recognize that it's being "used" twice and only actually run the code once. The problem is that the code is already very optimized and doesn't seem to be the bottleneck, so this doesn't seem to do much. Actually opening the form seems to be the bottleneck.
Trying to change the properties of the original form so that it opens the specific record I need, but I can't seem to change the properties without it opening the form.
Looking for a way to supply arguments to the form. That doesn't seem to be supported in VBA.
One other idea that popped into my mind was creating a variable in vba with the ID of the record, setting recordsource in the form properties to fetch that variable, but then, when I would open another form and change that ID, the form
I realize that I can do this with DoCmd.OpenForm, but that doesn't give me the option to open multiple instances of the same form.
How can I achieve this? This would mean a significant performance improvement.
OK, so I actually wanted to ask this question. But just before I hit the submit button, I had an idea...
Here's the idea: you set a global variable in VBA, which you then access in the form filter command. In my case I just added Public OpeningID As Long at the top of my VBA module. Then I create a function to get that value:
Public Function getFormID() As Long
getFormID = OpeningID
End Function
Then, in the form, you can set the filter criteria as ID = getFormID(). And then when you open the form instance, you do it like this:
OpeningID = ID
Set frm = New Form_Name
OpenindID = 0 'reset this to make sure that if it's being accessed when it shouldn't, it generates a clear error
The only problem is what happens when you refresh the form, as that will call the method again. When you refresh the form via VBA, you can set the OpeningID beforehand, and to avoid users refreshing the form, you can add this to your form (don't forget to turn on Key Previews in the form settings):
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF5 Then KeyCode = 0
End Sub
Bang. Forms open almost twice as fast. Awesome.

Uncheck "Formatting" in MSWord tracked changes Markup Options? (Office 365 for Mac v16.26)

I want to use an AutoOpen() sub for all docs to (1) turn on tracked changes [this part is successful] and (2) stop Word from displaying formatting changes in the sidebar. Word defaults to displaying all comments and formatting in balloons, which is what I want, but I don't actually need to track or review formatting changes.
See ![screenshot]https://imgur.com/a/28ARVob for a screenshot of what I want to accomplish automatically via Macro. When I choose Record Macro, uncheck Formatting, and stop recording, no code has been generated in VBE. There doesn't seem to be an available property in VBA for Word (Mac or otherwise) that will accomplish this goal.
Here's my current AutoOpen() code:
Sub AutoOpen()
ActiveDocument.TrackRevisions = True
With ActiveDocument
.TrackFormatting = False
End With
End Sub
Turning off tracking formatting, as I'm currently doing, only works going forward, so all existing changes will still be displayed until I uncheck the Formatting option. It's a partial solution, at least.
I'm also aware there is a property to show/hide revisions as a whole (i.e. I could include the code: .ShowRevisions = False), but that's not quite what I'm looking for here, since I only want to hide the formatting (I would expect it to be something like .ShowFormatting, but that is not a property in VBA).
Any thoughts/suggestions are welcome.
Those settings are in a different part of the object model, under the View object. Here's a code snippet that shows how to turn off the display of tracked changes. Note that this doesn't remove them from the document, it only supresses the display. In order to remove these revisions from the document it would be necessary to accept or reject the changes.
Dim doc As Word.Document
Set doc = ActiveDocument
doc.TrackRevisions = True
doc.TrackFormatting = False
doc.ActiveWindow.View.ShowFormatChanges = False

Update or refresh Word ContentControls

I have a scenario whereby I need to (programmatically) add ContextControls to a Word table.
My example is when a user adds a ContentControl to a table (first cell) then adds a repeating ContentControl to that row - this is perfect!
If the user then goes to cell 2 of a 2 cell table (to keep things simple) and adds another ContentControl - keeping in mind this row is already a repeating ContentControl and now just has an additional ContentControl added, the data does not repeat.
If I go into my Word ribbon - Developer, then flick design on and off, the data all appears fine again (almost like the repeating ContentControl was updated / refreshed). I'm wondering - is there a way to do this through code?
Something like repeating ContentControl.Update / Refresh / Reload (none of those exist).
Right now - I'll even accept if I can do this through the Word application itself, but I will be converting this to code.
For anyone needing the answer to this:
After 3 days, I've decided that the best way to accomplish this task, is by calling the .ToggleFormsDesign method twice.
This will basically "refresh / update" the binding on the repeating ContentControl
With ActiveDocument
.ToggleFormsDesign
.ToggleFormsDesign
End With
Calling this method back to back, will have no UI / visual impact (i.e. the user will not notice anything).
MSDN Link to the Method

Excel ActiveX ListBox Shrinks with each update

I have a set of linked subs which work like this:
A user types into an ActiveX TextBox
A Change Event in that TextBox calls a sub in a Module
That Module sub drives updating a named range in a sheet
The range value drives updating a table of Excel cells that uses lookup functions based on the range value
The table values are copied and pasted to a another range (to eliminate links to formulas)
That pasted range is put into a ListBox using this (props to Rory for his patience):
ActiveSheet.ListBox1.List = Sheets("Search Criteria Control").Range("G1:G21").Value
The result is that for every character the user types in the TextBox the ListBox is updated.
The problem I have is that the ListBox shrinks a bit with every keystroke in the TextBox referred to in #1 above. Is this normal behavior and I'm misusing ListBoxes, am I doing something wrong or do I need to respecify the dimensions of the ListBox every time it is updated with something like this?
ActiveSheet.OLEObjects("ListBox1").Top = 35
ActiveSheet.OLEObjects("ListBox1").Left = 650
ActiveSheet.OLEObjects("ListBox1").Width = 550
ActiveSheet.OLEObjects("ListBox1").Height = 610
Thanks in advance for any thoughts on this.
I was having trouble with the same thing. My ActiveX listbox would move around on the sheet and change size for no reason that I could see.
While I did go ahead and develop some code to reset size and coordinates, that wasn't satisfactory since there had to be a mechanism to trigger that code - something I didn't want to burden end-users with.
I found a better answer in another user forum. There's a Listbox Property called IntegralHeight whose default property is True - something to do with screen resolution and optimal display of listbox contents. Simply set that to False. I did that with some ActiveX boxes that were giving me fits, and I was able to disable the "adjustment" code and, so far, so good!

VB in Access: Combo Box Values are not visible in form view but are visible through Debug.Print

Code in Form onLoad:
country_combo.RowSourceType = "Value List"
Code in a reset function:
Dim lListIndex As Long
With Me.country_combo
For lListIndex = .ListCount - 1 To 0 Step -1
.RemoveItem (lListIndex)
Next lListIndex<br/>
End With
Code to populate country combo:
*For n = 1 To numCountries*
*countryCombo.AddItem (countryRS.Fields("countryName"))*
*countryRS.MoveNext*
*Next n*
I'm having a problem that occurs AFTER the code to populate the country combobox runs. The values are there as I can run Debug.Print(countryCombo.Value) and it prints out the name of the selected country, but I can't see the values in the combobox at all. They're invisible, and as far as I know there is no visiblity property for specific items, unless I'm completely mistaken.
comboBoxError.png http://img110.imageshack.us/my.php?image=comboboxerror.png
I think you should probably use Access's GUI tools to do what you're looking for. In design mode, click on the field you are trying to populate, then click the "lookup" tab. You can then specify a table to populate the field with and your forms should automaticly update as well.
I've also seen what you describe here - as far as I can tell, it's a bug within Access (I was using 2007) that only occurs when you programatically mess with the contents of a combo box. It does not happen every time. The issue corrects itself if you highlight the text that is in the combo box.
I am experiencing a similar issue with Access 2003. Based on the selection of one combo box, the row source of a listbox is set to an SQL string Basically a SELECT DISTINCT [MyField_Selected] FROM MyTable. For some fields the values are visible in the list box and others it is not. The values are there however as I can access them via code. To make it more interesting it works fine in Access 2007.
Just found the resolution on another forum. Check the format property of the field(s) in question on the table. In my case, when Access 2007 created the table, it put an # format in there. I removed that and all works great!