I'm trying to change pages while Microsoft Word is in "Read Mode"* by using a VBA code, and I can't seem to find any function that allows that.
(* Please notice that I'm talking about the "Read Mode" layout that you can change with the bottom-right icons in word, and not the "Read Only" mode that you sometimes get when you open a document from an unknown source)
In "Print Layout" it is simple since I can use something like -
Selection.GoTo What:=wdGoToBookmark, Name:="LastReadingPoint"
But It won't work while I'm in "Read Mode".
None of these work:
ActiveDocument.GoTo What:=wdGoToPage, which:=wdGoToAbsolute, Count:=1
ActiveDocument.Range.GoTo What:=wdGoToPage, which:=wdGoToAbsolute, Count:=1
Selection.GoTo What:=wdGoToBookmark, Name:="LastReadingPoint"
ActiveDocument.Range.GoTo What:=wdGoToBookmark, Name:="LastReadingPoint"
ActiveDocument.Range.GoToNext wdGoToPage
ActiveDocument.GoTo What:=wdGoToBookmark, Name:="LastReadingPoint"
(LastReadingPoint is a bookmark I've added to the text)
Any ideas or hacks?
Here are a few options, none of which are very good.
Use ScrollIntoView. This function scrolls so the provided range is visible somewhere on the screen. If the range is already on screen, nothing will happen. If it's off-screen, it seems to end up being positioned about 1/4 of the way down, similar to what happens when you follow a hyperlink.
ActiveWindow.ScrollIntoView ActiveDocument.Bookmarks("LastReadingPoint").Range
Follow an existing hyperlink within the document. This will position the target about 1/4 of the way down rather than at the top like the navigation pane or, in Word 2010, GoTo with a bookmark.
Dim h As Hyperlink
For Each h In ActiveDocument.Hyperlinks
If h.SubAddress = "LastReadingPoint" And h.Address = vbNullString And h.Type = msoHyperlinkRange Then
h.Follow
Exit For
End If
Next h
Create a new hidden document with a hyperlink to the bookmark and follow it. This requires your existing document to have been saved at some point so it has a name and path. It will also increment the counter used to name new documents.
Dim d As Document
Dim h As Hyperlink
Set d = Application.Documents.Add(Visible:=False)
Set h = d.Hyperlinks.Add(d.Range(0, 0), ActiveDocument.FullName, "LastReadingPoint")
h.Follow
d.Close False
Temporarily exit reading mode. The screen will flash and the ribbon will briefly appear, even if you turn off ScreenUpdating.
Dim readingMode As Boolean
readingMode = ActiveDocument.ActiveWindow.View.ReadingLayout
If readingMode Then ActiveDocument.ActiveWindow.View.ReadingLayout = False
Selection.GoTo wdGoToBookmark, , , "LastReadingPoint"
If readingMode Then ActiveDocument.ActiveWindow.View.ReadingLayout = True
After posting my answer, I was suggested with a superior answer.
My original answer:
At this point, I think that there is no way to control "Reading Mode" layout from VBA. Hence, I overcame this limitation by sending "Right arrow" key and "Left arrow" key to move one page forward and one page back respectively.
Private Sub BTN_IncPage_Click()
Word.Application.Activate
SendKeys ("{RIGHT}")
BTN_IncPage.SetFocus
End Sub
The superior answer:
source: https://social.msdn.microsoft.com/Forums/en-US/a5a198f4-f56c-4489-8ab4-3bb96db6a7f3/can-vba-change-pages-in-microsoft-words-8220read-mode8221-layout?forum=isvvba
ActiveWindow.ActivePane.PageScroll down:=1
Related
Hello I have a 2 pages word document with shapes and text boxes on each page. I would like to be able to hide the 2nd page or to show it.
I know we could hide a sheet on excel like bellow but I didn't find out how to do it in word :
Worksheets("Sheet1").visible = False
So I tried with this macro :
Sub HidePage2()
ActiveWindow.DocumentMap = True
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
CommandBars("Navigation").Visible = False
End Sub
I thought I was hiding my page but in fact, it deleted it because if I replace the end of the code by the following code, it do not show my page number 2.
CommandBars("Navigation").Visible = True
I don't know how to fix my problem, could you help me?
Paragraphs and sections are Word VBA objects, pages are not. You can apply a bookmark to each group of paragraphs that you want to hide, plus the page break at the bottom of the page, if there is one. Then use code like this to show or hide it:
Sub HidePage2()
ActiveDocument.Bookmarks("Page2Bookmark").Range.Font.Hidden = True
End Sub
Sub ShowPage2()
ActiveDocument.Bookmarks("Page2Bookmark").Range.Font.Hidden = False
End Sub
Macropod's comment about the printer driver is relevant, because the pagination of the document can change if you have a different active printer. Then what you thought was on page 2 could partially be on page 1 or 3. You can minimise this problem by adding page breaks before and after page 2 and ensuring that page 2 is not completely full.
For example, without needing to bookmark the page beforehand:
Sub Demo()
ActiveDocument.Range.GoTo(What:=wdGoToPage, Name:="2").GoTo(What:=wdGoToBookmark, Name:="\page").Font.Hidden = True
End Sub
Sometimes I have code blocks in my Word documents, and I want to work with them without copying to plain text editor.
Namely, I want to have an ability to indent/unindent multiple lines of code using "Tab" character. This task is very simple in any plain text editor or IDE, but for the sake of clarity, I will show it here. Tabs are shown as black arrows:
Initial state
Using the Shift key or mouse, I selected a part of JavaScript function
Then I pressed Tab key on my keyboard
Selected lines were indented by inserting tab character on each line.
How it could be done with VBA?
Since I don't post any code (as evidence of my own efforts), I don't expect to get something completely working. But at least, I hope to get an understanding "how" it could be done.
As David suggested, I recorded a macro. Here how it looks:
Sub Indentator()
Selection.TypeText Text:=vbTab
End Sub
The problem is, that I don't understand how to get it work for multiple lines. If I select them, this macro (and it was not surprise for me) just inserts "Tab" instead of selection.
Insert a tab character at the start of each paragraph in the selection:
Sub Indentator()
Dim para As Paragraph
For Each para In Selection.Paragraphs
para.Range.InsertBefore vbTab
Next
End Sub
(This assumes that each of your code "lines" is a new "paragraph" in Word, which it usually would be if you are intending to copy/paste this to/from actual code.)
If the macros are named IncreaseIndent and DecreaseIndent, they can be run using the Increase and Decrease Indent buttons on the Home tab.
Sub IncreaseIndent()
If Selection.Start = Selection.End Then
Selection.InsertBefore vbTab
Selection.Start = Selection.End
Else
Dim p As Paragraph
For Each p In Selection.Paragraphs
p.Range.InsertBefore vbTab
Next
End If
End Sub
Sub DecreaseIndent()
If Selection.Start = Selection.Paragraphs(1).Range.Start Then
Selection.Start = Selection.Start + 1
End If
Dim p As Paragraph, c As Range
For Each p In Selection.Paragraphs
Set c = p.Range.Characters(1)
If c.Text = vbTab Then c.Delete
Next
End Sub
Reference https://wordmvp.com/FAQs/MacrosVBA/InterceptSavePrint.htm
I have a VBA Code to resize objects in PowerPoint including Font size, margins and everything else. But I haven’t found a solution to update/change an existing TapStop. There is a the Ruler Object with different levels und a default value. I double checked also the TextRange Object with Characters.
Are there any ideas to update the TabStop size?
Here is an example of a TextBox, i would like to resize:
TextBox Example
Shape.textframe.ruler.tabstops.count is always 0, if I "take" just the shape by For-Each-Loop. If I select it manual, it's also 0 at the sub menu TabStops of Paragraph menu.
If I click inside the shape (blinking cursor) and open the TabStops menu again, I see one TabStopPosition.
How can I access this information by VBA?
I tried it already by Line.Selection and nothing works.
Thanks!
Moe
PowerPoint used to allow only one set of paragraph settings per textframe (ie, per shape). That changed in PPT2007; now each paragraph can have its own tab and other settings. Have a go with this:
Sub ShowMeTabs()
Dim X As Long
Dim lTabCount As Long
With ActiveWindow.Selection.ShapeRange(1).TextFrame2.TextRange
For X = 1 To .Paragraphs.Count
Debug.Print X
With .Paragraphs(X).ParagraphFormat
For lTabCount = 1 To .TabStops.Count
Debug.Print .TabStops(lTabCount).Position
Next ' Tab
Debug.Print "Level:" & .IndentLevel & " Position:" & .LeftIndent 'etc
End With
Next ' paragraph x
End With
End Sub
I am trying to figure out a way to automate the navigation pane so that when you open a document, it automatically searches and shows results for all of the word comments in that document. The manual way to do this is to click the drop down arrow next to the navigation search bar, select comments under find and then select all reviewers. Is there a way to automate this?
Thanks!
Code below:
* The code works as it should but it does not actually display any comments. If you do this process manually, it will display all the comments in the document. If I use this macro, it will say no results found, even if the search bar has the same info. I've tried defining a range as the entire document but it hasn't worked.
Sub AutoOpen()
' Activate/Deactivate Navigation Pane
' Collapse All, to only show heading level 1
Set myRange = ActiveDocument.Content
ActiveWindow.DocumentMap = True
If ActiveWindow.DocumentMap = True Then
' Ctrl-F = Find --> this sets the cursor at the top of the navigation pane
' ^ = Ctrl key
' + = Shift key
SendKeys "^(f){TAB}{ENTER}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}
{ENTER}{DOWN}{ENTER}"
End If
ActiveWindow.View.ShowComments = True
End Sub
I'm not sure it is an exact answer to your question, but your attempt helped me find a creative way of using the new Word2016 navigation pane. I designed a userFrom to select special characters to be searched. In my case, to have navigation pane to display items, I had to dismiss the form and perform an ActiveDocuemnt.activate. Then and only then the navigation pane showed its results. Thanks.
DoEvents
Application.ScreenUpdating = False
' put text in word, copy to clipboard, and undo it
Dim s As Long
Dim e As Long
s = Selection.start
Selection.TypeText "the text I want to search that comes from my userform"
e = Selection.start
Selection.SetRange start:=s, End:=e
Selection.Copy
ActiveDocument.Undo
Application.ScreenUpdating = True
' make document map active
ActiveWindow.DocumentMap = True
' fortunately input field has focus, erase any content, paste
SendKeys "^(f){backspace}^{v}{Enter}"
Me.hide ' I hide my user form
ActiveDocument.Activate ' i activate the active document to see the result
I'm trying to add a custom building block at the click of a button in MS word 10. Below is the code currently attached to my activeX button.
Private Sub CommandButton1_Click()
Dim objTemplate As Template
Dim objBB As BuildingBlock
' Set the template to store the building block
Set objTemplate = ActiveDocument.AttachedTemplate
' Access the building block through the type and category
Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustom5) _
.Categories("General").BuildingBlocks("Experience")
' Insert the building block into the document replacing any selected text.
objBB.Insert Selection.Range
End Sub
My problem is, as this code is invoked at the click of a button, the button becomes the "Selection.Range" and is thus replaced. I looked all around for alternate codes that mention of different "where" specification and found nothing.
I only found two links(can't find the urls in my history rightnow, will update shortly)
It mentioned "Paragraphs(1)" instead of "Selection.Range", but this
is an absolute location while I would need something relative
(Before the button)
Using InsertBefore method which I suppose applies only to text (it
was used to insert text in the example) as when I tried it for
building blocks it didnt work
P.S I'm relatively new to VBA
Ok, I solved the problem with the following code, posting it for others who might drop by in the future.
Private Sub CommandButton1_Click()
Dim objTemplate As Template
Dim objBB As BuildingBlock
' Set the template to store the building block
Set objTemplate = ActiveDocument.AttachedTemplate
' Access the building block through the type and category
Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustom5) _
.Categories("General").BuildingBlocks("Experience")
' Insert the building block into the document replacing any selected text.
Selection.MoveUp Unit:=wdLine, Count:=1
objBB.Insert Selection.Range
End Sub
Basically just added the following lines just before inserting the BuildingBlock
Selection.MoveUp Unit:=wdLine, Count:=1
Thanks everyone for the assistance.
When in "Creation mode", goto the "properties" toolbox of your CommandButton1, change the property TakeFocusOnClick from True to False:
The focus will stay onto the paragraph you selected.
If you want it just after your CommandButton you can add the following at the end of your sub:
...
CommandButton1.Select
Selection.HomeKey wdLine
objBB.Insert Selection.Range
End Sub