Consulting

Results 1 to 7 of 7

Thread: Change font size.. etc from excel to word

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

    Change font size.. etc from excel to word

    Hello

    I have an excel file that will create an doc file. Will copy some rows from excel and pasted as tabke in an new document created. this is my code:
    What I can not do it is to change the font.name , and the rest from the font, size, bold etc ...

    code for change the font size, etc ...

        For i = 1 To wdDoc.Tables.Count
        wdDoc.Tables(i).Select
        With Selection
            .Font.Bold = True
            .Font.Italic = False
            .Font.Name = "Arial"
            .Font.Size = "20"
        End With
    Next i
    entire code

    Option Explicit
    
    Sub CopyWorksheetsToWord()
    Dim OFile As String
    Dim filetoopen As String
    Dim WeDone As Long
    Dim tableTemp As Table
    Dim rngTemp As Range
    Dim i As Long
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim ws As Worksheet
    With Application
        .ScreenUpdating = False
        .StatusBar = "Creating new document..."
    End With
    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Add
    'Save for closing
    OFile = ActiveWorkbook.Name
    For Each ws In ActiveWorkbook.Worksheets
        ws.Range("B1").Copy
        wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
        wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste
        ws.Range("A25:K45").Copy
        wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
        wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste
        Application.CutCopyMode = False
        If Not ws.Name = Worksheets(Worksheets.Count).Name Then
            With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range
                .InsertParagraphBefore
                .Collapse Direction:=wdCollapseEnd
                '.InsertBreak Type:=wdPageBreak
            End With
        End If
    Next ws
    wdApp.Visible = True
    For i = 1 To wdDoc.Tables.Count
         wdDoc.Tables(i).Select
         With Selection
             .Font.Bold = True
             .Font.Italic = False
             .Font.Name = "Arial"
             .Font.Size = "20"
         End With
    Next i
    For i = 1 To wdDoc.Tables.Count
         wdDoc.Tables(i).AutoFitBehavior wdAutoFitWindow
    Next i
    For i = 1 To wdDoc.Tables.Count
         wdDoc.Tables(i).ConvertToText Separator:=wdSeparateByTabs, NestedTables:=True
         On Error Resume Next
    Next i
    If ActiveWorkbook.Name <> OFile Then
         ActiveWorkbook.Close False
    End If
    ' apply normal view
    With wdApp.ActiveWindow
         If .View.SplitSpecial = wdPaneNone Then
             .ActivePane.View.Type = wdPrintView
         Else
             .View.Type = wdPrintView
         End If
    End With
    wdApp.Visible = True
    Set wdDoc = Nothing
    Set wdApp = Nothing
    Set tableTemp = Nothing
    Set rngTemp = Nothing
    Set ws = Nothing
    'Reset
    With Application
         .StatusBar = False
         .ScreenUpdating = True
    End With
    End Sub
    any ideea?

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I don't see where you added the tables. See if the methods in this thread helps. http://vbaexpress.com/forum/showthread.php?p=168731

  3. #3
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    hello Kenneth

    when it paste data from xecel with will be paste as table ?
    For Each ws In ActiveWorkbook.Worksheets 
        ws.Range("B1").Copy 
        wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter 
        wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste 
        ws.Range("A25:K45").Copy 
        wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter 
        wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste 
        Application.CutCopyMode = False 
        If Not ws.Name = Worksheets(Worksheets.Count).Name Then 
            With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range 
                .InsertParagraphBefore 
                .Collapse Direction:=wdCollapseEnd 
                '.InsertBreak Type:=wdPageBreak
            End With 
        End If 
    Next ws
    then select the table an apply the changes


    For i = 1 To wdDoc.Tables.Count 
        wdDoc.Tables(i).Select 
        With Selection 
            .Font.Bold = True 
            .Font.Italic = False 
            .Font.Name = "Arial" 
            .Font.Size = "20" 
        End With 
    Next i
    if I look in word
    the table is selected but the selection dasn't change:

    With Selection 
        .Font.Bold = True 
        .Font.Italic = False 
        .Font.Name = "Arial" 
        .Font.Size = "20" 
    End With

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    It shows tables added but it does not look good to me. Maybe you can post an example workbook and what the MSWord file would look like. When you paste, you get alot of formatting codes that you may not want in the MSWord file. I suspect that you can get your formatting setup the way you want if you just insert the values into table cells. More tinkering is needed to be sure.

  5. #5
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    please look in attachment
    Attached Files Attached Files

  6. #6
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location
    wdDoc.Tables(1).Cell(1, 1).Range.Font.Size = 14
    Change the table cell font individually instead of the whole table seems to work. Perhaps, for each cell in table loop to set font? HTH. Dave

  7. #7
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
        For i = 1 To wdDoc.Tables.Count
        wdDoc.Tables(i).Select
        wdDoc.Tables(i).AutoFitBehavior wdAutoFitWindow
        With wdDoc.Tables(i).Range
            .Font.Bold = False
            .Font.Italic = False
            .Font.Name = "Trebuchet MS"
            .Font.Size = "9"
        End With
        Next i

Posting Permissions

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