Creating a Word Document from an Excel spredsheet - formatting

Blanchetdb

Board Regular
Joined
Jul 31, 2018
Messages
161
Office Version
  1. 2016
Platform
  1. Windows
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
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top