Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 40 of 40

Thread: Solved: justify paragraph via VBA

  1. #21
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Agree with everything Gerry says, and ..

    I don't like that you hard code "Rockwool" (and all the other options) twice - it's just asking for trouble. One way to avoid this is to store the document text in the combobox. I have copied Gerry's document and done this (see attached).
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  2. #22
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I was going to do that for the OP...but am too busy to do other than what I did. I only corrected/amended part of the commandbutton Click event.

    I absolutely agree. It is NOT a good idea to hard code values twice. It is indeed asking for trouble.

    Very nice coding Tony.

    white_flag, it is also a good and polite thing to respond to user choices. As you have the requirement to select an item from the combobox AND check the checkbox (something I am not sure I agree with), it would be good to give the user a second chance if they selected an item, but forgot to check the checkbox. I amended the commandbuton code.[vba]Private Sub CommandButton1_Click()
    Dim whatever As VbMsgBoxResult

    If CheckBox_Insulation1 = True Then
    Insulation
    Else
    whatever = MsgBox("You are clicking OK with no checkbox checked. " & _
    vbCrLf & _
    "Do you really wish to do nothing?" & vbCrLf & _
    vbCrLf & vbCrLf & _
    "Click Yes to exit and do nothing." & vbCrLf & _
    "Click No to try again.", vbYesNo)
    If whatever = vbYes Then
    GoTo Finish:
    Else
    Exit Sub
    End If
    End If

    Finish:
    Application.ScreenUpdating = False
    Unload Me

    End Sub
    [/vba]NOTE: this does NOT cover the same situation for the second combobox....hey, we can not do all your coding for you. But it does show the process for some error-trapping and/or user interface.

    I also made the comboboxes bigger so you can see both columns (as Tony coded them) better.

  3. #23
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    BTW: it is also a good idea to have a Cancel mechanism.

  4. #24
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Lastly, the reason I liked Tony's code so much is that it makes it impossible for the user to make their own alterations of the combobox items.

    As it stands, the user can put "yaskahks shdklq2648adha" into a combobox. This obviously is likely not a correct value. Tony's code, by using [vba]
    .List(.ListIndex, 1)

    [/vba]will error out, as that is NOT an item on the List.

    Properly speaking, this should ALSO be error-trapped and dealt with. With something like:
    [vba]
    Option Explicit

    Public ItIsOK As Boolean

    Private Sub Insulation()
    On Error GoTo Gibberish:
    With ComboBox_Insulation1
    Call FillBM("Insulation1", .List(.ListIndex, 1))
    End With
    With ComboBox_Insulation2
    If .ListIndex >= 0 Then
    Call FillBM("Insulation2", .List(.ListIndex, 1))
    End If
    End With
    ItIsOK = True
    GoTo MyEnd:

    Gibberish:
    If Err.Number = 381 Then
    MsgBox "Huh???? INVALID INPUT! Please try again."
    ComboBox_Insulation1.ListIndex = 0
    ItIsOK = False
    End If

    MyEnd:
    End Sub
    [/vba]for the Insulation procedure, and of course you would have to amend the commandbutton procedure as well....[vba]
    Private Sub CommandButton1_Click()
    Dim whatever As VbMsgBoxResult

    If CheckBox_Insulation1 = True Then
    Insulation
    ' everything is OK
    If ItIsOK = True Then
    GoTo Finish
    ' it is NOT OK, so simply exit
    Else
    Exit Sub
    End If
    Else
    whatever = MsgBox("You are clicking OK with no checkbox checked. " & _
    vbCrLf & _
    "Do you really wish to do nothing?" & vbCrLf & _
    vbCrLf & vbCrLf & _
    "Click Yes to exit and do nothing." & vbCrLf & _
    "Click No to try again.", vbYesNo)
    If whatever = vbYes Then
    GoTo Finish:
    Else
    Exit Sub
    End If
    End If

    Finish:
    Application.ScreenUpdating = False
    Unload Me

    End Sub
    [/vba]
    Last edited by fumei; 01-13-2010 at 01:56 PM.

  5. #25
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    We are a heck a long way from the Subject: justify paragraph with VBA

  6. #26
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    fumei, thank you! (that you are spending your time to help me)

    1. I did that (now)(Option explicit). Can you tell me what is the reason (for my understanding).
    2. Thx for the explanation
    3. I like that but I didn't know how to do it (can you point me the idea, a bit explicit)
    4. You are correct. I will do more tabs but this one (whit the combobox I didn't know to solve it).
    5. The reason is that is necessary to be sure that type of insulation that I am choosing it is the right one (the checkbox make me more attention).
    6. Good to know, that is better.
    7. I will correct in the future "Case . Value"

    the Call FillBM is a nice function. Because some time the bookmark was erase by code (I think)

    thx for the attachment.
    Another question:
    It is possible to insert an imagine (picture) via bookmarks? I will like to attach in this document also some technical drawings if I will check one checkbox. I search on the forum, but most issue are related with: insert an picture in an user form.

    one more time: thank you for your time

    have a nice day. Here it is night (so I am going to sleep)

  7. #27
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    I just saw now that are more pages in this thread.
    I will read it tomorrow ..Today was a long day. so, lovely people thank you for your time and for the help.

    ps1. I think I will lost in those codes
    ps2. Correct (the Subject for this thread is far away from the first post)

  8. #28
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Gerry .. you said you were busy!

    White_flag .. sleep well! Where in the world are you? Your early attachments were in Dutch but I don't think your time zone is European.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  9. #29
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    Good morning TonyJollans
    I am in Flanders (Belgium) were for the moment, it is fogy and gray.

  10. #30
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    It is also foggy here (in the UK) so now we can't see the snow
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  11. #31
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    here, the snow from yesterday is gone. But at least, we had some snow in this winter (an that was so cool).
    so ..can you help me (again)? I will like to add in the world file via bookmarks an picture (imagine). This is possible?

  12. #32
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    If you just want to add a picture, you can do something like this:

    [VBA]ActiveDocument.Bookmarks("Insulation2").Range.InlineShapes.AddPicture _
    "C:\Users\White_Flag\Pictures\Some Kladding.jpg" ' or whatever[/VBA]
    which doesn't even destroy the bookmark.

    If you want to add mixtures of text and pictures, it is probably worth using Autotexts (or Building Blocks, depending on version).
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  13. #33
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    It is working! thx

    another question:
    I like to add an title before "put technical txt for Rockwool". But the title was to be change in the way the selection will be done.So for every selection it is an title. How can I declare the "titles" to be inserted like it is now ".List(.ListCount - 1, 1) - for insert the text in bookmark but in a different one (Title bookmark)"
     .AddItem "Rockwool":    .List(.ListCount - 1, 2) = "put the Rockwool TXT": Title - this is asking for an function

  14. #34
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    The reason is that: I like to make an text combination between the first combobox and the second one to be put in the title. Like: Rockwool + Aluminium. Then the rest of the data sheets (like it is now). I do not have anyclue how to make it. any help will be more well appreciated.

  15. #35
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I really AM busy. It did not take me that long to do the changes I did.

    white_flag, you need to really spell out EXACTLY your requirements before coding things. As you do not have deep experience I would recommend trying to do simple things first. Adding another variable (this Title) will complicate things a great deal.

    Using the FillBM sub routine (it is not a function) is pretty much standard because, yes, just inserting text into a bookmark does delete the bookmark.

    Option Explicit is useful as a standard because it forces you to declare variables. It also allows full use of IntelliSense, the syntax helper within the VBE.

  16. #36
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Yes, of course you can do it! At a basic level, you just add whatever text you want to the extra bookmark. Determining the text is up to you - I don't know if you can base it on indexes into the combos, or whether it should include text or what. Here is a quick and dirty amendment to your Insulation routine that should get you started:

    [VBA]Private Sub Insulation()
    Dim Title1 As String, Title2 As String
    Title1 = "Title .. ": Title2 = "Title .. "
    With ComboBox_Insulation1
    If .ListIndex >= 0 Then
    Call FillBM("Insulation1", .List(.ListIndex, 1))
    Title1 = Title1 & .Text & " and "
    End If
    End With
    With ComboBox_Insulation2
    If .ListIndex >= 0 Then
    Call FillBM("Insulation2", .List(.ListIndex, 1))
    Title2 = Title2 & .Text & " and "
    End If
    End With
    With ComboBox_Clading1
    If .ListIndex >= 0 Then
    Call FillBM("Clading1", .List(.ListIndex, 1))
    Title1 = Title1 & .Text
    End If
    End With
    With ComboBox_Clading2
    If .ListIndex >= 0 Then
    Call FillBM("Clading2", .List(.ListIndex, 1))
    Title2 = Title2 & .Text
    End If
    End With
    If Right(Title1, 5) = " and " Then Title1 = Left(Title1, Len(Title1) - 5)
    If Right(Title2, 5) = " and " Then Title2 = Left(Title2, Len(Title2) - 5)
    Call FillBM("Title1", Title1)
    Call FillBM("Title2", Title2)

    End Sub[/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  17. #37
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I hope white_flag is realizing that there has to be the additional bookmark already in the document at the appropriate location....

  18. #38
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    this is so awesome.. what can I say:

    fumei and TonyJollans
    thank you sooooooooooooooo much.
    I am so happy.ok, Now, I have to go to make the food )
    I wish you, an lovely day and an lovely evening

  19. #39
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Yes, Gerry, white_flag had already added the bookmarks (which I referenced in the last bit of code I posted).

    White_flag - I'm glad you are sorted. Graag gedaan.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  20. #40
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    hello lovely people,
    I was busy and I can not continued this small project. till now from morning I can not figure out how to do resolve this:

    I want to insert an different language text (this selection will be done via option buton). Wen I am doing one selection (Dutch for example) then everything it is acting like I want (put the txt in Dutch language on bookmarks). But after the first option is done and I wanna change the option to an different option, then I received the text from the first option ().

    I see that the, function has allready an "text" inside (and that one will be inserted what ever I will select, after the first option). Who can this be made to act correct?

    any help will be appreciated

Posting Permissions

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