Word Macro Find Last Line of current page - vba

In MS Word using a macro, I would like to find the last line of the current page and add a "page break". Although this should be easy I can't find the script for the last line of the current page.
Sub LastLineInsert()
Selection-end 'is supposed to work to find last line
Selection.InsertBreak Type:=wdPageBreak
EndSub

The easiest way is probably to use Selection.GoToNext(wdGoToPage) and then back up one character. So:
Selection.GoToNext(wdGoToPage)
Selection.MoveEnd wdCharacter, -1
Selection.InsertBreak
Note that doing this where Word has already made a page break can result in a blank page being added. Be careful about this if you're trying to loop.

Related

Jump to found text in document

I am programming a loop which searches for highlighted text in a document.
If a text is found it should be shown in the Word window similar to the non-vba search function that you have in word.
How can I achieve this?
Here is the code that I use to find the highlighted text, and it works well.
But I don't now how to show the found instances in the word document.
With ActiveDocument.Range
.Find.Highlight = True
Do While .Find.Execute
' Jump to found text and prompt user to take action.
Loop
End With
PS: Once an occurrence is found, I want to prompt the user via to take action on this part of the text. The context is important for the user to decide, therefore he has to see the text. I'm clear on the prompting part, but I cannot figure out how to show the text to the user.
You need to use the .Select method. I'd recommend doing this on a .Duplicate of the current found range. e.g. Inside your do loop use '.Duplicate.Select' (NOT .Find.Duplicate.Select)

Need Word VBA code to move cursor to end of first column in two-column page layout

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

Autocorrect Word 2010 (VBA)

For my problem I tried different things and none worked: I am flexible about the way to actually solve it, so I think it's best to begin with the purpose.
I get long word files and I need to do some quality checks and proofreading. There are a lot of words in the file that are not capitalized correctly. I want to get this in automatic (no a normal grammar software/add-on won't do)
I have all the entries in my auto correct file, in word files (quite huge better not use them) and in an excel spreadsheet (wrong entry - correct entry).
Refreshing the document doesn't do the trick. If I pick a word from the list and select it and press the space-bar then it gets changed so I "wrote" a Macro for that
Sub Try()
For Each sentence In ActiveDocument.StoryRanges
For Each w In sentence.Words
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.TypeText Text:=" "
Selection.TypeBackspace
Next
Next
End Sub
The thing "worked" i.e. it runs and you can see the cursor speeding through the screen but no words get changed. So I am thinking that Autocorrect get's somehow disabled while Macros are running.. Anyone has a hint? ideally knows a way around it?
Worst case I thought of putting the text in an excel file and writing a macro that for each line of text checks each word against my original list and "if match then substitute" but it doesn't seem too easy (at least for me)
Cheers
I hope that I could explain myself
This Pseudo Code / Outline should get you started:
In Word you can create an instance of Excel and open your spreadsheet:
https://stackoverflow.com/a/24196120/3451115
You can then Loop through your list of WrongWords:
https://stackoverflow.com/a/1463308/3451115
For each WrongWord do a Find and Replace:
VBA to find and replace a text in MS WORD 2010

Delete page in Word

I need VBA code to delete all sections(pages) except first in Word document
For this I use below code.
For Each oSec In ActiveDocument.Sections
If oSec.Index <> 1 Then
oSec.Range.Delete
End If
Next oSec
This works but does not delete second section only removes its content.
If I removes if condition in code it removes content of first page.
I want to preserve content of first page.
Please tell me where I am making mistake.
When deleting you need to include section break marks. Try to change this line:
oSec.Range.Delete
into this one:
ActiveDocument.Range(oSec.Range.Start - 1, oSec.Range.End).Delete
BTW, you should not think that page=section, they are different type of document units.

Using VBA in Word, how can I find a specific piece of text and continue numbering in an outline

Using VBA in Microsoft Word, how can I automatically search for a specific piece of text, remove that piece of text and make Word continue a previous list. I can record a macro of the action of clicking the list button in Word, and it gives me some code involving Selection.Range.ListFormat.ApplyListTemplateWithLevel. I would like to be able to figure out how to find a piece of code and then automatically continue the previous list.
Here's what I have before the code starts:
First sentence
Second sentence
Third sentence
Fourth sentence
*/R*Fifth sentence
Here's what I want to have after the code finishes:
First sentence
Second sentence
Third sentence
Fourth sentence
Fifth sentence
Can you provide some more context to why you're looking to do this? This code will accomplish what you asked for (as long as the list already exists, and */R* is inside the list), but I imagine there's a better way.
With Selection.Find
.Text = "*/R*"
.Wrap = wdFindContinue
End With
If Selection.Find.Execute = True Then
Selection.TypeParagraph
End If