• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Applying Word styles to selected text

Shaun

Member
Hi all

I am still trying to create a report in Word 2010 from data out of Excel 2010.

As part of report I need to apply styles to text within the report so that the table of contents updates correctly. Despite every effort I have been unable to apply the formatting.

In the following I am trying to apply the style "Heading 1" to the text added at the bookmark.

All the reading and experimenting has led me in circles.

Code:
...
ActiveDocument.Bookmarks.Add ("ClientName1")
ClientName1 = ActiveWorkbook.Sheets("Entity Details").Range("C3")
With objDoc
  If objDoc.Bookmarks.Exists("ClientName1") Then
  .Bookmarks("ClientName1").Range.Text = Clientname1
  .Selection.Style = ActiveDocument.Styles("Heading1")
  End If
End With
...

Is anyone able to please shed some light? If more information is required, please let me know.

Kind regards

Shaun
 
Hi Shaun ,

If you can upload a workbook with the complete code in it , it will make it easier to execute the code and test it.

As it is , all I can say is that when the following line is executed , what is the Selection ?

.Selection.Style = ActiveDocument.Styles("Heading1")

Narayan
 
Hi Narayan

Please find attached example xlsx and docx files.

To answer your question, visually the text that I would like to apply the formatting is selected. Beyond that I am unsure.

Kind regards

Shaun
 

Attachments

  • Chandoo Example.txt
    12.4 KB · Views: 0
  • Chandoo Example.xlsm
    25.8 KB · Views: 0
Hi Narayan

Thank you so much.

When I amended the line accordingly, I received an error, but when I changed "Heading 1" to -2 it worked perfectly.

Kind regards

Shaun
 
Hi Narayan

When a table is being added to the word document it is deleting everything else in the document. I don't understand what is happening.

Are you able to please take a look?

Kind regards
 

Attachments

  • chandoo example 1.docx.txt
    41.7 KB · Views: 0
  • Chandoo Example 1.xlsm
    35.3 KB · Views: 0
Hi Shaun ,

See the file ; the first table is being added correctly. The new lines of code are :
Code:
        Set wrdRange = objDoc.Range
        With wrdRange
            .Collapse Direction:=wdCollapseEnd
            .InsertParagraphAfter
            .Collapse Direction:=wdCollapseEnd
        End With
       
    'Create table
        Set wrdTable = objDoc.Tables.Add(Range:=wrdRange, NumRows:=1, NumColumns:=intNoOfColumns)
Can you modify the second section on the same lines ?

Narayan
 

Attachments

  • Chandoo Example 1.xlsm
    33.2 KB · Views: 0
Hi Narayan

Thank you very much for your help. Your code worked perfectly!

I don't understand why my code was behaving the way it was, or more importantly why yours works correctly.

Cheers
 
Hi Narayan

I have spent the last few days trying to get bullet points to work.

I am not sure what I am doing wrong, would you kindly please take a look?

Cheers
 

Attachments

  • Chandoo Example.xlsm
    26.4 KB · Views: 0
  • Chandoo Example.txt
    12.4 KB · Views: 0
Hi All

There is an issue with code in the xlsm (besides the bullet point section) this file corrects that issue.

Hopefully someone may get the opportunity to take a look, I just can't get this to work. Any assistance would be greatly appreciated.

Cheers

Shaun
 

Attachments

  • Chandoo Example.xlsm
    26.4 KB · Views: 0
hmmm...possibly some progress.

Am I correct in thinking bullet points are really just a type of styling and if I create a bullet "style" then I can apply that style as I have done here (from Chandoo Example.xlsm):

Code:
If objDoc.Bookmarks.Exists("ClientName1") Then
    .Bookmarks("ClientName1").Range.Text = Clientname1
    objWord.Selection.Style = -2
End If

It seems to work, but is there anything I should be aware of applying bullet points this way?

Cheers

Shaun
 
Back
Top