I'd like to trigger an event as soon as the text in the (active) document changes. As I understand it, WindowSelectionChange is most suitable for this. Is this indeed the case? If so, how exactly can I use it? I've read the documentation here but I can't get my head around it, especially about which code goes in which file. Right now, I've all my code in the default module file in the "Modules" folder in the project.
FYI: I'm not really typing in the document, instead I only do Ctr+A, to select the existing content, and Ctrl+V, to replace it by new content. At that moment, I want the event to be triggered.
Although I wasn't able to find a solution for my specific question, I did find something that gave me results I wanted:
Sub EditPaste()
Selection.Paste
'Other code
End Sub
It's not quite the same as a 'text changed' event, but for me it is, because I only change the text by pasting new text. Because I named the Sub the same way as the actual paste event, this Sub is called whenever I paste something. Then, it's only a matter of adding the actual paste operation and then add your own code.
In short: instead of creating an event for an operation it's much easier to redefine the operation.
Related
Is there a way to apply "auto-updatable" style for hyperlink?
I believe, this question is not trivial.
When you normally click on hyperlink, it will change it's color to violet. Next, if you save, close, and then reopen the document, the link will be updated back to blue. This is default behaviour of Word, and there is no need to use any macros for it.
I'm trying to replicate this behaviour with VBA. Here is the code:
Sub Test1()
Selection.Range.Hyperlinks(1).Range.Fields(1).Result.Style = Word.WdBuiltinStyle.wdStyleHyperlinkFollowed
End Sub
To make it work, simply put caret into the link, run macro, and see the results:
This works fine, except such visited links will not be auto-updated after you save, close, and then reopen the document. See the difference in the picture below. The link "Google" was opened normally, using the mouse Ctrl-click; the link "StackOverflow" was opened using the macro:
As I already said, I want to make my VBA-opened links (StackOverflow) auto-updatable as well (as Google).
Yes, I understand, there is a workaround - simply create another macro, which will be started every time the document opened and change all violet hyperlinks back to blue. However, this is just workaround, and I don't like it. Using it, we use conversion from "permanent violet" to "permanent blue", instead of using "temporary violet" (that's mean, auto-updatable without any additional efforts).
Hope everything is clear. Thanks in advance.
Update (was added after several answers were already posted).
Yes, I understand, this will work:
Sub Test1()
On Error Resume Next 'To avoid an error in case if the link isn't reachable
Selection.Hyperlinks(1).Follow
End Sub
But I want just simulate following, without really opening the link in the browser. That's why, I can't use Selection.Hyperlinks(1).Follow.
you need to remove the line, the link will change once followed and change back once the doc is reopened.
Selection.Range.Hyperlinks(1).Range.Fields(1).Result.Style = Word.WdBuiltinStyle.wdStyleHyperlinkFollowed
this does it for me
Sub resetHyperlinks()
Dim hLink As Hyperlink
For Each hLink In ActiveDocument.Hyperlinks
hLink.Address = hLink.Address ' this works
' hLink.ScreenTip = hLink.ScreenTip ' this works also
Next hLink
End Sub
You don't need to change the style with code to make the link purple. Just use the Follow method. This will click the link and turn it purple and then it will be reset to blue upon opening the document again.
Sub Test1()
Selection.Range.Hyperlinks(1).Follow
End Sub
You can reset link styles with VBA code that runs at startup, i.e. is a part of Document_Open() routine in ThisDocument VBA module.
The Hyperlink class doesn't have any .Visited property or anything relevant (i.e. you cannot even see if it was visited), so there's no other way beside .Follow() that also opens the link as it should.
You're basically trying to falsify the information that the document provides about its state: make a link appear visited when it actually wasn't.
The fact that the class doesn't even provide a property means that Word's designers do not consider the visited status a part of the editor's functionality (i.e. it effectively doesn't exist as far as the program's job is concerned).
This evidence suggests that Word doesn't, and is not designed to, have any specialized facility to switch link status other than .Follow(). Which means, any way that you find that happens to have the desired effect in bound to be what you're calling a "workaround".
The "temporary" color of a followed hyperlink is an embedded (and not directly accessible) feature of the built-in Hyperlink character style. It is not exposed through the normal UI's Style tools, nor through the object model.
You can readily compare all formatting between two selections using the Reveal Formatting pane (Shift+F1) in the document window in Word.
If you compare a normally followed hyperlink with a hyperlink affected by your snippet, you'll see that the followed hyperlink still has the Hyperlink style, while your simulated follow has changed the style of the second hyperlink.
If you compare a never-followed hyperlink and a normally followed hyperink, Word identifies their formatting as exactly the same. Word does not acknowledge that any aspect of formatting (style, font color, etc.) has changed.
It seems likely that the Word.WdBuiltinStyle.wdStyleHyperlinkFollowed you are using exists explicitly to address this gap (which is somewhat disappointing).
I recommend using your existing approach, and then reverting the style in a procedure triggered by the Before Save and Before Close events of the document. Using those events will prevent the followed style from saving at all, and so avoid issues caused by someone opening the document without enabling macros.
Option Explicit
Sub test()
Dim HL As Hyperlink
For Each HL In Sheet1.Hyperlinks
HL.Range.Style.Font.Color = vbBlue
Next
End Sub
Can't you simply make it any colour you want without invoking it. As others have stated above whatever you you I think will be a work around as it's not an intended function.
I've made a form-letter document with a macro that performs the mail merge. I don't want the user to have run it from the menus, and I want this to be portable. If there's a way for a button to appear on each user's ribbon or quick command menu, I'm not familiar with it.
So I put a button in the document itself. Unfortunately, every form-letter created has the same button in it. I suppose I could write the code to delete every one, but I think that would be slow.
Is there a way to assign a shortcut key to an existing macro, and have it reside in the document?
I had to implement something pretty similar to what you were referring to some 10 staff. My solution (by no means as portable as desired) was to export the macros and forms from my Normal template to the other users, I coupled this with Ribbon customization and it worked well. Unfortunately, when a change was needed, I had to trudge over to everyone's machine individually.
I would suggest you stick with your solution of deleting button after the merge is complete. Here's some code to help with that:
Sub DeleteCommandButton()
For Each o In ActiveDocument.InlineShapes
If o.OLEFormat.Object.Name = "CommandButton1" Then
o.Delete
End If
Next
End Sub
Good luck, hopefully this helps.
Would it be possible to have a control button (or any mechanism) that once clicked on, a textbox would appear with fields like macro name Sub "NewMacroTitle"(), a field for the date, a field for reference sources ("Getting Links/URL from a webpage-Excel VBA"found this here"), a brief description of what the macro is to do. All of these fields, except Sub "NewMacroTitle"(), would start with a ' to show a comment and all of this would be inserted into the VBA editor, or enter it into cells on a spreadsheet that could then be copied and pasted into the VBA editor.
My problem is this. I'm new to VBA. I'm also the world's worst at documenting macros. I get an idea for a macro, I'm off into the VBA editor writing away, maybe even get the macro complete. A week later I'm reviewing the macro and the first thing is "what the heck does this thing do?"
I hope you see what I'm trying to do, essentially some way to force me to document the macro before even starting the actual code.
There are tools that will add pre-defined text to the header and/or footer of a sub. I use the one from MZ Tools for error handling code and adding my name and date to the header of each proc. It is worth a look.
I'm sure this is a simple question, with a simple answer, but I can't find it.
I've inherited a spreadsheet that I have to fix. Whoever wrote it made extensive use of VBA and VBA UserForms for inputting data. On one form, textual and numeric information is entered and then saved to a record on a specific worksheet. This spreadsheet is used to log project information. It gets copied, and re-copied again and again. With each re-copy, it is cleared and used for the next project.
A user has sent me a spreadsheet of one of these major projects. The VBA, data-entry UserForm has a problem. One TextBox, which accepts either text or numbers, is always reformatting the numbers as dates when you exit the field!? I've tripled-checked the VBA code. There is no special OnEnter or OnExit code related to this field that reformats the data. Furthermore, I can't find a Format property that is associated with the TextBox from within the designer.
I'm a C/C# developer, not a VBA developer. Still, this "simple" IDE has me stumped. I can't find the property that re-formats the TextBox display value.
How can I fix the TextBox so that it persistently interprets numbers as numbers and not as dates?
FURTHER NOTE
It is worth mentioning two things. First, user's can't modify these forms or VBA code. The underlying modules are password protected and only myself and a couple managers know the password. Furthermore, no one touches the code because I'm the only developer within the company and everyone is a bit scare that they might break something.
Second, something in the file may have been corrupt. When this file was sent to me, the user also mentioned that the worksheet that he was working on was renamed. It appears that something didn't save properly because the WorkSheet tab was renamed a random hex string value.
Everything appears to be functioning as normal on this form, other than these two issues. Any help or guidance would be greatly appreciated.
You can force the proper format by using cLng:
First enter =TODAY() in cell A1 and then run:
Sub TextBoxIssue()
With ActiveSheet.Shapes("TextBox 1").TextFrame.Characters
.Text = Range("A1").Value
End With
MsgBox "However when we use cLng......"
With ActiveSheet.Shapes("TextBox 1").TextFrame.Characters
.Text = CLng(Range("A1").Value)
End With
End Sub
There is most certainly code that is formatting that text on exiting the textbox. You just haven't found it yet. If you go into the Userforms class module and select the textbox from the left dropdown, you can see all of the events in the right dropdown. The bolded events are in use. I assume you've already checked all those, but that's the place to start.
Next, look for custom class modules that use the WithEvents keyword. They can trigger events outside of the userform's class module.
Finally, search the code for all instances of =Format(tbxName.Text,"mm/dd/yyy") or some such code. Somewhere the code is probably using the Format function to fill that textbox.
Is it possible to create a macro in MS Office (in this case Word) that will change other macro code? I was trying to find information but no results.
I have a doc which works as a template. Content of template is changed and then saved to another file. However it is important to have current date in it. It cannot be self-updated. Those docs go to folder of people and it is important to know when they get the document, so it must be simply data (or something that does not update).
I was thinking about an on-start event macro that would input current date and on exit it would ask "Do you want self-update functionality" Yes / No
If Yes, delete that event. However I have no idea if it is possible. If it is I still don't know how to search for it.
No this is not possible. In VBA, unlike some lower level languages when you define an event you can not disable it, even using other VBA code.
In C# or VB.NET, Java or C++ you can disable an event by un-wiring it from the handler, but this is not possible in VBA.
Maybe if you be more clear on what you need I can give you a better answer.