Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 06-18-2015, 12:28 AM
reesjordan reesjordan is offline Error when trying to open .docx after I run a macro to copy data between tables in two documents Windows 7 32bit Error when trying to open .docx after I run a macro to copy data between tables in two documents Office 2007
Novice
Error when trying to open .docx after I run a macro to copy data between tables in two documents
 
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
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error zipit189 Word VBA 7 03-18-2015 01:13 PM
Macro to copy cell info to multiple documents Patrick Innes Word VBA 2 02-18-2015 08:38 PM
Want to change Macro to copy and paste data to new sheet Vortex69 Excel Programming 0 12-23-2014 09:53 PM
Error when trying to open .docx after I run a macro to copy data between tables in two documents Word macro: copy from different tables into one table adisl Word VBA 4 03-25-2014 02:40 AM
Error when trying to open .docx after I run a macro to copy data between tables in two documents Run-time Error 5174: Open Word Documents from Excel tinfanide Excel Programming 3 10-01-2013 07:35 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:09 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft