Consulting

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

Thread: Solved: justify paragraph via VBA

  1. #1
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location

    Solved: justify paragraph via VBA

    Hello
    I have the following code (see code). My question is: Why the
        oPara1.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
    dasn't work.It is ignored and the text is all the time Align to the left (Default).
    Thank you
    Private Sub CommandButton1_Click()
    
    
        Dim oPara1 As Word.Paragraph
    
        'Start Word and open the document template.
        Set oWord = CreateObject("Word.Application")
        oWord.Visible = True
        Set oDoc = oWord.Documents.Add
        
        'Insert a paragraph at the beginning of the document.
        
        Set oPara1 = oDoc.Content.Paragraphs.Add
        oPara1.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
        oPara1.Range.InsertParagraphAfter
        oPara1.Range.Font.Name = "Trebuchet MS"
        oPara1.Range.Font.Size = "10"
        oPara1.Range.Font.Bold = False
        'oPara1.Format.SpaceAfter = 5   '5 pt spacing after paragraph.
        oPara1.Range.Text = "INSERT TEXT that has to be justify ..................."
        
        
    End Sub

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    It appears you are starting an incidence of Word from another program.

    [vba] Set oWord = CreateObject("Word.Application")[/vba]
    Perhaps the problem is Word's predefined properties (the wd* properties).

    So change..[vba] oPara1.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify[/vba] to [vba] oPara1.Range.ParagraphFormat.Alignment = 3[/vba]
    Last edited by Tinbendr; 01-05-2010 at 06:08 AM.

    David


  3. #3
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    Tinbendr, thx for reply. it is VBA for Word (ALT+F11)
    your code is acting correct but was to be put after the "INSERT TEXT"
    and also the
    oPara1.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
    is working, but has to be put after the oPara1.Range.Text
    Now the code it is going like I want.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "Tinbendr, thx for reply. it is VBA for Word (ALT+F11)"

    What does that mean?

    Surely (I hope) it does not mean you are executing the code from Word. Because Tinbendr is correct. Normally:[vba]
    Set oWord = CreateObject("Word.Application")
    [/vba]means you are executing the VBA from some other application (Excel, PowerPoint, Access...). If you are actually in Word, there is never a need to make an instance of Word.

    I have some suggestions.

    1. Use With statements. That means:[vba]
    oPara1.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
    oPara1.Range.InsertParagraphAfter
    oPara1.Range.Font.Name = "Trebuchet MS"
    oPara1.Range.Font.Size = "10"
    oPara1.Range.Font.Bold = False
    'oPara1.Format.SpaceAfter = 5 '5 pt spacing after paragraph.
    [/vba] would be written as:
    [vba]
    With oPara1.Range
    .ParagraphFormat.Alignment = wdAlignParagraphJustify
    .InsertParagraphAfter
    With .Font
    .Name = "Trebuchet MS"
    .Size = "10"
    .Bold = False
    End With
    End With
    [/vba]

    2. Use Styles rather than manually setting properties like Font.Size, or Bold...whatever. If all of those characteristics are in the Style (say named "Whatever"), then you could:[vba]

    Set oDoc = oWord.Documents.Add
    oDoc.Paragraphs(1).Range.Style = "Whatever"

    [/vba]Done.

    3. Technically this comment:[vba]
    'Insert a paragraph at the beginning of the document.
    [/vba]should not be needed, as ALL Word documents have a paragraph to start with. It is not possible to have a Word document with zero paragraphs. They all have at least one.

  5. #5
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    @fumei .. thx for the correction, you are correct.But my experience is zero, compare with you guys. I will putted in the correct VBA sentence.
    I just need to made an automation form + bookmarks in word. But for the moment it is ok, the script.

    Antoniu

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I am not actually trying to "correct". I am trying to help. Could you please answer the question though...what application are you running this VBA code from?

  7. #7
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    Application will be (it is) MS Word. I am doing this, wen I have some free time (to gain more free time :P)..

    ...so thx for the help
    ps. the start example was take it from Microsoft (Vba for Word)

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "Application will be (it is) MS Word. "

    If that is so, then there is no need to create another instance of Word. In other words, you do NOT need to do:[vba]

    'Start Word and open the document template.
    Set oWord = CreateObject("Word.Application")

    [/vba]You do not need to "start" Word, as it is already started. The above code makes a NEW instance4 of the Word application itself, which - if you are running the code from Word, is both not required, and not a good idea.

  9. #9
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    I understand
    now the code is like this:

    Private Sub CommandButton1_Click()
    
    
        Dim oPara1 As Word.Paragraph
    
        'Start Word and open the document template.
        Set oWord = CreateObject("Word.Application")
        oWord.Visible = True
        Set oDoc = oWord.Documents.Add
        
        'Insert an text
    
        Set oPara1 = oDoc.Content.Paragraphs.Add
            With oPara1.Range
                .InsertParagraphAfter
                .Text = "Text to be inserted"
                .ParagraphFormat.Alignment = wdAlignParagraphJustify
                With .Font
                    .Name = "Trebuchet MS"
                    .Size = "10"
                    .Bold = True
                End With
              End With
    
    
    End Sub
    how will be look better from your point of view? Yes the world is allready open and it is not necessary to be reopen. But I do not know how to do it.

  10. #10
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    double post

  11. #11
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    I understand
    Clearly you don't.

    If you are running from Word to begin with, you can start your code like this ...

    [VBA]Private Sub CommandButton1_Click()

    Dim oPara1 As Word.Paragraph

    Set oDoc = Documents.Add

    'Insert an text[/VBA]

    What the point of the rest of it is, I do not know. It can surely be better done, but how depends on your ultimate aim.
    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

  12. #12
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    That "understand" was: I understand that is not good what I am doing and I have to change it (because is better).

    your code, create an new document (it is possible to be done this text in the actual world file):

    Private Sub CommandButton1_Click()
    
    
        Dim oPara1 As Word.Paragraph
     
        Set oDoc = Documents.Add
    
        Set oPara1 = oDoc.Content.Paragraphs.Add
            With oPara1.Range
                .InsertParagraphAfter
                .Text = "This an text"
                .ParagraphFormat.Alignment = wdAlignParagraphJustify
                With .Font
                    .Name = "Trebuchet MS"
                    .Size = "10"
                    .Bold = True
                End With
              End With
    
    
    End Sub

  13. #13
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    I appreciate that English is not your first language, but I am not understanding what question you have.

    The code you have just posted works but it doesn't do anything useful. What are you trying to do?
    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

  14. #14
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    ok thx for understanding..I put in attachment what I try to do (like this is better to understand).

    This is an document with bookmarks. In those bookmarks will be fill in, some text via an user form. and some standard text via drop list validated by an checkbottom.

  15. #15
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    And what, if anything, now, isn't working for you?
    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

  16. #16
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I agree. The attached document has no userform, therefore no [vba]
    Sub CommandButton1_Click()
    [/vba]

    There is no code at all.

    Regarding the use CreatObject, I will reiterate.

    If you are in Word (and you are), then you do NOT need to create a Word application object. You can use the one you are already using.

    I would strongly recommend you use Styles. I am sure it would help a great deal. Having existing paragraphs starting with a Tab (in your header) is not good.

  17. #17
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    I made an mistake (wen I attach the file) and wen I was at home, I didn't have the file with me. Please look in attachment and tell me yours point of view.

    thx

  18. #18
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    I have the following error
    Private Sub Insulation()
    With ComboBox_Insulation1
        Select Case .Value
            Case Is = "Rockwool"
            Range.Text = "txt Rockwool"
            Else
            Range.Text = "JUST TEXT"
        End Select
    End With
    
    With ComboBox_Insulation2
        Select Case .Value
            Case Is = "Rockwool"
            Range.Text = "txt Rockwool"
            Else
            Range.Text = "JUST TEXT"
        End Select
    End With
        
    End Sub
    Private Sub CommandButton1_Click()
    
    If CheckBox_Insulation1 = True Then
        ActiveDocument.Bookmarks("Insulation1").Range.Text = Insulation
        
    End If
       
        Application.ScreenUpdating = False
        Unload Me
    
    End Sub
    
    Private Sub UserForm_Initialize()
        With ComboBox_Insulation1
            .AddItem "Rockwool"
            .AddItem "Rockwool DK"
            .AddItem "Armaflex"
            .AddItem "Armaflex HT"
            .AddItem "PIR"
            .AddItem "PUR"
            .AddItem "PUR SCH"
            .AddItem "JitraBand"
            .AddItem "Sound"
        End With
        With ComboBox_Insulation2
            .AddItem "Rockwool"
            .AddItem "Rockwool DK"
            .AddItem "Armaflex"
            .AddItem "Armaflex HT"
            .AddItem "PIR"
            .AddItem "PUR"
            .AddItem "PUR SCH"
            .AddItem "JitraBand"
            .AddItem "Sound"
        End With
    End Sub
    and there
        ActiveDocument.Bookmarks("Insulation1").Range.Text = Insulation
    tell me that "Expected Function or variable". what I did wrong?

  19. #19
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    update. Please tell me if the construction of this VBA code is correct (I mean in the healthy VBA code). Thank you

  20. #20
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    So many things...

    1. I strongly suggest you use Option Explicit in your code modules. In the VBE, go Tools > Options and under the Editor tab, check "Require Variable Declaration".

    2. The reason you had the ealier error was indeed the line:[vba]
    ActiveDocument.Bookmarks("Insulation1").Range.Text = Insulation
    [/vba]VBA is expecting a string (or a string variable), but the code is not a string. You fixed that with the later version of (for example):[vba]
    ActiveDocument.Bookmarks("Insulation1").Range.Text = "put PIR TXT"
    [/vba]

    3. However, using the above code, you will notice that "put PIR TXT" is placed after the bookmark. In other words:

    put the Rockwool TXT (after the bookmark Insulation1)

    becomes:

    put PIR TXTput the Rockwool TXT

    Do you want to replace "put the Rockwool TXT" with "put PIR TXT"?

    4. Why do you have a MultiPage with only one tab ("General")? Are you going to end with more tabs?

    5. Why do you have both the combobox to select an item, and a checkbox?

    Small points.

    6. It is normally a good thing to have a combobox have an item displayed, rather than blank. To do this, use ListIndex.[vba]
    With ComboBox_Insulation1
    .AddItem "Rockwool"
    .AddItem "Rockwool DK"
    .AddItem "Armaflex"
    .AddItem "Armaflex HT"
    .AddItem "PIR"
    .AddItem "PUR"
    .AddItem "PUR SCH"
    .AddItem "JitraBand"
    .AddItem "Sound"
    .ListIndex = 0
    End With
    [/vba]will display the combox with Rockwool displayed.

    7. You do not need to use Case Is =[vba]
    Case Is = "Rockwool"
    [/vba]This can be:[vba]
    Case "Rockwool"
    [/vba]

    Like this:[vba]
    Option Explicit

    Private Sub Insulation()
    With ComboBox_Insulation1
    Select Case .Value
    Case "Rockwool"
    Call FillBM("Insulation1", "put the Rockwool TXT")
    Case "Rockwool DK"
    Call FillBM("Insulation1", "put the Rockwool DK TXT")
    Case "Armaflex"
    Call FillBM("Insulation1", "put Armaflex DK TXT")
    Case "Armaflex HT"
    Call FillBM("Insulation1", "put Armaflex HT TXT")
    ' etc. etc
    [/vba]

    The Call FillBM passes the bookmark name, and text content to the Sub FillBM. This is in a standard module. This procedure inserts the text into the bookmark, not after it. Although technically, it actually inserts the text 9which deletes the bookmark) and then re-creates the bookmark.

    Amended doc attached. Click "Show The Form" on the top toolbar.

Posting Permissions

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