none
PrivateProfileString limited to 255 characters? RRS feed

  • Question

  • Hi,

    I try to get text to a textbox from an ini-file using System.PrivateProfileString. The string in the ini-file is longer than 255 characters. When I use System.PrivateProfileString, Word seems to only get the first 255 characters and ignore the rest.

    The ini-file is saved as wdFormatDOSText.

    The code: sTemp = System.PrivateProfileString(“C:\Temp\Test.txt”, “Paragraph”, 1)

    sTemp fills only with “Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long “

    I can fill the textbox with more characters than 255.

    I have tried https://support.microsoft.com/en-us/kb/105416 without success.

    Any suggestion?

    I am using Word 2013 and WIndows 10.

    // Ulf Nilsson

    Test.txt

    [Paragraph]

    1=Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters.

    2=Another long text that contains more than 255 characters. Another long text that contains more than 255 characters. Another long text that contains more than 255 characters. Another long text that contains more than 255 characters. Another long text that contains more than 255 characters.

    Count=2

    Monday, March 28, 2016 7:13 PM

Answers

All replies

  • System.PrivateProfileString populates a Windows Registry Key name. Registry Key names are limited to 255 characters. See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724872%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, March 29, 2016 2:25 AM
  • Can this be done in any other way?

    If I convert the Test.txt-file to XML?

    How do I, in that case, read XML-files using VBA?

    // Ulf

    Thursday, March 31, 2016 6:23 AM
  • That depends on what, exactly you're trying to achieve.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, March 31, 2016 6:40 AM
  • I want to get the whole text "Long text that..." and "Another text that..." and put them into a textbox.

    I imagine that the XML-file might look something like this:

    <?xml version="1.0" encoding="UTF-8"?>

    <Paragraph>

    <Text>Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters. Long text that contains more than 255 characters.</Text>

    <Text>Another long text that contains more than 255 characters. Another long text that contains more than 255 characters. Another long text that contains more than 255 characters. Another long text that contains more than 255 characters. Another long text that contains more than 255 characters.</Text>

    </Paragraph>

    //Ulf

    Thursday, March 31, 2016 6:50 AM
  • Other than wanting to put something into a textbox, nothing in what you've posted so far indicates why System.PrivateProfileString might be needed. A possible alternative might be to store the string in a document variable. For example:

    Const StrVar As String = "Long text that contains more than 255 characters. Long text that contains more than 255 characters. " & _
      "Long text that contains more than 255 characters. Long text that contains more than 255 characters. " & _
      "Long text that contains more than 255 characters. Long text that contains more than 255 characters. " & _
      "Long text that contains more than 255 characters." & vbCr & "Long text that contains more than 255 characters. " & _
      "Long text that contains more than 255 characters. Long text that contains more than 255 characters. " & _
      "Long text that contains more than 255 characters. Long text that contains more than 255 characters. " & _
      "Long text that contains more than 255 characters. Long text that contains more than 255 characters."
    ActiveDocument.Variables.Add "MyVar", StrVar

    You could then populate the textbox (I assume it's on a userform), with code like:
    TextBox1.Text = ActiveDocument.Variables("MyVar").Value


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, March 31, 2016 7:14 AM
  • Thank you for your suggestion.

    The reason why I used System.PrivateProfileString is that the text I want to put into a textbox (in a userform) is in a text-file (Test.ini).

    If I am going to put the text in a variable, I first need to retrieve it from the ini-file. How do I do that? The System.PrivateProfileString does not work.

    Or have I miss something?

    //Ulf

    Thursday, March 31, 2016 8:04 AM
  • If the string is in a text file, there's no need to store it in a document variable - you can just read the file directly. For example:

    Const StrFlNm As String = "C:\Temp\Test.txt"
    Dim fso As New FileSystemObject
    TextBox1.Text = fso.GetFile(StrFlNm).OpenAsTextStream(ForReading, TristateUseDefault).ReadAll
    Set fso = Nothing

    Note: you'll need to set a Microsoft Scripting Runtime reference


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, March 31, 2016 8:46 AM