vb.net get cell value from word file table - vb.net

I have some code to get cell value from a word file table.
But it contains something like 口 and will generate a line which is useless, how can I remove this character?
Dim num As String = Trim$(wordApp.ActiveDocument.Tables.Item(1).Cell(6,1).Range.Text)
MsgBox(num)
output is this.
But I'm sure the original cell value doesn't have a empty line.

For example (with VBA):
String = Split(wordApp.ActiveDocument.Tables.Item(1).Cell(6,1).Range.Text, vbCr)(0)
The ¤ is Word's end-of-cell marker, which you need to exclude.

Related

How to modify title text and keep auto numbering - VBA, word

In MS Word (2010 .docx) file I need to modify text of title with autonumber, but keep the autonumber:
Original Word Example:
1.4.12 [ORIGINAL_TEXT]
Intended tile after modification:
1.4.12 [MODIFIED_TEXT]
I can get the [ORIGINAL_TEXT] by
ActiveDocument.Paragraphs(i).Range.Text
This returns [ORIGINAL_TEXT], but not autonumber.
But by setting
ActiveDocument.Paragraphs(i).Range.Text = "[MODIFIED_TEXT]"
the autonumber disappear and the title text is only
[MODIFIED_TEXT]
(autonumber missing)
From there I understand, that assigning anything to ActiveDocument.Paragraphs(i).Range.Text is not a way, because I want to keep autonumber.
I was able to modify the title by
ActiveDocument.Paragraphs(i).Range.InsertBefore ("[MODIFIED_TEXT]")
This results in title in form:
1.4.12 [MODIFIED_TEXT][ORIGINAL_TEXT]
(This looks like pointless, but actually my [ORIGINAL_TEXT] is constant text in whole document - it is placeholder, which tells mactro where to insert autogenerated text "[MODIFIED_TEXT]". Therefore after inserting autogenerated text by InsertBefore I can later simply do find and replace "[ORIGINAL_text]" to "", but I want to avoid this second step)
How it is possible to replace [ORIGINAL_TEXT] by [MODIFIED_TEXT] without loosing the autonumber?
ActiveDocument.Paragraphs(i).Range.Text includes the paragraph mark (carriage return).
To replace the text you can either include a carriage return in your replacement text
ActiveDocument.Paragraphs(i).Range.Text = "[MODIFIED_TEXT]" & vbCr
or modify the range you are replacing
Dim textRange As Range
Set textRange = ActiveDocument.Paragraphs(i).Range
textRange.MoveEnd wdCharacter, -1
textRange.text = "Modified text"
The autonumber is likely in the paragraph formatting so you would want to avoid replacing the paragraph mark unless you can add that formatting back.

Checking if a Word table's cell is empty - Empty cell containing a "marker"

This Word file has a giant table with 2 columns. I have to make a statement like:
If theCellIsEmpty, then
Fill it
End if
The thing is that when I get the content of the cell through:
FirstCellText = ActiveDocument.Tables(Tbl).Cell(Rw, 1).Range.Text
It doesn't get the "" I'm expecting, but it contains a sort of dot, so the text read is never empty.
Here is the dot I'd like to test for:
Moreover, I have no idea what this dot can be or where it came from. I tried to find it in the characters table without success.
Could someone explain me how to test the presence of this "end of line character" or the emptiness of the row? I don't want to check if the cell only has 1 character as if this character is something put there with a purpose, I don't want to delete it.
Also, every single line has this dot at the end...
What is it? How can I use/avoid it?
All you should need is something like:
Dim Cll As Cell
For Each Cll In ActiveDocument.Tables(1)
With Cll.Range
'if all it has is the end-of-cell marker, it's empty, so fill it
If Len(.Text) = 2 Then .Text = "Filler Text"
End With
Next
Thanks for all your help, I might have a simple solution thanks to Freeman, mostly :-)
I took the ascii number of the character to check what it is (through asc(xxxx) function) and it happens to be a "carriage return" having the ascii number "13"
Therefore, so I just need check if the content of the cell gives the ascii number 13 and it's done. (the "asc(xxxxxx)" gives the ascii number of only the first character of the text chain :) )
Of course, if the cell starts with a carriage return and there is text after, it won't be nice, so checking also if the text contained in the cell is only 2 char long will be a nice thing (this "carriage return" is apparently 2 char long... strange...) :)
If Asc(FirstCellText) = 13 and len(FirstCellText) = 2 Then

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.

Get value from cell without transform in vba macros

I have an issue with vba macros. Cell contains value 941144280284022000000. But when try to get this value in macros, variable is equal
9.41144280284022E+20. Is it possible to get "real" value?
Thanks.
Here is an example:
Sub dural()
MsgBox ActiveCell.Text
End Sub
This will insure that long strings of numerals are not converted to numbers.
EDIT#1:
This assumes that the cell actually displays the long string of numerals. If the cell displays ####, that is what the sub will pick-up. If the cell displays 9.41E+20, then that is what my sub will pick-up.
My sub will not necessarily pick-up the contents of the Formula Bar.
Try a simple :
Dim myVar as Variant
myVar = CDec(Range("A1").Value)
If you wish to store a value that looks like a number (or is a number) but has more than 15 digits of precision, then you must either format the cell as text before entering the value, or prepend the value with a single apostrophe to indicate to Excel that the value is to be treated as text and not a number.
If you don't do this, then as soon as the value is entered, there's a good chance it will be altered by Excel. The trailing zeros in the example value mean this does not happen in this specific case, but try changing that last 0 to 1 and you'll see what I mean.
You enter: 941144280284022000001
Excel converts this to: 941144280284022000000
More reading: https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel

How to change the format of the cell in Excel COM to Text

I am using VB.NET to write an Excel Worksheet. I need to write the value 3E01 but it always returns 3.00E+01.
There is one easy way to write it by prepending single quote on the text but it will alter the value that is why I need it to format the cell value into Text.
Format the cell as text before you put the value in it:
theCell.NumberFormat = "#"
theCell.Value = "3E01"
where theCell is a reference you have to the cell.