Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-02-2023, 11:39 PM
darkmaster006 darkmaster006 is offline Simulate a key press (left arrow) Windows 10 Simulate a key press (left arrow) Office 2021
Novice
Simulate a key press (left arrow)
 
Join Date: Aug 2023
Posts: 10
darkmaster006 is on a distinguished road
Default Simulate a key press (left arrow)

Hello there! I am trying to make a Macro to simulate a single key press (left arrow, in this case); just that key press and stop. Why this? Well, I have an AutoOpen macro that reads like this:


Sub AutoOpen()
'
' AutoOpen Macro
'
' This macro opens itself (AutoOpen) automatically with every Word document that's opened normally
' Choose background colour (light green 2)
ActiveDocument.Background.Fill.ForeColor.ObjectThe meColor = wdThemeColorAccent6
ActiveDocument.Background.Fill.ForeColor.TintAndSh ade = 0.6
ActiveDocument.Background.Fill.Visible = msoTrue
ActiveDocument.Background.Fill.Solid
' Turn option 'Show background colors and images in Display view' on
ActiveWindow.View.DisplayBackgrounds = True
' Select all text and change font to black colour
Selection.WholeStory
Selection.Font.Color = wdColorBlack
End Sub

Now what does it do? First, it chooses the background colour to be light green 2, and then turns the option to show background colors on, this makes it so that every already-created word document that is opened is opened with a green background (as new ones were already created green because I changed the display). Now the second thing is the important one: select all text and change the colour of the font to black, this is because the documents I'm being given are coloured in grey, which is not so optimal. Therefore, I made this macro in AutoOpen so that all text is selected and then all text is changed to black. The problem is, when this ends, it ends with all the text being selected still, which means I don't have the cursor at the start as I should. This is solved, manually, by pressing left arrow, which makes the cursor go to the start, so that's why I want this keypress. If anyone knows how to do this, any help is appreciated. Just in case, I am just a novice in VBA/Macros, I just fiddled a bit with some codes I found around internet and suited them to my needs, very very roughly. Thank you.
Reply With Quote
  #2  
Old 08-03-2023, 02:10 AM
Italophile Italophile is offline Simulate a key press (left arrow) Windows 11 Simulate a key press (left arrow) Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

It is very rare that you need to select anything when using VBA code. Here is your routine rewritten to avoid selecting the text.

Code:
Sub AutoOpen()
    '
    ' AutoOpen Macro
    '
    ' This macro opens itself (AutoOpen) automatically with every Word document that's opened normally
    ' Choose background colour (light green 2)
    With ActiveDocument.Background.Fill
        .ForeColor.ObjectThe meColor = wdThemeColorAccent6
        .ForeColor.TintAndSh ade = 0.6
        .Visible = msoTrue
        .Solid
    End With
    ' Turn option 'Show background colors and images in Display view' on
    ActiveWindow.View.DisplayBackgrounds = True
    ' Select all text and change font to black colour
    ActiveDocument.Content.Font.Color = wdColorBlack
End Sub
Reply With Quote
  #3  
Old 08-03-2023, 11:40 AM
darkmaster006 darkmaster006 is offline Simulate a key press (left arrow) Windows 10 Simulate a key press (left arrow) Office 2021
Novice
Simulate a key press (left arrow)
 
Join Date: Aug 2023
Posts: 10
darkmaster006 is on a distinguished road
Default

Quote:
Originally Posted by Italophile View Post
It is very rare that you need to select anything when using VBA code. Here is your routine rewritten to avoid selecting the text.

Code:
Sub AutoOpen()
    '
    ' AutoOpen Macro
    '
    ' This macro opens itself (AutoOpen) automatically with every Word document that's opened normally
    ' Choose background colour (light green 2)
    With ActiveDocument.Background.Fill
        .ForeColor.ObjectThe meColor = wdThemeColorAccent6
        .ForeColor.TintAndSh ade = 0.6
        .Visible = msoTrue
        .Solid
    End With
    ' Turn option 'Show background colors and images in Display view' on
    ActiveWindow.View.DisplayBackgrounds = True
    ' Select all text and change font to black colour
    ActiveDocument.Content.Font.Color = wdColorBlack
End Sub
Thank you, yes, this was better than what I could come up with, thank you very much!
Reply With Quote
Reply

Tags
key presses



Similar Threads
Thread Thread Starter Forum Replies Last Post
TOC goes into update after using the QAT back button or Alt and left arrow key to return craigt Word 7 01-30-2023 05:45 AM
left - right arrow keys don't function properly natanzel Word 1 06-27-2017 04:38 AM
Simulate a key press (left arrow) Problem with [Ctrl] + [Left Arrow] in Word 2010 (32bit) alxx86 Word 4 03-03-2016 07:22 AM
Simulate a key press (left arrow) Why does the circular and U Turn arrow only go left to right? Cerdic PowerPoint 1 10-22-2013 07:16 PM
Left arrow won't work on Word LaBecs Word 2 09-26-2011 09:27 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:16 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft