Setting a style to a word document created from Excel VBA
Results 1 to 3 of 3

Thread: Setting a style to a word document created from Excel VBA
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Mar 2006
    Posts
    447
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Setting a style to a word document created from Excel VBA

    I have some code I've used in a few projects that creates a new word document (from excel vba) and pastes in a string. I remove the last character (a ") and set the font and font size. What new in this part of the code is I want to set the style to No Space. I recorded the vba from word and paste3d in the lines, but I get an error on the Style line... Any ideas on what I'm doing wrong.

    Code:
    Sub CreateWordDoc()
      Dim wdapp As Word.Application
    
      Set wdapp = New Word.Application 'Start word
      With wdapp
        .Visible = True  'See Word on Task bar
        .Activate 'Bring word to the front
        .Documents.Add
      End With
      '===========Copy text from cell====================================
       Range("o12").Copy
    
      '===================================================================
        With wdapp
            .Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:=wdInLine, DisplayAsIcon:=False
            .Selection.TypeBackspace
            .Selection.HomeKey Unit:=wdStory
            .Selection.Delete Unit:=wdCharacter, Count:=1
            .Selection.WholeStory
            .Selection.Style = ActiveDocument.Styles("No Spacing")
            .Selection.Font.Name = "Arial"
            .Selection.Font.Size = 12
            .Selection.Collapse
        End With
    End Sub
    Thanks for any ideas
    Mark

  2. #2
    Board Regular
    Join Date
    Mar 2006
    Posts
    447
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Setting a style to a word document created from Excel VBA

    I'm still doing some poking around and discovered "Documents.count" and its returning a zero value. Why is that, I have an open document?

  3. #3
    Board Regular
    Join Date
    Mar 2006
    Posts
    447
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Setting a style to a word document created from Excel VBA

    I think I got it.... This seems to work to change the Style of the word document and not leave a zombie process, which I think was also getting in the way.

    Code:
    Sub CreateWordRpt()
        Dim wdapp As Word.Application
    On Error GoTo ErrHandler
        Set wdapp = New Word.Application 'Start word
        With wdapp
              .Visible = True  'See Word on Task bar
              .Activate 'Bring word to the front
              .Documents.Add
        End With
    '===============copy information==================================
         Range("O12").Copy   'copy excel range
    '==================================================================
        With wdapp
             .Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:=wdInLine, DisplayAsIcon:=False
             .Selection.TypeBackspace
             .Selection.HomeKey Unit:=wdStory
             .Selection.Delete Unit:=wdCharacter, Count:=1
             .Selection.WholeStory  'select all informaiton
             .Selection.Style = ("No Spacing")  'change style
             .Selection.WholeStory  'Selection all informaiton
             .Selection.Range.Font.Name = "Arial"
             .Selection.Range.Font.Size = 11
        End With
        Set wdapp = Nothing
        Exit Sub
    ErrHandler:
        Set wdapp = Nothing
     End Sub
    Last edited by mark hansen; Aug 23rd, 2019 at 12:35 PM.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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