none
Can Docvariable Fields have a space in their name? RRS feed

  • Question

  • I have several hundred documents that contain DOCVARIABLE fields and I want a user to be able to select their chosen document and then for VBA code to iterate through it and provide prompts for the associated value.

    All works well until the field & variable have a space in their name and then the variable is created but the update does not work.

    Is it possible to utilise fields with a space in the name or not? 

    The code below shows the kind of code I'm using:

    For Each myfield In ActiveDocument.Fields

    myFieldName = Trim(Mid(myfield.Code, 14, 99))

    ActiveDocument.Variables(myFieldName).Value = "Update to something new" Next myfield ActiveDocument.Fields.Update

    The version of Word doesn't seem to alter the result.

    Anyone have any idea how I can achieve what I need?

    I have managed a solution, of sorts, by using wordbasic and changing the

    name prior to using it but it feels like a bit of a fudge.

    Any and all thoughts appreciated


    • Moved by Max Meng Friday, May 27, 2011 6:16 AM coding (From:Word IT Pro Discussions)
    Wednesday, May 25, 2011 9:27 PM

Answers

  • Use

    myFieldName = Replace(Trim(Mid(myfield.Code, 14, 99)), " ", "")

    or

    myFieldName = Replace(Trim(Mid(myfield.Code, 14, 99)), " ", "_")

    The name of a variable cannot include a space.


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "NanaHawk" wrote in message news:f8c5bc25-14f4-42e7-897e-936e56383044@communitybridge.codeplex.com...

    I have several hundred documents that contain DOCVARIABLE fields and I want a user to be able to select their chosen document and then for VBA code to iterate through it and provide prompts for the associated value.

    All works well until the field & variable have a space in their name and then the variable is created but the update does not work.

    Is it possible to utilise fields with a space in the name or not?

    The code below shows the kind of code I'm using:

    For Each myfieldIn ActiveDocument.Fields

    myFieldName = Trim(Mid(myfield.Code, 14, 99))

    ActiveDocument.Variables(myFieldName).Value = "Update to something new"Next myfield ActiveDocument.Fields.Update

    The version of Word doesn't seem to alter the result.

    Anyone have any idea how I can achieve what I need?

    I have managed a solution, of sorts, by using wordbasic and changing the

    name prior to using it but it feels like a bit of a fudge.

    Any and all thoughts appreciated


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Saturday, May 28, 2011 5:38 AM
  • Field code content /can/ be quite variable, e.g.

     a. the field code itself (in this case, "DOCVARIABLE") does not have to start immediately after the opening field brace, or after a single space, even though it will typically do one of those two things.

     b. the whitespace in a field (e.g. the part between the { and DOCVARIABLE, the part between DOCVARIABLE and the variable name, and so on) will typically only contain spaces, which can be removed using the trim() function. However, the whitespace /can/ contain whitespace characters other than space (e.g. tab etc.), and these can't be removed using trim(). Even Word 2003's Application.Cleanstring only converts some of them to space characters.

     b. a Variable name /may/ contain spaces. In fact, they can also contain other types of whitespace character, so e.g. "my<space character>var" and "my<tab character>var" woud name different variables, although I think it would be unusual to encounter such names. AFAIK, where the name contains whitespace the DOCVARIABLE field would need to surround the name with double quotes, e.g.

    { DOCVARIABLE "my variable name" } 

    (and it is worth bearing in mind that a Variable name can also contain double quotes)

    However, it is also possible to have

    { DOCVARIABLE "my variable name} 

     c. fields may have "switches", e.g. formatting switches, at the end of the field that would not be part of the name

     d. In theory you could have more complicated situations, e.g. where the variable name is the result of another field, e.g.

    { SET v "my variable name" }

    { DOCVARIABLE { v } }

    Unfortunately, in the latter case, what you get from myfield.Code may depend on what is actually visible in the document - it might look something like 

    DOCVARIABLE  !! v ┴

    or

    DOCVARIABLE my variable name

     e. There can be other field types than DOCVARIABLE fields (I ony mention that because your code doesn't actually show the bit where you filter the field code type).

     

    So you either need to find or develop a routine that can deal with every eventuality, or pick and choose what you are going to try to deal with.

    e.g. something like the following would probably get work in most cases

    Dim q as String

    Dim s as String

    q = chr(32)

    s = trim(mid(trim(myfield.code),12))

    if left(s,1) = q then

      myFieldName = mid(s,2,instr(2,s & q,q)-2)

    else

      myFieldName = left(s,instr(1,s & " "," ")-1)

    end if


    Peter Jamieson
    Sunday, May 29, 2011 1:59 PM

All replies

  • Use

    myFieldName = Replace(Trim(Mid(myfield.Code, 14, 99)), " ", "")

    or

    myFieldName = Replace(Trim(Mid(myfield.Code, 14, 99)), " ", "_")

    The name of a variable cannot include a space.


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "NanaHawk" wrote in message news:f8c5bc25-14f4-42e7-897e-936e56383044@communitybridge.codeplex.com...

    I have several hundred documents that contain DOCVARIABLE fields and I want a user to be able to select their chosen document and then for VBA code to iterate through it and provide prompts for the associated value.

    All works well until the field & variable have a space in their name and then the variable is created but the update does not work.

    Is it possible to utilise fields with a space in the name or not?

    The code below shows the kind of code I'm using:

    For Each myfieldIn ActiveDocument.Fields

    myFieldName = Trim(Mid(myfield.Code, 14, 99))

    ActiveDocument.Variables(myFieldName).Value = "Update to something new"Next myfield ActiveDocument.Fields.Update

    The version of Word doesn't seem to alter the result.

    Anyone have any idea how I can achieve what I need?

    I have managed a solution, of sorts, by using wordbasic and changing the

    name prior to using it but it feels like a bit of a fudge.

    Any and all thoughts appreciated


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Saturday, May 28, 2011 5:38 AM
  • Field code content /can/ be quite variable, e.g.

     a. the field code itself (in this case, "DOCVARIABLE") does not have to start immediately after the opening field brace, or after a single space, even though it will typically do one of those two things.

     b. the whitespace in a field (e.g. the part between the { and DOCVARIABLE, the part between DOCVARIABLE and the variable name, and so on) will typically only contain spaces, which can be removed using the trim() function. However, the whitespace /can/ contain whitespace characters other than space (e.g. tab etc.), and these can't be removed using trim(). Even Word 2003's Application.Cleanstring only converts some of them to space characters.

     b. a Variable name /may/ contain spaces. In fact, they can also contain other types of whitespace character, so e.g. "my<space character>var" and "my<tab character>var" woud name different variables, although I think it would be unusual to encounter such names. AFAIK, where the name contains whitespace the DOCVARIABLE field would need to surround the name with double quotes, e.g.

    { DOCVARIABLE "my variable name" } 

    (and it is worth bearing in mind that a Variable name can also contain double quotes)

    However, it is also possible to have

    { DOCVARIABLE "my variable name} 

     c. fields may have "switches", e.g. formatting switches, at the end of the field that would not be part of the name

     d. In theory you could have more complicated situations, e.g. where the variable name is the result of another field, e.g.

    { SET v "my variable name" }

    { DOCVARIABLE { v } }

    Unfortunately, in the latter case, what you get from myfield.Code may depend on what is actually visible in the document - it might look something like 

    DOCVARIABLE  !! v ┴

    or

    DOCVARIABLE my variable name

     e. There can be other field types than DOCVARIABLE fields (I ony mention that because your code doesn't actually show the bit where you filter the field code type).

     

    So you either need to find or develop a routine that can deal with every eventuality, or pick and choose what you are going to try to deal with.

    e.g. something like the following would probably get work in most cases

    Dim q as String

    Dim s as String

    q = chr(32)

    s = trim(mid(trim(myfield.code),12))

    if left(s,1) = q then

      myFieldName = mid(s,2,instr(2,s & q,q)-2)

    else

      myFieldName = left(s,instr(1,s & " "," ")-1)

    end if


    Peter Jamieson
    Sunday, May 29, 2011 1:59 PM