I'm writing a code to select a specific autotext from a dropdown list. I've not written code since the late 1970s the old Basic A...I've forgotten most of it.
The dropdown list is called HVAC
the text to insert is in an autotext file called "Split Systems" or whichever text associated with the dropdown items
The error message is "5941 the requested member of the collection does not exist"
I've tried using the value of 1,2,3,or 4 in the if statement line instead of the "Split...", it doesn't work either
Where is issue or how should I code it.
MY code follows:
Sub one()
If ActiveDocument.Formfields(hvac).DropDown.Value = "Split Systems" Then GoTo 10 Else GoTo 20
10
ActiveDocument.Content.Select
Selection.Collapse Direction:=wdCollapseEnd
ActiveDocument.AttachedTemplate.AutoTextEntries("Split Systems").Insert _
Where:=Selection.Range, RichText:=True
GoTo 100
20
If ActiveDocument.Formfields(hvac).DropDown.Value = "Packaged Systems" Then GoTo 25 Else GoTo 30
25
ActiveDocument.Content.Select
Selection.Collapse Direction:=wdCollapseEnd
ActiveDocument.AttachedTemplate.AutoTextEntries("Packaged System").Insert _
Where:=Selection.Range, RichText:=True
GOto 100
30
If ActiveDocument.Formfields(hvac).DropDown.Value = Central Heating System" Then GoTo 35 Else GoTo 40
35
ActiveDocument.Content.Select
Selection.Collapse Direction:=wdCollapseEnd
ActiveDocument.AttachedTemplate.AutoTextEntries("Central Heating System").Insert _
Where:=Selection.Range, RichText:=True
GoTo 100
40
If ActiveDocument.Formfields(hvac).DropDown.Value = "PTACs" Then GoTo 45
45
ActiveDocument.Content.Select
Selection.Collapse Direction:=wdCollapseEnd
ActiveDocument.AttachedTemplate.AutoTextEntries("Central Heating System").Insert _
Where:=Selection.Range, RichText:=True
100
End Sub
Tried everything I know
Word now has Building Block Gallery Content Controls that eliminate the need to add code to dropdowns. Choose the Developer tab, then choose Building Block Gallery Content Control in the Controls group. Use the Properties button in the Controls group to point the control at AutoText. The only significant drawback is that this control doesn't currently work in Word for Mac.
Why are you trying to reinvent the wheel?
Look at AutoTextList and Building Blocks Gallery Content Control
These two features built into Word create drop downs of AutoText or other Building Blocks. What are you trying to do that cannot be accomplished using one of them?
Here is my writing on the Building Blocks Gallery Content Control.
Here is a link to a sample template with both.
The Content Control gives a very nice drop-down gallery.
Here is a link to vba to insert AutoText/Building Blocks. (It varies depending on the location of the AutoText.)
Note: all links are to my writing.
Use Field Coding rather than vba for this
Since you want to select something in your list and have the AutoText appear in a different location in the document, this could be done with either an AutoText field or with an IF Field.
The AutoText Field would only be useful if what is in your dropdown is the exact name of an AutoText entry's name. This is unlikely.
However, the AutoText Field could be combined in an IF Field that tests the choice in the dropdown.
Here is an example where the choice in the dropdown is "Choice 1," the bookmark name of the Dropdown is "Location" and the corresponding AutoText entry is named "TextPart123."
The properties in your dropdown must include Calculate on Exit checked.
{ IF { Location } = "Choice 1" "{ AutoText TextPart123 }" }
For the dropdown to work, that part of the document must be protected for filling in forms.
Here is a temporary link to a sample document with a dropdown and fields like the one above. https://www.dropbox.com/s/wbfnxoji0uvoikd/deleteme%20dropdown%20for%20conditional%20autotext.docx?dl=0
The first two choices trigger different building blocks. The third choice does not trigger anything.
To trigger the change, you need to exit from the dropdown. Typically this is done using the Tab key.
Note that because there are currently no AutoText entries built into Word, the AutoText field in the sample calls a couple of Watermarks that are built in. The field works with any Building Block, including AutoText.
If you absolutely need to use vba to insert AutoText rather than use the AutoText field, see my page on Boilerplate in Word, specifically on using vba to insert building blocks. As you've discovered, with the entry of Building Blocks in 2007 the code to insert became finicky.
Related
I suspect my strategy is incorrect as I can't seem to apply my search results for the keywords I've used over the last few days. ( https://stackoverflow.com/questions/54496552] seems to be the closest. )
I am able to populate a single document from the two forms I've built and save it under a new name
...
MsgBox "Saving as " & aFullPath
ActiveDocument.SaveAs FileName:=aFullPath, FileFormat:=wdFormatDocumentDefault
But that changes the name of my parent document that contains my forms (Word2016 document name: waiver.docm). In practice, that won't be a problem because the user will not be saving "waiver.docm" except by accident.
But that's why I think my approach is wrong.
Ideally my VBA code would
Load a protoype waiver template with the page heading, bookmarks and table as I have now in my waiver.docm.
Upon filling that template, append another waiver template as a new page.
And return control to the form
Repeat above two steps of appending of sheets until the user indicates completion (e.g. "Finish" command button). Typically after 1 to 4 pages
Print and save the entire document.
Right now I interrupt the process after each page to force my prospective user to print and save the document (and under a unique name).
I have a Word VB macro that automatically inserts a Visio drawing in a two-column layout. The code works well, but I would like to further automate the process. To do so, I am looking specifically for code that will move the cursor to the end of the first column of the selected page. This column typically does not already have a column break, so my solution can't involve searching for column breaks. And I believe I already have code that selects the current page, just not the end of the first column on that page.
I am able to move the cursor to the end of the page programmatically, and I am able to move the cursor to the top of the page programmatically. I just can't get the cursor to move to the end of the first column of text. All of the online examples I've found so far focus on moving to the end of a column in a table, but not to the end of a column of text in a two-column layout.
10/15/2019: Here's an example page as requested by #cindym
[SamplePage][1]
The document may be several pages long. My current macro works well and meets company requirements for this kind of document, but it requires the user to place the cursor where they want the two-column graphic inserted. I want to make the process more automated (check "top" or "bottom" in an existing userform and have the macro place the two-column figure accordingly on the current page).
Again, I am able programmatically move the cursor to the top or bottom of the page just fine, I just can't get the cursor to the end of the first column (the left-hand column on the two-column page). Our code inserts continuous section breaks and works well if it can insert the first of those breaks at the end of the left-hand column of text.
Based in part on CindyM's suggestion, I tried the following for a two-column Visio diagram at the bottom of the current page, but things go wrong after the third line of code. Other than this initial VBA-driven cursor placement problem, the rest of my code works fine (selects the Visio, inserts continuous breaks, inserts a caption for the figure, etc.). Hope this helps clarify. Thanks again!
`ActiveDocument.Bookmarks("\Page") `Select selects current page`
Selection.GoToNext (wdGoToPage) `cursor at top of page 2`
Selection.MoveEnd wdCharacter, -1 `cursor now at end of target page`
Selection.MoveRight Unit:=wdItem `cursor now in headline, top of page :(`
Selection.MoveRight Unit:=wdItem
[1]: https://i.stack.imgur.com/OR7BF.png
As no code was included in the question as a starting point, the following uses GoTo to move to a specific page. The assumption is that this position is not a newspaper column. Then the equivalent of pressing Alt+Down Arrow twice in the UI to move to the next TextColumn (newspaper column) is performed, which would be the beginning of the next column after the first. The selection is then moved back one character, to put it at the end of the column.
If the top of the page is a newspaper column and the code should move to the end of that column, then remove one of the Selection.MoveRight Unit:=wdItem lines.
Sub MoveToEndOfNextNewspaperColumn()
Dim goToPageNr As String
goToPageNr = "1"
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=goToPageNr
Selection.MoveRight Unit:=wdItem
Selection.MoveRight Unit:=wdItem
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
End Sub
I'm setting up a set of "boilerplate" Word documents.
All the documents have different Footers for odd and even pages.
The Footers are left- and right-hand biased so that double sided printing will always show important information at the thumb edge of the page.
The document printing default is double sided, so the Header/Footer "Different Odd & Even Pages" box is checked.
However there will be times when a user will want to print single sided.
The box will need to be unchecked.
Each document contains a UserForm to manage features (import external values for Document Variables etc.)
I would like to control the "Different Footer Odds & Even Pages" feature from the UserForm via a pair of Option Buttons.
I've searched multiple site to no avail.
The closest that I gotten is the sample code from the OddAndEvenPagesHeaderFooter Help topic:
Set myDoc = Documents("Document1")
myDoc.PageSetup.OddAndEvenPagesHeaderFooter = True
With myDoc.Sections(1)
.Headers(wdHeaderFooterPrimary).Range _
.InsertAfter "Odd Header"
.Headers(wdHeaderFooterEvenPages).Range _
.InsertAfter "Even Header"
End With
However I don't want to create footers. I just want to toggle the PageSetup.OddAndEvenPagesHeaderFooter between True and False
I have tried this "stripped down version:
'Set OddFootersOnly
With ActiveDocument.PageSetup
.OddAndEvenPagesHeaderFooter = False
End With
I get the following Error Message
Run-time error: '4608': Value out of range
I prepare a MS Word file with a large amount of news articles, each followed by a full, long clickable hyperlink (e.g. http://www.newssite.com/this-is-the-name-of-the-article.html).
I need to replace all the hyperlinks in the file with a single word: "Link", while keeping their url adresses and click functionality. I can use MS Word "change hyperlink" dialogue to do that, but doing it by hand takes incredible amount of time.
The hyperlinks, however, are not always formatted as 'hyperlink' style. Is there any condition for MS Word Macro replacement that will lookup hyperlinks by their functionality, not by their style or text?
Despite this task seems to be quite common, I could not find anything like it in the web.
Since you have the hyperlinks already created. It should just require looping through the documents hyperlinks collection and changing the 'TextToDisplay' property. This should get you started:
Public Sub ChangeHyperlinksText()
Dim hlink As Hyperlink
For Each hlink In ThisDocument.Hyperlinks
hlink.TextToDisplay = "Link"
Next hlink
End Sub
I took an old MS Word document to adapt it with a new layout.
I finished last week and everything was working fine, the main macro has to hide or display some text.
For this, a zone of text is "bookmarked", and then we get this bookmark and set its font to hidden:
ActiveDocument.Bookmarks("MyBookMarkname").Range.Font.Hidden = True 'Or False
It's how it was done on the old document, and I had only to do the same on the new document(recreate those bookmarks).
But today, when trying again to make this action, the text isn't hidding anymore! When it is supposed to be hidden, the text is like underlined by a small blue line(the same line you have when an word is not spellt correctly, but in blue).
I searched online, I found several things, but none of them worked:
Private Sub HideHiddenText()
For Each myWindow In Windows
myWindow.View.ShowHiddenText = False
Next myWindow
End Sub
I've no "revision mode" enabled either.
What could be wrong?
I believe the wavy blue line that Word is displaying is being triggered by the hidden text because Word uses the blue line to mark formatting inconsistencies. To get rid of the line in Office 2007/2010 go to
Office Orb Menu (2007) or File Menu (2010)|Options|Advanced
and uncheck Mark formatting inconsistencies
The wavy blue line, however, has nothing to do with your hidden text being displayed. I believe this is happening because the "Show/hide formatting marks" function is turned on. To make sure your hidden text is kept hidden by vba, you will need the following:
With ActiveDocument
.ActiveWindow.View.ShowAll = False 'Hide all formatting marks
.ActiveWindow.View.ShowHiddenText = False 'Do not display hidden text
.Application.Options.PrintHiddenText = False 'Do not print hidden text
End With
It is worth noting that an experienced Word user can always choose to display hidden text via Word's user interface and that if this is to be avoided, a great deal of additional work would need to be invested to disable the native Word functions that can be used to display hidden text (if that is even possible).