• WSreesjordan

    WSreesjordan

    @wsreesjordan

    Viewing 3 replies - 1 through 3 (of 3 total)
    Author
    Replies
    • Andrew,

      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
    • Andrew,

      Thanks for you assistance. But I found the error (after hours of trial and error). My source table contained a “Enter/Return” in the paragraph, which caused the file to become corrupt. From what I gathered online, contentcontrol don’t handle the “Enter/Return” key too well.

      Example of my source table:

      Some text, some text.
      Goes here.

      I changed that particular cell to one paragraph:

      Some text, some text. Goes here.

      Now when I save my document and try to reopen it, I no longer receive the xml/corrupt file error.

      Is there a way to ignore or remove “Enter/Return” from cells?

      Jordan

    • Andrew,

      Thanks for the quick reply it’s exactly what I required.

      With a few modifications, I got everything to work on the actual documents. There is one issue.

      When I save the document as a .docx and try to reopen it I get the following error:

      41073-Errors2

      I traced down the error (basically by commenting each line out 1 by 1) to the following line of code:

      Code:
          tgtTable.Cell(i, 5).Range.ContentControls(1).Range.Text = strDesc 

      The description field contains, letters, numbers, symbols (° ‘ “) and so forth so declaring this field as a sting should be good. I tried to edit this line of code and to find a solution online but nothing seems to work.

      Can you tell me what ContentControls(1)
      -Dim strDesc as Variant
      -Changed tgtTable.Cell(i, 5).Range.ContentControls(1).Range.Text to …. .ContentControls(2), .ContentControls(3), .ContentControls(4), .ContentControls(5), .ContentControls(i) etc.
      -Ensure Word was updated

      Do you have any suggestions on how to fix this error?

      Jordan

    Viewing 3 replies - 1 through 3 (of 3 total)