Word VBA search for adjacent (non-space) characters with different formatting - vba

I need to be able to find every place in my document (hundreds of pages) where there is a formatting change without a space. For example:
a bold partnext to regular text
Or red text next to black with no space. I want to have my macro find each "word" (in the vba sense) like this, and execute code based on that character location accordingly. (The loop should identify the character position where the format change occurs... although I can do that part with a loop through the characters within the found word).
Is there a simpler way to do this than by looping character by character through the whole document and checking for a difference in formatting, which would be too resource-intensive?
Thanks for your help.

Related

Word VBA command to determine if found character is at beginning of end of line

I am trying to create a macro that will search for a single character and perform a specific action based on whether or not the character is at the end (do nothing) or beginning (do something) of a line.
In my quest to find how to do this, I've seen plenty of documentation for determining if the text is at the beginning or end of a document or page or paragraph or on a specific line, or its position in a cell, but nothing about the columnar (I assume)position relative to a line of text.
Does anyone know if such an animal exists?
Thanks in advance!
Selection.Information(wdFirstCharacterColumnNumber) will give you the position of first character in the selected text relative to the line. Is this what you are searching for?

VBA to copy text from excel to on specific location in wordfile

Problem: Pasting copied data from excel to specific location in a word file.
Currently I have code which can paste the value, but it does so to "paragraph1"
myDoc.Paragraphs(1).Range.Paste
How do I specify the exact location (by line) in which to paste the data?
Let me know if more info is required.
Thanks!
Mohd Akhtar
Word gives a number to each character in the document's body, from 1 up. It then defines a range with Range.Start to Range.End So, Paragraphs(1).Range might be equal to Range(Start:=1, End:=120).
The text contained in that range is Range.Text, Read/Write. Therefore, Paragraphs(1).Range.Text = "My new paragraph text" will replace the existing text in the document's first paragraph. ActiveDocument.Range(0, 0).Text specifies the range before the first character in the document.
In order to insert text at a specific location you have to find the location, meaning the Range. As you have seen above, if the range has a length of 0 you can insert before or between existing text, and if it has any length the new text will replace whatever was there before. New and old text need not have the same length.
Counting paragraphs is helpful to find a range. You can also count words or sentences. You can search for a specific word combination. Or you can use a bookmark. In all of these cases you define a range the text of which you can replace outright, or which you can use to find a location relative to it where to insert the text, such as the beginning or end or after the 3rd word or whatever.
You could also use some bookmarks:
You can choose where you put your bookmark and then write on it like this
ThisDocument.Bookmarks("NAME_OF_THE_BOOKMARK").Range.Text = THE_EXCEL_DATA
To place a bookmark you have to click on the selected area and then go on Insert->Bookmarks and then name it.

How to add formatted text to a word document in VBA

How can I take just a substring of text (but formatted, so it will keep it bold/italic/in a table) from one Word document and add it to another in VBA?
You might be better off copying the whole think, paste it in the new document and do some string replacements.

Removing invisible question mark from text - #​E using vba

I have to read the text from the cells of a column in excel and search for it in another sheet.
say for example, the text in sheet1 column A is "Evoked Potential Amplitude N2 - P2." This has to be searched in sheet2 column C. This fails because a question mark appears before the "E" which is not present in the value in the sheet2.
Both are representation of same character in different application. Maybe someone might recognize it.
In the excel sheet I don't see any junk characters, but while handling it in the vb code I see a question mark before the word - Evoke.
This data was extracted from a share point application and this character (?) is not visible to the plain eye. Search and replace functions are not working in this case.
Unicode 8203 is a zero-width space. I'm not sure where it's coming from. It is probably a flaw in the way the data is imported into Excel which you haven't noticed before, but it might be worth fixing.
In the meantime, you can simply use the Mid() function in Excel VBA to remove the unwanted character. For example instead of
x = cells(1,1).value
use
x = Mid(cells(1,1).value,2)
which deletes the first character.

Excel 2007: remove text limitations from cell

I would like to type at least 500 characters in excel sheet cell. But when I do that it only lets me add 1 paragraph like 196 characters. When I add another paragraph, it gives me a message **
"Exceeded Text Limit"
**. How do I resolve this so I can add lot of text in the cell. I googled and tried different things and failed.
Please help !!!
There is a 32,767 character limit in Excel per cell when wrapping text.
You might be able to import it into Access as a Table, with a Memo field that can hold 65,000 characters.
A text box can also hold more, but if you're doing any calculations, this isn't practical.
Try looking here: http://excel.tips.net/Pages/T003163_Character_Limits_for_Cells.html