I'm trying to write a VBA macro in Microsoft Word to do the same thing as Ctrl+click does (follow a link or go to the bookmark).
I've tried SendKeys but I don't think that works for left mouse click.
I've actually came up with a partially working solution involving the use of
Selection.GoTo What:=wdGoToBookmark, Name:=BLAbut this unfortunately means I can't use ctrl+< because it seems that the history of where the cursor previously was is not saved.
So instead of coming up with my own solution, is there actually a way to just bind the action of Ctrl+click to another button? Or is there a way to write a macro that'll do the same action including keeping track of the history of the cursor?
The following code should do what you want. Install it on a standard code module.
Option Explicit
Dim ReturnRange As Range
Sub GotoBookmark()
' 13 Sep 2017
With Selection
If .Hyperlinks.Count Then
Set ReturnRange = .Range
.Hyperlinks(1).Follow
End If
End With
End Sub
Sub ReturnToLink()
' 13 Sep 2017
If Not ReturnRange Is Nothing Then ReturnRange.Select
End Sub
For testing purposes, create a bookmark in your document and a hyperlink to it. Select the hyperlink and run Sub GotoBookmark. Then run procedure ReturnToLink to go back to where you came from. Note that you can return from anywhere as well as multiple times.
You may wish to create keyboard shortcuts to call the two subs.
Related
In MS Word, when adding a comment to a text selection, I would like to change the background color of this selection if the comment contains some "keyword".
Then obviously, if the comment is deleted or doesn't contain the "keyword", I need to remove this color/highlight to the scope text.
I managed to do it on the global 'run macro' with this code (except the remove part), but ideally this should be dynamic and activates when the focus changes back from the comment to the document or anything else.
Here is a simplified version of my code attempt so far:
'
' ColorComments Macro
'
Dim wdCmt As Comment, cat As String, pg As Paragraph
For Each pg In ActiveDocument.Paragraphs
'Trying to reset the background colors before reapplying the active ones but doesn't work
pg.Shading.BackgroundPatternColorIndex = 0
Next
For Each wdCmt In ActiveDocument.Comments
With wdCmt
If Trim(.Range) = "keyword" Then
'Probably a better way to change the background color of the text that would give me access to more colors?
.Scope.Shading.BackgroundPatternColorIndex = 2
Else
'resetting the comment "Scope" color if it doesn't contain the code
.Scope.Shading.BackgroundPatternColorIndex = 0
End If
End With
Next
End Sub
I guess I should use the pulic events or the WindowsSelectionChange event but I can't find a documentation about it anywhere.
I've little to no experience in VBA so thanks in advance for awy help :)
To make that work effectively you will need to repurpose these two controls from the RibbonUI:
ReviewNewComment
ReviewDeleteComment
Here are a couple of links to articles on repurposing RibbonUI controls
https://gregmaxey.com/word_tip_pages/repurpose_user_interface_controls.html
https://www.experts-exchange.com/articles/21499/Intercepting-Office-Ribbon-Control-Events-with-VBA-using-Repurposing-Commands.html
In prior versions of Word, I don’t recall seeing what version you are trying to use, VBA solutions could access the following event routine:
Sub InsertNewComment()
'
' InsertNewComment Macro
' Insert comment (includes menu)
'
End Sub
And even though this routine still works if you invoke it directly from VBA, this is not what is used from the RibbonUI in today’s versions of Word. I know that because I’ve tried to trap the event using the subroutine but haven’t been successful.
The following VBA routine is invoked by clicking on the Insert Ink Comment on the RibbonUI.
Sub InsertInkComment()
'
' InsertInkComment Macro
' Insert ink comment
'
Selection.Comments.Add Range:=Selection.Range
End Sub
It’s a mystery why this one works and the other does not and that is why I said upfront that you will have to trap and repurpose the RibbonUI, particularly for regular comments.
As for trapping the Delete Comment event, there isn’t a DeleteComment VBA routine that I have found. There are event routines for Deleting All Comments but none for a single comment.
I just finished some VBA and I was wondering if there is a way to hide certain macros on Excel.
I need the user to run a certain macro and only that one, but it shows all the sub macros in Excel. I want to hide the unnecessary macros from the user so that way the user doesn't accidentally click on the wrong one.
You can also do this by placing the macros you want to hide in a separate module and using Option Private Module at the top of the module before the code. The macros will still be available to your project but will not appear in the Macros seen by the user when he clicks the Macros button.
You can either create a button in the ribbon to run the macro, or you can add "Private" before each "Sub" in the VBA editor that you don't want the user to easily access.
To subjectively 'hide' certain sub procedures (i.e. 'macros') from the (Alt+F8) Developer, Macros dialog use an optional non-variant parameter that means nothing.
Sub meh(Optional w As Worksheet)
Debug.Print "hello world"
End Sub
The meh macro will not show up in the list of macros to run. If you dim the parameter as variant it will show in the list. This is likely due to a optional variant parameter being able to use the IsMissing function. It will also not be able to be run from the VBE with F5 or stepped through with F8.
The test sub procedure will run the code correctly.
Sub test()
meh
End Sub
Sub meh(Optional w As Worksheet)
Debug.Print "hello world"
End Sub
I am in Outlook 2010 in Windows 7 writing in VBA and want to pass the name of a macro (or sub routine) as a string variable to another sub routine and have that routine run the macro. In Word you can do this with
Application.Run MacroName:=strMacroName
Where strMacroName is a string variable with the name of the macro. That approach does not work in Outlook 2010. How can I accomplish the same thing?
I tried
Call Application.strMacroName
Call strMacroName
strMacroName on its own line
Outlook.Application.strMacroName
None of those things worked.
I just upgraded to Outlook 2010 and so can no longer use keyboard shortcuts to run custom code for handling email. So to restore some version of that functionality I have created code to present a dialog box with my most common macros. The code is fairly clean to modify as time goes along and pass along the name of the macro I want to run but I used to be able to run that routine in one command (Application.Run MacroName:=strMacroName).
Now I have to include a long switch statement to accomplish the same thing. Not nearly as simple.
Thanks!
CallByName seems the only way to go.
With this code in ThisOutlookSession:
Public Sub TestFoo()
Dim testClass As New TestClass1
CallByName testClass, "TestMethod1", VbMethod
End Sub
And this code in TestClass1:
Public Sub TestMethod1()
MsgBox "huzzah!"
End Sub
Calling ThisOutlookSession.TestFoo gives you the expected message box.
As far as I can tell, the only way to run a named macro programmatically in Outlook (without using custom Classes, as the other answer here does) is to create a temporary CommandBarButton, execute it, and immediately delete it. This works in Outlook 2013 even with the Ribbon:
Public Sub runProc(procToRun As String)
With Application.ActiveExplorer.CommandBars.Add("Custom", temporary:=True)
With .Controls.Add(msoControlButton, 1, , , True)
.OnAction = procToRun
.Execute
.Delete
End With
.Delete
End With
End Sub
I know this is an old question, but I was unable to find this exact answer myself in late 2017, so hopefully it will help someone else. Please note that this will NOT run macros that are in ThisOutlookSession...your macro needs to be in a code module.
As described in http://support.microsoft.com/kb/278591 PowerPoint will usually combine all changes that a macro or add-in makes to a single undo step. So if you put the following code into VBA and execute it twice by pressing F5 there
will only be one undo step.
Sub Move()
If ActiveWindow.Selection.Type = ppSelectionShapes Then
ActiveWindow.Selection.ShapeRange.IncrementLeft 10
End If
End Sub
I am looking for a way to change this behavior in a more complex scenario where the user can make multiple changes without directly accessing PowerPoint. Ideally, it should be possible to undo a set of modifications that corresponds to one change from the user's perspective.
What I found out is that ExecuteMso seems to break the undo transaction. So if you execute the following code twice it results in 4 undo steps (first increment, ExecuteMso, second increment, ExecuteMso).
Sub Move()
If ActiveWindow.Selection.Type = ppSelectionShapes Then
ActiveWindow.Selection.ShapeRange.IncrementLeft 10
Application.CommandBars.ExecuteMso "Bold"
End If
End Sub
Anyone knows a real solution for the problem or a better workaround? Although I didn't find it maybe there is a solution for Word or Excel that can be ported to PowerPoint?
If you replace the ExecuteMso line with this one, the entire procedure remains one Undo:
ActiveWindow.Selection.ShapeRange.TextEffect.FontBold = msoTrue
Update
If you are using PowerPoint 2010 or later put this line before each block of code that you want the user to be able to undo a step at a time:
Application.StartNewUndoEntry
I have only basic VBA experince and my prior Macro experence was primarily with WORD 2003. Recording Macros used to take GoToFooter (or Edit Footer) Menu Commands and allow subsequent editing. In WORD 2010, this (and many other) commands do not "record" to the Macro (yet when in Record mode, I do get into Edit Footer function).
A research of various VBS options shows several ways to create Footers and to make global Footer setting changes within Macro. However If I simply want to Revise the Company name within the Footer (for example), I can find no way to do this within a Macro subroutine.
This subroutine is one that I would call from the Main Macro that is stepping through each file in a Folder (& subfolders). I have the main Macro functioning.
Does WORD 2010 Macro-VBA preclude simple Edit-Footer function?
Thanks in advance
So, thanks to Issun, here is my solution:
`
Sub Sub_FTR_0()
'
ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
For i = 1 To ActiveDocument.Sections.Count
'REM: INSERT Code from RECORD MACRO recorded when editing one Footer correctly
Selection. [[xxx]], etc.
If i = ActiveDocument.Sections.Count Then GoTo Line1
ActiveDocument.ActiveWindow.ActivePane.View.NextHeaderFooter
Line1:
Next
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
`
Here is a way you can access the headers/footers via VBA. As you can see, it's rather complicated syntax to get to something so simple :p there
Sub EditHeadersAndFooters()
Dim i As Long
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.Headers(wdHeaderFooterPrimary).Range.Text = "Foo"
.Footers(wdHeaderFooterPrimary).Range.Text = "Bar"
End With
Next
End Sub
Here is a link to example code on how to change the headers in every file in a folder. It takes a different approach and I have never tried it, but for your reference: http://www.vbaexpress.com/kb/getarticle.php?kb_id=45
This worked for me for all pages in the document.
word.ActiveDocument.Sections(1).Headers(1).Range.Text = "Put the header here"