Hello everyone,
I have two word documents, one document used content control (Test_www.docx) and the other document (Test daily.docx) has a table.
I have to physically copy each cell from one table into the content controlled document. And it is a utter pain. I want to copy the table from Test daily.docx into the content control document Test_www.docx.
I want to copy the following items from Test daily.docx -> Test_www.docx
time ———————————————————> From To
Details of Events/Activities ———————————> Description.
I want this to continue until the end of the day at 23:59. If I have a very busy day this is very painful.
Time Details of Events/Activities
00:00 Some text
05:46 Some text
From To Activity Type Code Description
00:00 05:46 Choose activity type SUR (text) Some text
05:46 06:00 Choose activity type SUR (text) Some text
………………………………………………
23:59 23:59 Choose activity type SUR (text) Some text
Below are the two sample files I am working with:
41045-Test-www
41046-Test-daily
I made several attempts at this but I cannot wrap my head around VBA in word. Hopefully I have enough scraps of code for someone to help me out. I would be grateful for any working macro.
Attempt #1 have two documents open, copy from one file to another. I use, find then extend mode feature in word to select the right items.
Sub CopyFromSecondDoc() ‘ ‘ CopyFromSecondDoc Macro ‘ from http://windowssecrets.com/forums/showthread//135517-Macro-to-copy-from-one-document-to-another ‘ Dim ThisDoc As Document Dim OtherDoc As Document If Documents.Count 2 Then MsgBox “Must only have two documents open!” Exit Sub End If Set ThisDoc = ActiveDocument If ThisDoc = Documents(1) Then Set OtherDoc = Documents(2) Else Set OtherDoc = Documents(1) End If OtherDoc.Activate ‘ ‘ ‘I’m trying to use the extendMode to select and copy all the times in Test daily.docx from 0000_2359 ‘ ‘ Selection.Find.ClearFormatting With Selection.Find .Text = “Time” .Replacement.Text = “” .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Selection.MoveDown Unit:=wdLine, Count:=1 Selection.Extend Selection.Find.ClearFormatting With Selection.Find .Text = “23:59” .Replacement.Text = “” .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Selection.Copy ‘ I’m trying to use the extendMode to select all the From rows in Test www.docx from 0000_2359 ‘ ThisDoc.Activate Selection.Find.ClearFormatting With Selection.Find .Text = “Project Operational Log” .Replacement.Text = “” .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Selection.MoveDown Unit:=wdLine, Count:=1 Selection.HomeKey Unit:=wdLine Selection.MoveLeft Unit:=wdCharacter, Count:=1 Selection.HomeKey Unit:=wdLine Selection.MoveLeft Unit:=wdCharacter, Count:=1 Selection.HomeKey Unit:=wdLine Selection.MoveLeft Unit:=wdCharacter, Count:=1 Selection.HomeKey Unit:=wdLine Selection.MoveDown Unit:=wdLine, Count:=1 Selection.Extend Selection.MoveDown Unit:=wdLine, Count:=39 On Error Resume Next ‘Selection.PasteAndFormat (wdPasteDefault) ‘Selection.MoveDown Unit:=wdCell OtherDoc.Activate Selection.Copy ThisDoc.Activate Selection.PasteAndFormat (wdPasteDefault) Set ThisDoc = Nothing Set OtherDoc = Nothing End Sub
Attempt #2, this may be easier to implement. I tied to declare and assign the ContentControl items values in Test http://www.docx to the appropriate values in Test daily.docx
Sub demo2() Dim projectlog1cc1 As ContentControl Dim projectlog2cc2 As ContentControl Dim projectlog3cc3 As ContentControl Dim projectlog4cc4 As ContentControl Dim projectlog5cc5 As ContentControl Dim projectlog6cc6 As ContentControl Dim projectlog7cc7 As ContentControl Dim projectlog8cc8 As ContentControl Dim projectlog9cc9 As ContentControl Dim projectlog10cc10 As ContentControl Dim projectlog11cc11 As ContentControl Dim projectlog12cc12 As ContentControl Dim projectlog13cc13 As ContentControl Dim projectlog14cc14 As ContentControl Dim projectlog15cc15 As ContentControl Dim projectlog16cc16 As ContentControl Dim projectlog17cc17 As ContentControl Dim projectlog18cc18 As ContentControl Dim projectlog19cc19 As ContentControl Dim projectlog20cc20 As ContentControl Dim projectlog21cc21 As ContentControl Dim projectlog22cc22 As ContentControl Dim projectlog23cc23 As ContentControl Dim projectlog24cc24 As ContentControl Dim projectlog25cc25 As ContentControl Dim projectlog26cc26 As ContentControl Dim projectlog27cc27 As ContentControl Dim projectlog28cc28 As ContentControl Dim projectlog29cc29 As ContentControl Dim projectlog30cc30 As ContentControl Dim projectlog31cc31 As ContentControl Dim projectlog32cc32 As ContentControl Dim projectlog33cc33 As ContentControl Dim projectlog34cc34 As ContentControl Dim projectlog35cc35 As ContentControl Dim projectlog36cc36 As ContentControl Dim projectlog37cc37 As ContentControl Dim projectlog38cc38 As ContentControl Dim projectlog39cc39 As ContentControl Dim projectlog40cc40 As ContentControl ‘Dim projectlog41cc41 As ContentControl Set projectlog1cc1 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (1)”).Item(1) Set projectlog2cc2 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (2)”).Item(1) Set projectlog3cc3 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (3)”).Item(1) Set projectlog4cc4 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (4)”).Item(1) Set projectlog5cc5 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (5)”).Item(1) Set projectlog6cc6 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (6)”).Item(1) Set projectlog7cc7 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (7)”).Item(1) Set projectlog8cc8 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (8)”).Item(1) Set projectlog9cc9 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (9)”).Item(1) Set projectlog10cc10 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (10)”).Item(1) Set projectlog11cc11 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (11)”).Item(1) Set projectlog12cc12 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (12)”).Item(1) Set projectlog13cc13 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (13)”).Item(1) Set projectlog14cc14 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (14)”).Item(1) Set projectlog15cc15 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (15)”).Item(1) Set projectlog16cc16 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (16)”).Item(1) Set projectlog17cc17 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (17)”).Item(1) Set projectlog18cc18 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (18)”).Item(1) Set projectlog19cc19 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (19)”).Item(1) Set projectlog20cc20 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (20)”).Item(1) Set projectlog21cc21 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (21)”).Item(1) Set projectlog22cc22 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (22)”).Item(1) Set projectlog23cc23 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (23)”).Item(1) Set projectlog24cc24 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (24)”).Item(1) Set projectlog25cc25 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (25)”).Item(1) Set projectlog26cc26 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (26)”).Item(1) Set projectlog27cc27 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (27)”).Item(1) Set projectlog28cc28 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (28)”).Item(1) Set projectlog29cc29 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (29)”).Item(1) Set projectlog30cc30 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (30)”).Item(1) Set projectlog31cc31 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (31)”).Item(1) Set projectlog32cc32 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (32)”).Item(1) Set projectlog33cc33 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (33)”).Item(1) Set projectlog34cc34 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (34)”).Item(1) Set projectlog35cc35 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (35)”).Item(1) Set projectlog36cc36 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (36)”).Item(1) Set projectlog37cc37 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (37)”).Item(1) Set projectlog38cc38 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (38)”).Item(1) Set projectlog39cc39 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (39)”).Item(1) Set projectlog40cc40 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (40)”).Item(1) ‘Set projectlog41cc41 = ActiveDocument.SelectContentControlsByTitle(“Project Operational Log – Description (41)”).Item(1) ‘Here i’m not sure how to assign values to the content control items from the table in Test daily.docx projectlog1cc1.Range.Text = “teee” End Sub
Any help would be graciously appreciated.:confused: