View Single Post
 
Old 06-18-2015, 12:28 AM
reesjordan reesjordan is offline Windows 7 32bit Office 2007
Novice
 
Join Date: Jun 2015
Posts: 3
reesjordan is on a distinguished road
Default

Graham,

Here is the code I have right now. Not very elegant but it works. How could I make it better?

Code:
Sub Update_Wdp()

  Dim srcDoc As Document, tgtDoc As Document, Ddp As Document, Wdp As Document
  Dim srcTable As Table, tgtTable As Table, srcDate As Table, tgtDate As Table
  Dim i As Integer, J As Integer, strTime As String, strDesc As String
  

'Check to see if two documents are open, Ddp and Wdp
MsgBox ("Must have Ddp & Wdp open!" & vbNewLine & vbNewLine & "Ensure Ddp is selected, not Wdp!")
   If Documents.Count <> 2 Then
      MsgBox ("Must have Ddp & Wdp open!")
   End If
   
'Set word document #1 to Ddp and word document #2 to Wdp
Set Ddp = activeDocument

   If Ddp = Documents(1) Then
      Set Wdp = Documents(2)
   Else
      Set Wdp = Documents(1)
   End If

'Set Ddp as source and Wdp target
Ddp.Activate
  Set srcDoc = Ddp
  Set tgtDoc = Wdp
  Set srcTable = srcDoc.Tables(3)
  Set tgtTable = tgtDoc.Tables(3)
  Set srcDate = srcDoc.Tables(1)
  Set tgtDate = tgtDoc.Tables(1)
  
'Set Date and Clear contents in Wdp for Table 3 - Project Log and format
Wdp.Activate
Application.ScreenUpdating = False
  'Set Date
  tgtDate.Cell(1, 4).Range.contentControls(1).Range.Text = srcDate.Cell(2, 4).Range.Text
  'Clear contents in Wdp for Table 3 - Project Log and format
  For i = 2 To tgtTable.Rows.Count
    tgtTable.Cell(i, 1).Range.contentControls(1).Range.Text = "HH:MM"
    tgtTable.Cell(i, 1).Range.Font.Color = -603937025
    tgtTable.Cell(i, 2).Range.contentControls(1).Range.Text = "HH:MM"
    tgtTable.Cell(i, 2).Range.Font.Color = -603937025
    tgtTable.Cell(i, 5).Range.contentControls(1).Range.Text = "...."
    tgtTable.Cell(i, 5).Range.Font.Color = -603937025
  Next
    
'Update contents in Wdp (Table 3 - Project Log) based on Ddp (Table 3 - Time and Details of Activities)
  For i = 2 To srcTable.Rows.Count
    strTime = srcTable.Cell(i, 1).Range.Text
    strTime = Left(strTime, Len(strTime) - 2)
    strDesc = srcTable.Cell(i, 2).Range.Text
    strDesc = Left(strDesc, Len(strDesc) - 2)
    tgtTable.Cell(i, 1).Range.contentControls(1).Range.Text = strTime
    tgtTable.Cell(i, 5).Range.contentControls(1).Range.Text = strDesc
  Next
'Update contents in Wdp (Table 3 - Project Log) column 2
  For J = 3 To srcTable.Rows.Count
    strTime = srcTable.Cell(J, 1).Range.Text
    strTime = Left(strTime, Len(strTime) - 2)
    tgtTable.Cell((J - 1), 2).Range.contentControls(1).Range.Text = strTime
  Next J
'Update last time of the day 23:59 in Wdp (Table 3 - Project Log) column 2
  For i = 2 To tgtTable.Rows.Count
    If tgtTable.Cell(i, 1).Range.contentControls(1).Range.Text = "23:59" Then
      tgtTable.Cell(i, 2).Range.contentControls(1).Range.Text = "23:59"
    End If
  Next

Application.ScreenUpdating = True


MsgBox ("NOTE: Content Controls don't always accept the:" & vbNewLine & vbNewLine & Space(30) & "ENTER/Return key" & vbNewLine & vbNewLine & "Ensure all your text in the DdP is in ONE paragraph.")

End Sub
Reply With Quote