Results 1 to 12 of 12

Thread: Issue in the VBA macro of MS word.

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    7

    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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Issue in the VBA macro of MS word.

    Welcome to VBForums

    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).

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    7

    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.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    7

    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

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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').

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    7

    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

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Issue in the VBA macro of MS word.

    do you want to post a demo datafile?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    7

    Re: Issue in the VBA macro of MS word.

    Yes I want to upload a demo data file .

    let me try 'manage Attachments'.
    Attached Files Attached Files

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    7

    Re: Issue in the VBA macro of MS word.

    Also Please find the attached word document(with macro) which I use .
    Attached Files Attached Files

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. TESTING = "M_1111"
    2. Selection.TypeText Text:=Chr(13)
    3. Selection.TypeText Text:="TESTING"
    4. Selection.TypeText Text:=Chr(13)
    5. ActiveDocument.MailMerge.Fields.Add Selection.Range, TESTING
    6. 'Selection.TypeText Text:=.DataFields(TESTING)
    7. Selection.TypeText Text:=Chr(13)
    8. Selection.TypeText Text:="END OF TESTING"
    9. Selection.TypeText Text:=Chr(13)
    Last edited by westconn1; Feb 12th, 2008 at 06:39 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    7

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width