Issue in the VBA macro of MS word.
Greetings to all,
I am new to VB. In my VBA macro I am trying to access a data source(a text file) using the below code snippet.
Dim TESTING As String
..
..
TESTING = "M_1111" 'Accesing the value of M_1111 from data source
Selection.TypeText Text:=Chr(13)
Selection.TypeText Text:="TESTING"
Selection.TypeText Text:=Chr(13)
This works fine, if the value of M_1111 contains less number of characters. But if the value of M_1111 contains more number of characters, the value gets truncated upon accessing.
I don't know why this truncation is happening. Is there any maximum for the number of characters that can be accessed?
Any help appreciated.
Thanks
priby
Re: Issue in the VBA macro of MS word.
Welcome to VBForums :wave:
Thread moved to Office Development forum
Is that the actual code you are using?
If so, the variable TESTING will always contain the text "M_1111" (it will not get a value from a data source). Also the value that is being typed is the text "TESTING", not the contents of your variable (to do that, remove the quotes).
Re: Issue in the VBA macro of MS word.
Sorry the code which I pasted was not complete. Below is the code snippet.
Dim TESTING As String
..
..
TESTING = "M_1111"
Selection.TypeText Text:=Chr(13)
Selection.TypeText Text:="TESTING"
Selection.TypeText Text:=Chr(13)
Selection.TypeText Text:=.DataFields(TESTING)
Selection.TypeText Text:=Chr(13)
Selection.TypeText Text:="END OF TESTING"
Selection.TypeText Text:=Chr(13)
When I run the code, I am able to get the value of M_1111 from the data source(which is a text file) . But the problem is that, if the value of M_1111 has more number of characters(more than around 200 characters), the value gets truncated.
Re: Issue in the VBA macro of MS word.
there is nothing in the code you show that should truncate the value of .datafields(testing), try removing the first line as it may be causing an issue and you are not using it anyway
what is the size of the datafield testing?
Re: Issue in the VBA macro of MS word.
Quote:
Originally Posted by westconn1
what is the size of the datafield testing?
Hi westconn1,
In my data source the value of the TESTING has more than 200 characters.
(I guess this what you meant by 'size of the datafield'.)
Thanks
priby
Re: Issue in the VBA macro of MS word.
What data type is it, and where does it come from?
I don't know anything about .DataFields, but I am guessing that it provides a link to a database. If so, and the field is a Memo data type, behaviour like this is not uncommon; The easy fix (the data is short enough) is to change the data type to a smaller text based data type (in Access 'Text', for others 'VarChar').
Re: Issue in the VBA macro of MS word.
Hi,
My data source is a simple text file of the following form:
M_1111,
"Visual Basic (VB) is a programming environment from Microsoft in which a programmer uses a graphical user interface to choose and modify preselected sections of code written in the BASIC programming language. Visual Basic is also widely used to write working programs. Microsoft says that there are at least 3 million developers using Visual Basic.",
Thanks,
priby
Re: Issue in the VBA macro of MS word.
do you want to post a demo datafile?
1 Attachment(s)
Re: Issue in the VBA macro of MS word.
Yes I want to upload a demo data file .
let me try 'manage Attachments'.
1 Attachment(s)
Re: Issue in the VBA macro of MS word.
Also Please find the attached word document(with macro) which I use .
Re: Issue in the VBA macro of MS word.
as far as i can tell the length of the datasource.datafield has limitation
the alternative is to insert a merge field in the document, this will return the full length of the text
vb Code:
TESTING = "M_1111"
Selection.TypeText Text:=Chr(13)
Selection.TypeText Text:="TESTING"
Selection.TypeText Text:=Chr(13)
ActiveDocument.MailMerge.Fields.Add Selection.Range, TESTING
'Selection.TypeText Text:=.DataFields(TESTING)
Selection.TypeText Text:=Chr(13)
Selection.TypeText Text:="END OF TESTING"
Selection.TypeText Text:=Chr(13)
Re: Issue in the VBA macro of MS word.
Thanks westconn1 for your suggestion.
In the test code, I'm just printing the value of M_1111. So just using the merge field is an alternative.
But in the actual code(which I am yet to write), I need to get the value of merge field M_1111 (which has some separators) and split them and then print it at different places.