Creating a Word Document from an Excel spredsheet - formatting

Blanchetdb

Board Regular
Joined
Jul 31, 2018
Messages
63
Hi,

I presently have coding that will create the word document but I was wondering if it is possible to have coding that will help with the formatting of the Word document and the removal of bookmarks that are no used (no information in the corresponding cell in excel)

the person fills information in specific cells in excel and the information is then moved to a word document at specific locations identified by bookmarks....

all cells in excel are not mandatory to be filled with information so that create gaps in the word document (unused bookmarks). I there a way to eliminate those gaps?

Code:
Private Sub CommandButton1_Click()


    Dim wdApp As Word.Application
    Dim theString As String
    Dim theObject As Object
    Set wdApp = New Word.Application
    Set theObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    
    With wdApp
        .Visible = True
        .Activate
        
        .Documents.Add "M:\HR\National Transfer Inventory\Indeterminate_Appointment_Template_.DOCX"
        
        Sheet3.Activate
        
        theString = Range("C5").Value         'cast to string
        theObject.SetText theString
        theObject.PutInClipboard    'copy from cell B2 to clipboard.
        .Selection.Goto what:=-1, Name:="Incumbent"
        .Selection.Paste        'paste from the clipboard to the Word Doc.

' several other lines of coding the same as above....then

    
    If Range("A23") = "x" Then
    theString = Worksheets("Sheet4").Cells(1, "A").Value        'cast to string
        theObject.SetText theString
        theObject.PutInClipboard    'copy from cell B2 to clipboard.
        With .Selection
        .Goto what:=-1, Name:="ManTraining"
        .BoldRun
        .Font.Size = 12
        .Font.Name = "Arial"
        .TypeText "MANDATORY TRAINING"
        .BoldRun
        .TypeParagraph
        .Font.Name = "Arial"
        .Font.Size = 12
        .Paste        'paste from the clipboard to the Word Doc.
        .TypeParagraph
        End With
    End If
    If Range("A24") = "x" Then
    theString = Worksheets("Sheet4").Cells(2, "A").Value        'cast to string
        theObject.SetText theString
        theObject.PutInClipboard    'copy from cell B2 to clipboard.
        With .Selection
        .Goto what:=-1, Name:="ManTraining"
        .BoldRun
        .Font.Size = 12
        .Font.Name = "Arial"
        .TypeText "MANDATORY TRAINING"
        .BoldRun
        .TypeParagraph
        .Font.Name = "Arial"
        .Font.Size = 12
        .Paste        'paste from the clipboard to the Word Doc.
        .TypeParagraph
        End With
    End If
    
    ' Hours of Work
    
    If Range("A27") = "x" Then
    theString = Worksheets("Sheet4").Cells(3, "A").Value        'cast to string
        theObject.SetText theString
        theObject.PutInClipboard    'copy from cell B2 to clipboard.
        With .Selection
        .Goto what:=-1, Name:="HoursWork"
        .BoldRun
        .Font.Size = 12
        .Font.Name = "Arial"
        .TypeText "HOURS OF WORK"
        .BoldRun
        .TypeParagraph
        .Font.Name = "Arial"
        .Font.Size = 12
        .Paste        'paste from the clipboard to the Word Doc.
        .TypeParagraph
        End With
    End If
    If Range("A28") = "x" Then
    theString = Worksheets("Sheet4").Cells(4, "A").Value        'cast to string
        theObject.SetText theString
        theObject.PutInClipboard    'copy from cell B2 to clipboard.
        With .Selection
        .Goto what:=-1, Name:="HoursWork"
        .BoldRun
        .Font.Size = 12
        .Font.Name = "Arial"
        .TypeText "HOURS OF WORK"
        .BoldRun
        .TypeParagraph
        .Font.Name = "Arial"
        .Font.Size = 12
        .Paste        'paste from the clipboard to the Word Doc.
        .TypeParagraph
        End With
    End If
    If Range("A29") = "x" Then
    theString = Worksheets("Sheet4").Cells(5, "A").Value        'cast to string
        theObject.SetText theString
        theObject.PutInClipboard    'copy from cell B2 to clipboard.
        With .Selection
        .Goto what:=-1, Name:="HoursWork"
        .BoldRun
        .Font.Size = 12
        .Font.Name = "Arial"
        .TypeText "HOURS OF WORK"
        .BoldRun
        .TypeParagraph
        .Font.Name = "Arial"
        .Font.Size = 12
        .Paste        'paste from the clipboard to the Word Doc.
        .TypeParagraph
        End With
    End If
    If Range("A30") = "x" Then
    theString = Worksheets("Sheet4").Cells(6, "A").Value        'cast to string
        theObject.SetText theString
        theObject.PutInClipboard    'copy from cell B2 to clipboard.
        With .Selection
        .Goto what:=-1, Name:="HoursWork"
        .BoldRun
        .Font.Size = 12
        .Font.Name = "Arial"
        .TypeText "HOURS OF WORK"
        .BoldRun
        .TypeParagraph
        .Font.Name = "Arial"
        .Font.Size = 12
        .Paste        'paste from the clipboard to the Word Doc.
        .TypeParagraph
        End With
    End If
    If Range("A31") = "x" Then
    theString = Worksheets("Sheet4").Cells(7, "A").Value        'cast to string
        theObject.SetText theString
        theObject.PutInClipboard    'copy from cell B2 to clipboard.
        With .Selection
        .Goto what:=-1, Name:="HoursWork"
        .BoldRun
        .Font.Size = 12
        .Font.Name = "Arial"
        .TypeText "HOURS OF WORK"
        .BoldRun
        .TypeParagraph
        .Font.Name = "Arial"
        .Font.Size = 12
        .Paste        'paste from the clipboard to the Word Doc.
        .TypeParagraph
        End With
    End If
    If Range("A32") = "x" Then
    theString = Worksheets("Sheet4").Cells(8, "A").Value        'cast to string
        theObject.SetText theString
        theObject.PutInClipboard    'copy from cell B2 to clipboard.
        With .Selection
        .Goto what:=-1, Name:="HoursWork"
        .BoldRun
        .Font.Size = 12
        .Font.Name = "Arial"
        .TypeText "HOURS OF WORK"
        .BoldRun
        .TypeParagraph
        .Font.Name = "Arial"
        .Font.Size = 12
        .Paste        'paste from the clipboard to the Word Doc.
        .TypeParagraph
        End With
    End If
    If Range("A33") = "x" Then
    theString = Worksheets("Sheet4").Cells(9, "A").Value        'cast to string
        theObject.SetText theString
        theObject.PutInClipboard    'copy from cell B2 to clipboard.
        With .Selection
        .Goto what:=-1, Name:="HoursWork"
        .BoldRun
        .Font.Size = 12
        .Font.Name = "Arial"
        .TypeText "HOURS OF WORK"
        .BoldRun
        .TypeParagraph
        .Font.Name = "Arial"
        .Font.Size = 12
        .Paste        'paste from the clipboard to the Word Doc.
        .TypeParagraph
        End With
    End If
End With
   
    Set wdApp = Nothing
    Set theObject = Nothing
        

End Sub
any help would be appreciated

thank you
 

Some videos you may like

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,089,337
Messages
5,407,666
Members
403,158
Latest member
Limerick2030

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top