Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: auto insert docvariable field

  1. #1

    auto insert docvariable field

    Hi,

    I was wondering if it's possible to insert a docvariable field in the document by clicking a button on a userform. Basically, depending on scenario, i would like to be able to add as many docvariable fields of the same kind ("name" or "surname" for example) as necessary.

    I hope it's possible, otherwise my project is kind of useless .

    thanks,
    sessionone

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Perhaps describe the project in a bit more detail.

    If you want to repeat the same field (and not just a docvariable field), this can be easily done.

    But how - exactly - are you thinking of doing this? On the userform do you have something that determines how many times? If so, do you have locations pre-determined - so if you choose three times, there are three locations to put the value in?

    Are you tied to using DOCVARIABLES? There are other possibilities.

  3. #3
    Hi,

    Sorry, about that. I'll give you more details.

    I have a userform with name and surname textboxes for clients (there are other textboxes, but only these two are of my concern). In the document, I have varName and varSurname fields.

    Now the clients come from different municipalities, but many come from the same municipality. I have a combobox with all municipalites. So, I pick a municipality, then enter name and surname, click ok, and the docvariable fields are filled, but only for one person. What I would like to be able to do is to have an "add another client" button on the userform. Upon clicking of this button, I'd love to get a new set of docvariable fields varName and varSurname in the document. I realize, that these docvariables would have different names, or perhaps have a variable attached to them automatically. (??)

    Also, I can't just insert 10 fields varName1 through varName10, because the institution I'm doing this thing for is brutal about the protocols of letter writing. If there is only one person in the letter, the line just below that person must be the name, position held, and signature of the manager. If, for example, varName6 through varName10 are blank, then the space between the client section and the manager section of the letter is too great. So yes, I would like to have those locations predetermined. (if it's possible, of course)

    I'm not tied to docvariables. I've tried bookmarks, but they were pain in the bum, so docvariables were much much better and easier to work with. Appart from them, I don't really know much, because I'm new to programming...

    I hope this has cleared things up a bit, and I also hope that this project I'm trying to do is actually doable, and I'm just not overly optimistic

    Thanks again,
    sessionone

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    If you need new variables, then just add them. Once added:[vba]
    ActiveDocument.Variables.Add "Being", "Gerry Knight"

    [/vba]you can then insert a DOCVARIABLE field for it.
    [vba]
    Selection.Fields.Add Range:=Selection.Range, _
    Type:=wdFieldEmpty, _
    Text:="DOCVARIABLE Being ", _
    PreserveFormatting:=True
    [/vba]Assuming you are using Selection for placement.

    So.....
    [vba]
    Public j As Long
    Public CurrentVarName As String
    Public CurrentVarSurname As String
    Sub NewClient_Click()
    j = j + 1
    ActiveDocument.Variables.Add "varName" & j
    ActiveDocument.Variables.Add "varSurname" & j

    CurrentVarName = varName & j
    CurrentVarSurName = varSurname & j

    txtName.SetFocus ' the textbox for the name
    End Sub
    [/vba]This makes new variables and returns focus to the textbox to put in the name.

    Note that:

    1. the DOCVARIABLE field is not in the document yet.

    2. Nor has a .Value been set for the new variables.

    Your button: "click ok, and the docvariable fields are filled" will have to be adjusted to do this. It can be done, but - as usual - you will have to give actual real details in order to achieve that code.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    fumei -- you helped me in another thread by suggesting docvariables.

    How can I delete one from the document if I no longer need it?

    Could not find a .Delete method


    Paul

    PS Hope this isn't thread hi-jacking

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    No, I do not think it is hijacking. It has some relevance.

    "Could not find a .Delete method"

    There is a .Delete method...but it only shows up in IntelliSense if you are pointing to an explicit item in the Variables collection.
    [vba]
    ActiveDocument.Variables.no Delete
    [/vba]This makes sense, as VBA is not being told WHAT to .Delete.
    [vba]
    ' create a Variable ("Blue"), and give it a .Value (6)
    ActiveDocument.Variables.Add Name:="Blue", Value:=6
    MsgBox ActiveDocument.Variables("Blue")
    ' displays 6

    ActiveDocument.Variables("Blue").Delete
    MsgBox ActiveDocument.Variables("Blue")
    ' returns an error message:
    ' Run-time error '5825'
    ' Object has been deleted.

    [/vba]So, to delete one, simply name it, and delete it.

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Just to expand a bit here. The value of any DOCVARIABLE field must be set by VBA.

    You can insert a new DOCVARIABLE field using the menus, but to assign a Value (or change it) requires - I think, but macropod or Tony can confirm - code, as it is a variable.

    So you could:

    Insert > Field > DOCVARIABLE and put in GeorgeClooney as the name. In the document (showing field codes) you will see:

    { DOCVARIABLE GeorgeClooney \* MERGEFORMAT }

    If you toggle field codes off, you will see:

    Error! No document variable supplied.

    However, if you then execute (in code):[vba]

    ActiveDocument.Variables("GeorgeClooney").Value = _
    "Mary had a little lamb"
    [/vba]in the document, you will still see (with field codes visible):

    { DOCVARIABLE GeorgeClooney \* MERGEFORMAT }

    However...if you Update the field, instead of:

    Error! No document variable supplied.

    You will see:

    "Mary had a little lamb"

    It is important to understand the relationship between the actual Variable itself, and a Field (DOCVARIABLE) that points to that variable.
    [vba]
    ' this creates the variable YaddaYadda, and
    ' sets a Value
    ActiveDocument.Variables.Add "YaddaYadda", "Blah Blah Blah"

    ' this creates a Field that POINTS
    ' to the variable YaddaYadda
    Selection.Fields.Add Range:=Selection.Range, _
    Type:=wdFieldDocVariable, Text:="YaddaYadda"

    [/vba]In the above, with field codes off, you will see "Blah Blah Blah".

    Now suppose you executed:[vba]
    Selection.Fields.Add Range:=Selection.Range, _
    Type:=wdFieldDocVariable, Text:="Yippee"
    [/vba]but you have NOT created and set a value for Yippee previously.

    Word will put in the field and its code, but it will not be valid, and thus: Error! No document variable supplied.

    Yet, if you then execute:[vba]
    ActiveDocument.Variables("Yippee").Value = _
    "Once upon a time."
    [/vba]and Update the field, then you WILL get the .Value ("Once upon a time.").

    The point being is that WITH:
    Error! No document variable supplied.

    the field code looks like:

    { DOCVARIABLE Yippee \* MERGEFORMAT }

    and after you DO set a Value...it still looks like:

    { DOCVARIABLE Yippee \* MERGEFORMAT }




    Are we having fun?

  8. #8
    hi fumei,

    thanks for your suggestion; however, being the novice that i am, i'm experiencing a few problems. first of all, i get a compile error saying that "j" variable is not defined.
    also i don't quite understand what you mean by saying

    1. the DOCVARIABLE field is not in the document yet.

    2. Nor has a .Value been set for the new variables.
    .

    Perhaps I didn't quite clearly explain what I'm trying to achieve. So I'll try again. i have a very simple userform with txtName and txtSurname. On the document I have varName and varSurname docvariable fields. When I click OK button, the values of the textboxes are filled in their respective docvariable fields. That I've managed to do.
    I would like to have an ADD button, which would add new docvariable fields just below the existing ones. That is, i enter name and surname, click ADD, the current values in the textboxes are transfered to the fields, the textboxes are then cleared, new fields are created, then a new name and surname are entered in the textboxes, click ADD again, the values transfered and so on and on and on.
    My document looks like this at the moment:

    LOGO

    Municipality

    The following person submitted the application forms:

    Mr. Jim Morrison, his details, and blah blah blah.

    Signed
    Manager Ms. D. Cruella

    What I want to have is this (depending on how many people from the same municipality submit the forms):

    LOGO

    Municipality

    The following persons submitted the application forms:

    Mr. Jim Morrison, his details, and blah blah blah.
    Mr. Mick Jagger, his details, and blah blah blah.
    Ms. Paris Hilton, her details, and blah blah blah.

    Signed
    Manager Ms. D. Cruella

    ...and then click SAVE and PRINT.

    Sorry for the long post and for being dumb
    I look forward to you suggestions, as I'm quite tired of googleing and not finding any tips on how to do this.

    cheers,
    sessionone

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "Perhaps I didn't quite clearly explain what I'm trying to achieve. So I'll try again."

    Au contraire. I understand exactly what you want to do. You were clear enough. I have made a document that does precisely what you want to do. Here is what my document does.

    1. a userform with two textboxes (txtName and txtSurName), and two commandbuttons (cmdAdd and cmdExit).

    2. when initially started the focus (cursor) is in txtName

    3. user (me) types first name, and presses Tab, which moves focus to txtSurname.

    4. user (me) types surname and presses Tab, which moves focus to cmdAdd, which has the Caption "Add".

    5. pressing Enter (same as clicking the "Add" button as that is where the focus is), inserts the .Text values of the textboxes (txtName and txtSurname) into the existing DOCVARIABLES in the document. These are named varName1 and varSurname1. It also adds a paragraph above "Signed"..to be used if you want to add new clients. It updates the fields in the document, thus showing the input from the textboxes,

    6. the commandbutton cmdAdd resizes itself and renames its Caption to "Add NEW client.

    7. user (me) presses Enter (as the focus is still on cmdAdd) - or if you insist, click cmdAdd (now with the Caption "Add NEW client").

    8. the click event of cmdAdd first checks its own Caption.

    If it is "Add" and it is the first time it has been clicked (set by a boolean...if it has been clicked this is True, if it has not been, i.e. it is the first time, then it is False), then the procedure actions Step #5 above.

    If the boolean is True (the cmdAdd button has been used once and therefore the Caption is "Add NEW client"), then it:

    a) clears txtName and txtSurname
    b) set focus to txtName - for user input
    c) resizes cmdAdd and changes its Caption back to "Add"

    9. user (me) types new client name/surname and Tabs to "Add"....or if you insist, clicks the now "Add" again.

    10. the click event checks its Caption - it is "Add"; it checks the boolean (has it been used before?). It HAS been used before...so.....

    a) it creates NEW variables varName + j, with j being a variable itself. The original was varName1, the next one will be....varName2...and so on.
    b) it inserts new DOCVARIABLE fields at the new paragraph (above Signed) with those new names.
    c). it updates the fields in the document, thus showing the new client information.
    d) changes the Caption back to "Add NEW client"...ready for another, if needed.

    The other commandbutton simply Unloads the userform.

    The coding is NOT difficult...getting the logic just right is.

    Oh and just to make it easier for the user, when I change the commandbutton Caption from "Add" to "Add NEW client", I also bold one of them to make it stand out. So it changes from:

    Add

    to

    Add NEW client

    Demo attached. Click "Show The Form" on the top toolbar to display the userform. To see it work, for example, type (actual keystrokes in bold):

    Harry
    tab (to go to next textbox)
    Belefonte
    tab (to go to cmdAdd button)
    ....values go into doc
    Enter (to action cmdAdd button, now Caption as "Add NEW client"; textboxes cleared, focus moves to txtName)
    George
    tab
    Clooney
    tab
    Enter

    Result?

    Harry Belefonte
    George Clooney

    above Signed, with each element as a DOCVARIABLE, like this:

    { DOCVARIABLE varName1 \* MERGEFORMAT } tab { DOCVARIABLE varSurname1 \* MERGEFORMAT }

    { DOCVARIABLE varName2 \* MERGEFORMAT } tab { DOCVARIABLE varSurname2 \* MERGEFORMAT }


    I have said it before, this kind of stuff is primarily working out the logic. The coding part is a VERY minor part of getting things like this to work.

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    OK, I understand that some may be a little annoyed that I password protected the code. Of course those of you who are advanced VBA users will find a way around that.

    The OP has requested I give the password to allow access to the full code. I am not going to do that. I am not getting paid to hand over full solutions. However, here is the full code for the cmdAdd button that does 99% of the work. I am not posting the few small, but crucial, instructions that make up the 1%.

    In other words, the code below for cmdAdd_Click does the work, yes. But there are instructions in userform_Initialize that are important, AND there are declarations of variables that I am not posting.

    What those are, can be extrapolated. And they better be, because if you execute Sub cmdAdd_Click - as it is - a run-time error will occur.[vba]
    Private Sub cmdAdd_Click()
    Dim strName As String
    Dim strSurname As String
    Dim oField As Field
    strName = txtName.Text
    strSurname = txtSurname.Text

    Select Case cmdAdd.Caption
    Case "Add"
    ' first time around
    If MakeField = False Then
    ' insert textbox contents if not blank
    If txtName.Text <> "" And _
    txtSurname.Text <> "" Then
    ActiveDocument.Variables("varName" & j) _
    .Value = strName
    ActiveDocument.Variables("varSurname" & j) _
    .Value = strSurname
    Else
    MsgBox " No client name values."
    Exit Sub
    End If
    ' increment counter for NEW client (if needed)
    j = j + 1
    ' make boolean TRUE
    MakeField = True
    ' resize button, caption and font
    With cmdAdd
    .Width = 66
    .Left = 138
    .Caption = "Add NEW client"
    .Font.Bold = True
    End With
    lblMessage.Caption = "Enter new client data."
    Else
    ' NOT first time (NakeField = True)
    ' need to add Variables
    ActiveDocument.Variables.Add _
    Name:="varName" & j, _
    Value:=strName
    ActiveDocument.Variables.Add _
    Name:="varSurname" & j, _
    Value:=strSurname
    ' insert the DOCVARIABLE fields
    With Selection
    .Fields.Add Range:=Selection.Range, _
    Type:=64, Text:="varName" & j
    .TypeText Text:=vbTab
    .Fields.Add Range:=Selection.Range, _
    Type:=64, Text:="varSurname" & j
    End With
    End If
    With cmdAdd
    .Width = 70
    .Left = 138
    .Caption = "Add NEW client"
    .Font.Bold = True
    End With
    ' update fields
    For Each oField In ActiveDocument.Fields
    oField.Update
    Next
    ' add a paragraph
    With Selection
    .GoTo what:=wdGoToBookmark, Name:="BeforeSignature"
    .Collapse 1
    .Move Unit:=wdCharacter, Count:=-1
    .TypeParagraph
    End With
    j = j + 1
    Case Else ' Caption is Add NEW client
    ' clear textboxes
    txtSurname.Text = ""
    With txtName
    .Text = ""
    .SetFocus
    End With
    ' resize button and change Caption
    With cmdAdd
    .Width = 36
    .Left = 156
    .Caption = "Add"
    .Font.Bold = False
    End With
    End Select
    End Sub

    [/vba]As you can see, while a little extensive in length, there is NOT anything obscure or way out there in the coding.

    HINT to OP: notice there is use of a bookmark. Turn bookmarks visible ON and you can see it is just before "Signed". I use it to know where to put any NEW client data.

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    PS. everything that I state can be extrapolated CAN be. Just really read the logic steps I wrote in post #9. It is all there.

    Finally, I really do have to question the use of DOCVARIABLE fields this way. DOCVARIABLES are designed to retain variable values between macro sessions, or document close/open. As I understand the purpose here, the object is to be able to insert text at a certain location, with the actual text...ummm....variable.

    If I understand correctly, it is an issue of:

    George Clooney
    Marlon Brando
    Golda Meir

    put before Signed

    or......

    George Clooney
    Golda Meir

    put before Signed. In BOTH cases, really, it is just text.

    So.....here is another version (NoDocsvars_SimpleVer.doc) that does NOT use DOCVARIABLES, but DOES allow multiple clients to be put in before "Signed".

    The result is exactly the same as the document using DOCVARIABLES!

    Actual keys in bold again:

    Harry
    tab (to go to next textbox)
    Belefonte
    tab (to go to cmdAdd button)
    Enter (to action cmdAdd button; textboxes cleared, focus moves to txtName,....values go into a label on the userform )
    George
    tab
    Clooney
    tab
    Enter (actioning the cmdAdd button)
    tab
    tab
    tab
    tab four Tabs to move focus to the Done button...or just click the Done button.

    The contents of the label.Caption is:
    Harry Belefonte
    George Clooney



    Result?

    Harry Belefonte
    George Clooney

    now put before "Signed". No use of DOCVARIABLES required whatsoever. Now, yes again, you can use DOCVARIABLES - as I have demonstrated in the previous demo...but I still do not understand WHY you want to use them.

    If the purpose - the logic - is to simply be able to vary the text inserted at a specific location, there are a heck of a lot of easier ways.

    Compare all the code for cmdAdd for the DOCVARIABLES example (which also did NOT include other essential code) with the following, which is the full code for the alternative described above.[vba]
    ' the userform module

    Option Explicit
    Public j As Long

    Private Sub cmdAdd_Click()

    ReDim Preserve ClientName(j)
    ClientName(j) = txtName.Text

    ReDim Preserve ClientSurname(j)
    ClientSurname(j) = txtSurname.Text

    ' build client list string
    lblClientList.Caption = lblClientList.Caption & _
    ClientName(j) & vbTab & ClientSurname(j) & vbCrLf

    ' clear textboxes and reset focus
    txtSurname.Text = ""
    With txtName
    .Text = ""
    .SetFocus
    End With
    ' increment counter
    j = j + 1
    End Sub


    Private Sub cmdExit_Click()

    With Selection
    .GoTo what:=wdGoToBookmark, Name:="BeforeSignature"
    .Collapse 1
    .Move Unit:=wdCharacter, Count:=-1
    ' and dump in full client list
    ' as one string
    .TypeText Text:=lblClientList.Caption
    End With

    Unload Me
    End Sub

    ' in a standard module

    Option Explicit

    Public ClientName()
    Public ClientSurname()
    [/vba]See the difference? It is MUCH simpler, but the results are EXACTLY the same. This is a good demonstration of why working out the logic, the actual REAL requirements is so important.

  12. #12
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Finally, you do not even need two separate arrays (ClientName and ClientSurname)...a two element array would work as well.

  13. #13
    Hi,
    Thanks again. I'll try this when I get home.

    I decided to go for DOCVARIABLES, because I thought they were a better alternative to bookmarks. Also, with my very very very limited knowledge of VBA, I didn't really know how to do this whole thing otherwise.

    Your second demo does seem a lot simpler, but I was I thinking of using the DOCVARIABLE field values for Save As file name string. Would it be possible to do this with your second suggestion? (sorry if this is a dumb question..)

    Thanks,
    sessionone

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    There are no dumb questions, if you do not know the answer.

    "I thinking of using the DOCVARIABLE field values for Save As file name string"

    Well sure, you could...but....which one????? This is - again - a matter of logic, not code. Once you have figured out the logic for determinign WHAT is the string to be used for the SaveAs filename, then yes, absolutely you can do it without a DOCVARIABLE. The filename is a string, where you get the string to use is totally up to you.

    "I decided to go for DOCVARIABLES, because I thought they were a better alternative to bookmarks."

    Why? Explain your logic in determining that DOCVARIABLE would be a better alternative? I am not (for now) disputing anything...I can not, as I do not know your logic is deciding that.

    I will simply reiterate the design purpose of DOCVARIABLE fields. To retain values when a document is closed. Generally values that are used in code. Not always of course, but the point is that if you are wanting to use text, then most of the time the text can be acquired by other means.

  15. #15
    Hi,

    Ok, for the file name I'd like to use surname, first letter of the name, and date (autodate, which I think is the docvariable field). So, the length of the file name string will depend on the number of clients being added to the document. Also, I want the document to be saved as .doc and sent to a particular folder, depending on the choice of the "municipality" combobox.

    There was not logic in me going for DOCVARIABLES. It was a matter of choice between DOCVARIABLES and bookmars. This was, once again, due to my lack of knowledge on how to do things. I found bookmarks quite annoying and I couldn't make them work the way I wanted them to work. DOCVARIABLES were much simpler, and at the beginning I got the results I wanted. But now, when my project is growing, it's getting more complicated and I'm stuck

    Basicaly, I'm trying to make letter preparation as simple and as quick as possible. That includes less typing (that is, SaveAs file names, especially when there are many clients and the user has to type their surnames over and over again) and less wondering around the folders (the documents are organized by the municipalities). Also, the user should be able to write those letters without closing and reopening of the form, once one letter is done and the user is ready to do another one. So the text in the document template must be erased as many times as necessary.

    I'm beginning to understand that, keeping in mind the purpose of DOCVARIABLES, they indeed are not the best choice. But once again, I only knew two ways of how to do what I want to do - DOCVARIABLES and bookmarks.

    Thanks again,
    sessionone.

  16. #16
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I will say it again. Work out EXACTLY what the logic is, and go from there. Not being critical here...

    "Ok, for the file name I'd like to use surname, first letter of the name, and date (autodate, which I think is the docvariable field). So, the length of the file name string will depend on the number of clients being added to the document. Also, I want the document to be saved as .doc and sent to a particular folder, depending on the choice of the "municipality" combobox."

    ALL of this is possible. All of it. Just determine EXACTLY what the logic is.

    Let's try to - and this is the only way to actually determine logic - step by step. And I am just going to use one example...

    "for the file name I'd like to use surname, first letter of the name, and date"

    OK. You just used your userform to put in five clients. each one, of course, has a surname. So....WHICH ONE are you going to use? First one? The second one? If you put in five clients...WHY would you choose the first one (or second one, or fouth one)? YOU have to determine that; or if you wish, you could make the user do it...but somebody has to. You can not just say "I'd like to use surname, first letter of the name, and date" - you have to use some sort of logical process to actually DO that.

    But it most certainly can be done. I can think of a couple of ways, although the first decision is whether you (as the programmer/developer) makes the decision as to which one of the clients to use, or the user.

    One other comment: "Also, the user should be able to write those letters without closing and reopening of the form, once one letter is done and the user is ready to do another one. So the text in the document template must be erased as many times as necessary. "

    Oh....bad. This is NOT using a template (as Word understands template). What you are describing is what used to be called boiler-plate documents. Those are NOT templates.

    "Also, I want the document to be saved as .doc and sent to a particular folder, depending on the choice of the "municipality" combobox."

    This part is easy. Especially if you were using this as a true/proper template.[vba]
    ActiveDocument.SaveAs Filename:=build your string
    [/vba]such as:
    [vba]
    ActiveDocument.SaveAs Filename:=cboMunicipality.Text & _
    client_surname? & Date? & ".doc"
    [/vba]I put the question marks as I do not know how you are getting a surname if you have multiple surnames, Further:

    "autodate, which I think is the docvariable field" No it is not a DOCVARIABLE. DOCVARIABLES are - I have mentioned this before - fields that point to a variable that YOU are created.

    Yes, there are internal DATE fields you could use, but I have no idea what you even mean by "autodate". AUTOdate. What is auto about a date?

  17. #17
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Let me suggest something. I have mentioned this a number of times to many people here.

    Write it out!.

    Preferably on paper...with a pencil.

    Write it out. Make boxes like a flowchart at first; make hand arrows showing the logic flow (THIS has to happen...then THAT). You will - I promise you - find it getting more and more complicated, yet at the same time, if you REALLY work out the step-by-step pieces, more...ummm, sane.

    Writing/sketching it out on paper allows you to make a mess of things as you go...oh, crap THAT actually has to happen first...oh, crap...I need a variable to be set BEFORE this happens...not after and yadda yadda yadda.

    For example:

    You are enter five different client into one document. Why...I have no idea, but let's assume you are (since it seems this is something you want to be able to do...although frankly, I have my doubts; I think this is a design flaw). WHEN - at what point - are you determining which surname to use?

    Since you are doing this from a userform, you COULD have for each client input, a "Principal Name" checkbox appear (or a button) and if checked/clicked THAT current client name would be used.

    I am just tossing that out, there could a number of ways to doing it. the point being though is you must figure it out, and THAT is logic....not coding.

  18. #18
    Hi again,

    I do agree that scetching is a good idea, although I haven't done it, because initially I thought that it is a fairly simple project I'm trying to do. In fact, the idea is very very simple. Instead of typing into the document itself, the user enters all the data through the userform.

    For example:

    You are enter five different client into one document. Why...I have no idea, but let's assume you are (since it seems this is something you want to be able to do...although frankly, I have my doubts; I think this is a design flaw). WHEN - at what point - are you determining which surname to use?
    Because I am doing this thing for an incredible bureaucratic machine, a system which uses spartan methods to achieve even the most simple tasks, I ask you to trust me that what I'm trying to do is exactly what has to be done. While I'm not arguing that there might be flaws in my design, the biggest flaw is the system itself. I can't tell you why it has to be done, because I don't think you will understand; I don't fully understand it myself, but there is no one to complain to, so we just have to do it. Anyway that's a totally different topic.

    At the moment all SaveAs strings are either "Clooney G. - 2009-06-09" (if there's only one client) or "Clooney G., Minouge K., Gates B., Iphone A. - 2009-06-09 (if there are multiple clients). And the user has to type this. The date is the date when the document was created. THIS IS exactly what I would like to have without actually typing the whole thing into Word's SaveAs dialog. So, as far as I understand, I should be able to get these values from the DOCVARIABLE fields. Ok, there might be other methods, but I'm not aware of them.

    Thanks again,
    sessionone

    p.s. I'll post the whole process later on, and hopefully it will be more clear.

  19. #19
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Hey...I work for the Canadian federal government...I know what stupid multiplex convoluted bloated pieces of crap processes are like. in fact, you have my sympathies.

    OK, if the file name is to include ALL the surnames, that makes it easier. VERY easy. However...again...this logic..and I have to make a comment. Although this is still easy - I have adjusted the demo to do EXACTLY what you ask for, and it took just a couple of minutes...and does not use any DOCVARIABLES.

    I notice you are using "Clooney G."

    Oh-oh. I am assuming the string input would be "George". So you have to do string manipulation.

    Fortunately, string manipulations, while sometimes convoluted, are straightforward. So.....

    Remember the simple version - no DOCVARIABLES? The string going into the document before "Signed" is...just a string. It was built. Every time the Add button was clicked, it took the text from txtName (the client first name), and txtSurname (duh), and appended them.[vba]
    ' build client list string
    lblClientList.Caption = lblClientList.Caption & _
    ClientName(j) & vbTab & ClientSurname(j) & vbCrLf
    [/vba]At the end - with the Done button - it simply dumped the whole string into the document.

    Eg.

    George Clooney
    Bozo TheClown
    Her Majesty

    Remember though, these are just strings.

    So you want to get:

    "Clooney G., TheClown B., Majesty H. - 2009-06-09"

    You are going to get a lesson in string functions. Here is the amended code (just the added in parts, the full code is in the attached demo doc file). [vba]
    ' a new Public string variable to hold filename to use
    Public FileToSave As String

    ' in the cmdAdd procedure
    ' build filename save string
    FileToSave = FileToSave & ClientSurname(j) & " " & _
    Left(ClientName(j), 1) & _
    "., "

    ' in the cmdExit (Done) procedure
    FileToSave = FileToSave & Format(Now, "yyyy - mm - dd") & _
    ".doc"

    " VOILA! your filename
    ' "Clooney G., TheClown B., Majesty H., 2009-06-09.doc"

    ' you could save it with.....
    ActiveDocument SaveAs Filename:=myPath & _
    FileToSave
    [/vba]Da-da. Done.

    Adding a Path with multiple folders is just as easy really.

    Demo attached. Try it. Type:

    George
    tab (to go to next textbox)
    Clooney
    tab (to go to cmdAdd button)
    Enter (to action cmdAdd button; textboxes cleared, focus moves to txtName,....values go into a label on the userform )
    Bozo
    tab
    TheClown
    tab
    Enter (actioning the cmdAdd button)
    Her
    tab
    Majesty
    tab
    Enter (actioning the cmdAdd button)
    tab
    tab
    tab
    tab (four Tabs to move focus to the Done button...or just click the Done button.)

    The text will be put in the document, AND a string will be available for a file SaveAs.

    Like I keep saying, the coding to NOT hard. The coding is NOT hard. The thinking, what goes on in your head....now THAT can be hard.


    Are we having fun? Again though, there is not a DOCVARIABLE in sight, and - at least as far as you have described your needs - there is no need for any.

  20. #20
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    No password on the project, so knock yourself out. What a weird expression.

    The thing to keep in mind is everything - so far - that you seem to think is difficult is actually very basic string manipulation. What looks to you like a complicated thing like getting multiple names into something to use as a filename, are really BASIC string manipulations.

    OK, OK, I know I am perhaps more experienced, but the FACT of the matter is - just look at the code - it is not difficult coding.[vba]
    FileToSave = FileToSave & ClientSurname(j) & " " & _
    Left(ClientName(j), 1) & _
    "., "

    [/vba]can look formidable, but really, all it is:

    1. the value of FileToSave is now (FileTopSave =) ;
    2. the current string value of FileToSave (FileToSave)
    3. PLUS the current value of ClientSurname(j) - eg. "Clooney" ClientSurname(j)
    4. PLUS a space (" " )
    5. PLUS the first character on the left of current ClientName e.g. "George" - Left(ClientName(j), 1)....thus "G"
    6. PLUS a period and a comma ".,"

    All together now....

    "Clooney G.,"

    The next time round, FileToSave (currently "Clooney G.,")
    becomes:

    1. the value of FileToSave is now ("Clooney G.");
    2. etc. etc. etc.

    When working with Excel, you have to really understand formula and especially bracketing expressions. With Word, as it is text (strings), it would be well worth your while to try and really get a handle on what you CAN do with strings. Which is vitually anything.

    Never mind HOW to do it - the code.

    Think about WHAT you need to do. This is logic.

    So, when I wrote that little wee code, it came out (the logic) as:

    I need the full string of the first name, and the full string of the surname for the document, but for the filename I need the first letter of the first name and the whole Surname in reverse order, with a space between them, and THAT string followed by a period (for the initial) and a comma.

    Document string: George Clooney
    Filename string: Clooney G.,

    THEREFORE logically I need a separate variable to hold the build of the filename string...as opposed to the document string.

    Thus, a new variable required: FileToSave As String

    Then it is simply using the built-in string manipulating functions of VBA.

Posting Permissions

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