PDA

View Full Version : Solved: emulate overtype



andalusia
02-07-2007, 09:06 PM
Any way to emulate Overtype in Word VBA?

lucas
02-07-2007, 09:21 PM
On a page or in a userform?
I think this will work on a userform....yoinked it from Tommy.
Private Sub UserForm_Initialize()
Me.TextBox1.Text = Me.Caption
Me.TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
End Sub

andalusia
02-07-2007, 10:14 PM
This sounds interesting, but how do I then enter the data to be overtyped into the textbox?

mdmackillop
02-08-2007, 06:49 AM
Here's a simple modification of a recorded macro.

Sub Macro1()
Dim txt as String
txt = InputBox("Enter new text")
Selection.MoveRight Unit:=wdCharacter, Count:=Len(txt), Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText txt
End Sub

lucas
02-08-2007, 08:46 AM
This sounds interesting, but how do I then enter the data to be overtyped into the textbox?


Private Sub UserForm_Initialize()
Me.TextBox1.Text = Me.Caption
Me.TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
End Sub


It's this line from the code above:
Me.TextBox1.Text = Me.Caption
You could for instance use:
Me.TextBox1.Text = "my text"

TonyJollans
02-09-2007, 03:02 AM
What do you mean by emulate overtyping? Overtyping is a way for the user to replace a character at a time as they press keys - it works in documents and userforms. One wouldn't normally respond to keys in the same way in VBA so what is it that you are trying to do?

fumei
02-09-2007, 10:24 AM
I would like to know this as well.

andalusia
02-09-2007, 11:38 PM
What do you mean by emulate overtyping? Overtyping is a way for the user to replace a character at a time as they press keys - it works in documents and userforms. One wouldn't normally respond to keys in the same way in VBA so what is it that you are trying to do?

The problem is that there is a certain amount of space for the macro to insert text into. It needs to preserve the number of lines used in the entry so as not to influence later entry points. When encountering a blank line, I believe overtype will insert text out to the margin and then go to the next line. Overtype into a paragraph should always preserve the number of lines in the paragraph. If there are spacing lines after the paragraph, overtype should preserve the total number of lines, possibly inserting text into the spacing lines.

Care will be taken to ensure that no entry is too long; nothing should spill out beyond the final spacing line.

The problem requires that the text be entered from a macro. The solution I wish to attain is to conduct text entry from VBA according to the specifications of overtype entry.

TonyJollans
02-10-2007, 02:46 AM
To be perfectly honest none of that makes a whole lot of sense to me - I don't really understand exactly what you are trying to achieve and I think maybe you are misunderstanding what overtype will do.

Overtype causes the character ahead of the cursor to be replaced with the character entered - as far as overtype is concerned a paragraph mark is a character like any other. Unless you are using a fixed width font (in fact even if you are), changing the text may cause lines and paragraphs to reflow and there is certainly no reason to think blank lines will be preserved.

However, if you want to add text according to overtype 'rules' this will give you a starting point:

NewText = "whatever your new text to be entered happens to be"
' Assuming the cursor is positioned at the input point ...
ActiveDocument.Range(Selection.Start, Selection.Start + Len(NewText)) = NewText

This will fail at the end of a document - and at numerous other places. As you are non-specific about where in a document you might want to do this, it may have to be amended. However, as already said, I don't think it will do what you want at all unless there are many constraints applied.

andalusia
02-10-2007, 03:01 AM
I don't really understand exactly what you are trying to achieve and I think maybe you are misunderstanding what overtype will do.

No, I have typed with overtype and it is exactly what I want.




Overtype causes the character ahead of the cursor to be replaced with the character entered - as far as overtype is concerned a paragraph mark is a character like any other.

There is more to overtype than that. Try turning on overtype in a document and typing on a blank line. If it were as you say, the first key pressed would replace the eol and go to a new line. But it doesn't. It types out just like insert would, filling the line. This is why the simple fixes of taking out as many characters as you put in do not work.

TonyJollans
02-10-2007, 06:28 AM
My apologies - you are correct. I never use it - I guess it shows.

So you have some text in a macro and you want to put it in the document at the cursor position replacing the minimum of (the length of the text) and (the length of the rest of the paragraph).

> Selection.Paragraphs(1).Range.End - Selection.Start - 1

gives the length of the rest of the paragraph. Is that enough to let you decide what needs replacing?

As a matter of interest what is the source of your text (and indeed the document - is it a form?) that you need to treat like this?

fumei
02-10-2007, 06:13 PM
Frankly, that is what you get when you HAVE blank lines. When you HAVE...ahem...."spacing lines".

Overtype into a paragraph should always preserve the number of lines in the paragraph.Have you actually tried this? It certainly does NOT do that for me - Word 2002. If a paragraph has three lines, and you go to OverType it will overtype to the paragraph mark...and keep on going. You can use OverType to increase a three line paragraph to a 15 line paragraph. It does NOT preserve the number of lines. Why would it? Text entered in OverType mode in a paragraph will be overtyped up to the paragraph mark, then inserted while maintaining inside that paragraph. It does NOT go into the the next paragraph, "blank" or otherwise.

Sounds like something needs to be redesigned. using proper styles may be a start. Perhaps if you also give more detail on the requirement for "later entry points". Well designed code should not have a problem with this. What seems to be the problem with that?

andalusia
02-13-2007, 05:01 PM
> Selection.Paragraphs(1).Range.End - Selection.Start - 1

gives the length of the rest of the paragraph. Is that enough to let you decide what needs replacing?

As a matter of interest what is the source of your text (and indeed the document - is it a form?) that you need to treat like this?

Yes, I believe this will do it. The source of text is a database with medical diagnoses/risks of operation/nature information. These are going into a medical form which is represented by a jpg image. The entries have to be placed onto the form on the appropriate lines for them.



Have you actually tried this? It certainly does NOT do that for me - Word 2002. If a paragraph has three lines, and you go to OverType it will overtype to the paragraph mark...and keep on going. You can use OverType to increase a three line paragraph to a 15 line paragraph. It does NOT preserve the number of lines. Why would it? Text entered in OverType mode in a paragraph will be overtyped up to the paragraph mark, then inserted while maintaining inside that paragraph. It does NOT go into the the next paragraph, "blank" or otherwise.

You are correct. I think this is non-intuitive.



Sounds like something needs to be redesigned. using proper styles may be a start. Perhaps if you also give more detail on the requirement for "later entry points". Well designed code should not have a problem with this. What seems to be the problem with that?

Since I want the entries to the forms to be in their appropriate places, I have placed text markers at these locations. The macro searches for the marker, replaces it with the medical text, and then goes on to the next one. If the new text takes up multiple lines, it can throw the placement of markers off.

geekgirlau
02-13-2007, 07:52 PM
Just a thought - would it be worthwhile setting up your document in a table where the rows are set to a specific height? This would allow any additional text to be entered, without actually impacting the number of lines that will be printed.

fumei
02-14-2007, 09:29 AM
text markers? Do you mean bookmarks?

fumei
02-14-2007, 09:30 AM
Darn....button....

If you are using nookmarks, then the bookmark will be where ever it is. It sounds like you are talking about "placement" as a fixed location, which is odd.

andalusia
02-16-2007, 03:55 PM
Just a thought - would it be worthwhile setting up your document in a table where the rows are set to a specific height? This would allow any additional text to be entered, without actually impacting the number of lines that will be printed.

Yes, I think this would be a good way to solve the problem.

fumei
02-17-2007, 08:52 AM
The macro searches for the marker, replaces it with the medical text, and then goes on to the next one. If the new text takes up multiple lines, it can throw the placement of markers off.Could you indulge me and answer my question? These markers, are they bookmarks?

andalusia
02-17-2007, 04:37 PM
Could you indulge me and answer my question? These markers, are they bookmarks?

No. They mark where a response should go.

For example the form might have some locations for descriptions of an illness:


Diagnosis ABC1
Risks ABC2
Treatment ABC3


Note that Diagnosis, Risks, and Treatment are part of a jpg image of the form. ABC1, ABC2, and ABC3 are text markers in a document which will be imported at the appropriate locations. Currently the macro searches for these markers and replaces them with appropriate text.

Not every marker corresponds to a one-line entry. So, there are some entries which will be two or three lines depending on the given illness.

lucas
02-17-2007, 04:57 PM
But how do they mark it? How are the appropriate locations "marked"? Is it a find and replace routine or are they bookmarks is what Gerry is asking I think.

fumei
02-17-2007, 10:37 PM
Yes, I will ask for a THIRD time.
text markers in a document which will be imported at the appropriate locations. Currently the macro searches for these markers and replaces them with appropriate text. Searches for WHAT????????????

Bookmarks? Strings of text?????

Markers, in my mind, ARE book....MARKs. Markers.

So if, as you say, they are not bookmarks, what the heck (for the third time) are they?

I don't really care, it is your document, but I am curious because if they are bookmarks, then this concern for movement can possibly be worked with.

But...whatever.

andalusia
02-25-2007, 04:40 PM
Yes, I will ask for a THIRD time.Searches for WHAT????????????

Bookmarks? Strings of text?????

Markers, in my mind, ARE book....MARKs. Markers.

So if, as you say, they are not bookmarks, what the heck (for the third time) are they?

I don't really care, it is your document, but I am curious because if they are bookmarks, then this concern for movement can possibly be worked with.

But...whatever.

They are not bookmarks. They are strings of text placed beforehand in the proper places for the responses to queries on the medical document. The macro searches for them and goes there to put the appropriate response in.

fumei
02-26-2007, 07:24 AM
Ah, they are strings of text.

The macro searches for the marker, replaces it with the medical text, and then goes on to the next one. If the new text takes up multiple lines, it can throw the placement of markers off.

I will reiterate my original point.

If the locations were NOT just strings of text, but bookmarks containing that text, you would NOT have any issue with locations. Changing the text would never make any problem with location, or the placement of those markers. Even if the insertion of text into the bookmarks moved the location 20 pages, it would may no difference. You would always be able to replace text into the bookmark. The bookmark knows where itself is, no matter where it ends up.

So instead of (I am making up examples):

Searching for "broken leg", and replacing it with "broken arm"...

you could have a bookmark named "BrokenLimb" and simply replace the current text in the bookmark, with any new text you want - "broken leg" replaced by "broken arm". Whatever.

The location where the bookmark ended up becomes irrelevant, as you are replacing the bookmark text, no matter where it is.

PLUS, it would be much easier to replace the text.

Further, I still fail to see how things are messed by location. if you are using Find to find the string, it should make no difference either.

A:
yadda yadda yadda SEARCH STRING HERE yadda yadda yadda.

B:
yadda yadda yadda yadda yadda yadda. yadda yadda yadda SEARCH STRING HERE yadda yadda yadda.

If you are searching for SEARCH STRING HERE, it makes no difference if it is on the first line, or the second line, or the fourth line. Search (or Find) will just look until it finds it.

andalusia
02-27-2007, 07:55 PM
Further, I still fail to see how things are messed by location. if you are using Find to find the string, it should make no difference either.


Say you have some search strings:



T

?

e

----

When you replace the first one with a three-line piece of text, it will push the others forward two lines each. They are then no longer aligned with the form.

That doesn't matter now, though. I am using a table as suggested earlier. The problem I am having now is getting the table to be invisible so it does not show up and distract from the rest of the page.

fumei
02-27-2007, 11:22 PM
Now it is an alignment issue? NOT a location issue. Plus you are talking about making the table invisible. I will leave this one as I am quite confused. Alignment issue (not location) with something you do not want to be seen - in which case why is there an alignment issue?

Whatever.