Hi I am getting an error for this. I have tried both of the following examples. Please let me know if you know what's wrong. Thanks!
Sub Spanish()
'
' Spanish Macro
'
'
Templates.LoadBuildingBlocks
Selection.Range.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
ActiveWindow.Close
Application.Quit
End Sub
Sub Algebra()
'
' Algebra Macro
'
'
Application.Templates.LoadBuildingBlocks
Selection.Range.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
ActiveWindow.Close
Application.Quit
End Sub
Your code isn't going to do what you think it will.
Both routines do exactly the same thing, action any selected hyperlink and shut Word down.
The first line, Application.Templates.LoadBuildingBlocks, has no relevance to following a hyperlink, and should be removed.
I suspect that you intended the second line, Selection.Range.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True, to open something specific. It won't. It will simply execute whatever hyperlink you currently have selected, or create an error if your selection doesn't contain a hyperlink. Essentially you have just created a long winded way of pressing Ctrl whilst clicking on a hyperlink.
The final two lines will shut Word down completely. Is that really what you want to happen?
To execute a specific hyperlink in Word you should use the FollowHyperlink method, for example:
ActiveDocument.FollowHyperlink Address:="https://stackoverflow.com/", NewWindow:=False, AddHistory:=True
"I don’t know code sorry. I just pressed record and it worked."
You don't need to know code to realize that what you recorded isn't going to work as you intended. The two routines were presumably meant to open different links, but just a quick read of the recorded code should have told you they weren't going to do that. If they did they would include the link you wanted to follow.
My advice to you would be to only use what you understand. If you want to use code with Word then invest some time in learning how to do it. There are plenty of online resources to learn VBA, including online courses. Although most are aimed at Excel rather then Word they will still be valuable.
But don't just use the macro recorder and hope it will work.
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'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.
i need some help with my code. I wrote a macro which copies many tables as pictures from different Excel files to PowerPoint, but sometimes it works perfectly, and sometimes appears Error 1004 - CopyPicture of method class failed. Here is my code:
Sheets("List1").Select
' group chart
ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
' select cells to be copied to PowerPoint:
Range("A1:X83").Select
' copy cells:
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
' paste to PowerPoint:
pptSld.Shapes.Paste.Select
I found that for someone was helpful inserting Sheets("List1"). before Range("A1:X83").Select, but it didn't help.
I have this problem at work, where I'm using Office 2010. At home I have Office 2016 and code works perfectly.
Do you have any ideas, how to make it work without any errors?
Lose the habit of using .Select for everything; that's what macro-recorder code does, but only because macro-recorder code mimicks every manual action - when automating Office, you don't actually need to reproduce every mouse click you'd do when doing the task manually - instead you work with the Office application's object model.
</rant>
So you work off Worksheets("List1"), and copy a specitic Range as a picture:
Application.Worksheets("List1").Range("A1:X83").CopyPicture xlScreen, xlPicture
This code is much more robust than anything that relies on Selection: you're calling .CopyPicture off a specific Range object.
So all that's left to do is to paste:
pptSld.Shapes.Paste
Note that Paste is a method that doesn't return anything, so you can't legally do .Select on it - but why would you want to .Select it anyway?!
If you need to access the newly-created Shape object, you can retrieve it from the pptSld.Shapes collection:
Dim excelScreenshot As Shape
Set excelScreenshot = pptSld.Shapes(pptSld.Shapes.Count)
And then whatever you wanted to so with Selection, you can now do with excelScreenshot - and as a bonus you get IntelliSense and auto-completion for working with a strongly-typed object reference, as opposed to Object, which makes every member call a late-bound call - in other words you'll get better performance, however unnoticeable it might be.
Check your references to make sure that you're calling the correct library.
Tools > References
Not sure which on you need, but make sure you have Microsoft Powerpoint xx.x blah blah blah checked.
I am trying to create a macro for Word 2016 that will automatically edit a document based on a list of about 250 search terms that I used the "find and replace" function to change. When I record the macro from start to finish and attempt to run, I receive a message saying "Procedure is too long". I have looked through answers on here and understand that you can clean up some code and/or create sub sections within the code to troubleshoot. However, I am not sure what I am doing and the code is very long.
Could someone help me please?
portion of code is included below:
Portion of code
Break your macro into steps. The easiest way is (use a copy of your document for testing, so you can always put the original text back if you make a mistake!):
Record a short macro. It doesn't matter what it does. Name it something like DoAllReplacements.
Record a macro that does your first block of replacements. Name it something like DoReplacement1.
View the macros, and edit the DoReplacement1 macro. Copy all of the text from the end of the comment block to the line just before End Sub.
Put the edit caret (cursor) at the end of the DoReplacement1 End Sub and hit enter twice. Enter the following text:
Sub DoReplacement2()
End Sub
Paste the code from the clipboard between the Sub and End Sub lines you just added. Edit the macro code to do your next block of replacements.
Repeat the above until you have all of your replacements coded, using as many new DoReplacementX routines as needed.
Go back to the very first macro you recorded (DoAllReplacements), and remove all of the code between the Sub DoAllReplacements() and End Sub. Add new calls to the individual DoReplacementX macros you wrote.
Sub DoAllReplacements()
DoReplacement1
DoReplacement2
DoReplacement3
' Etc. until you've added them all
End Sub
Exit the macros code window. Use View Macros, select your DoAllReplacements macro and execute it.
I have two macros that I want to work when I push a button. I inserted the code needed in the button sub and made it so that it is not private. Then I opened the macro dialog box and ran the macro and it worked as expected. But, when I try to press the button, it comes up with an error that says "the requested member of the collection does not exist."
The code shouldn't really matter in this case since it is working correctly when I run it manually but here it is:
Sub CommandButton4_Click()
Selection.Tables(1).Select
Selection.Copy
Selection.PasteAndFormat (wdPasteDefault)
ActiveWindow.ActivePane.VerticalPercentScrolled = 24
'Some other Code here
End Sub
When I perform a debug, it says that the problem is in this line: Selection.Tables(1).Select
Then in my other instance I have this code:
Sub CommandButton3_Click()
Selection.SelectRow
Selection.Copy
Selection.InsertRowsBelow 1
Selection.Paste
End Sub
The code error appears to be in this line:
Selection.SelectRow
It says
The SelectRow method or property is not available because some or all of the object does not refer to a table.
I got the code by using the Macro Recorder.
If you have any ideas as to why this is happening, I would appreciate your help.
This code
Selection.Tables(1)
works only when the Selection (the cursor) is inside a table.
When you click the button, the button gets the focus, and the Selection is moved there. If the button is not inside a table, the code will fail.
It does work if you move the button inside the table, but that would probably look silly.
Replace your
Selection.SelectRow
with
'r being which ever row you want to Select
Selection.Table(1).Row(r).Select
You can bind your macro to a key combination. This won't change any focus you've set, and thus should solve your problem.
How to do this differs among different versions of Word, but the recent versions are explained here:
http://wordribbon.tips.net/T008058_Assigning_a_Macro_to_a_Shortcut_Key.html