Macro to copy a whole table and then paste it in a new doc

  • Thread starter Thread starter Johnnyboy5
  • Start date Start date
J

Johnnyboy5

Hi

I have a document with some text, fields, field ref and a table in
it, some of the fields & ref populate a table lower down in the
document and then the macro will copy it a paste it into a new
document which can then be saved on its own.

The main document also has a number of other functions / purposes so I
dont really what to change it.

The problem.

If I put too much text in some of the "fields" when I use the Macro
the "line" number reference is wrong.

I need to know how to select the table and copy it ...or any other
ideas

Thanks

John

This is the macro I was using -

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If


Selection.HomeKey Unit:=wdStory
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=120
Selection.MoveLeft Unit:=wdCharacter, Count:=6
Selection.MoveDown Unit:=wdLine, Count:=75, Extend:=wdExtend
Selection.Copy

' reprotect without emptying the fields

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
Noreset:=True

ChangeFileOpenDirectory "f:\1 CASE NOTES\"
Documents.Open FileName:="""2 blank UA sheet.doc""",
ConfirmConversions:= _
False, ReadOnly:=False, AddToRecentFiles:=False,
PasswordDocument:="", _
PasswordTemplate:="", Revert:=False,
WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto,
XMLTransform:=""
Selection.WholeStory
Selection.Paste
Selection.WholeStory
Selection.Font.Size = 10
Selection.WholeStory
Selection.Fields.Unlink
Selection.HomeKey Unit:=wdStory
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
 
Lines are a volatile entity in Word and you cannot use them in this manner
for the reason you have found.

We have already discussed the issue of creating reports from data in a form
and you can find more information about it on my web site at
http://www.gmayor.com/ExtractDataFromForms.htm

Your previous post suggests that you only want the data from the form so
there is no need to copy and paste (or unprotect it). Read the content of
the fields and write it to the new document in the locations that you want
it to appear.

Can I suggest that you investigate the use of ranges - see the section on
ranges in Greg Maxey's vba primer at
http://gregmaxey.mvps.org/VBA_Basics.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Lines are a volatile entity in Word and you cannot use them in this manner
for the reason you have found.

We have already discussed the issue of creating reports from data in a form
and you can find more information about it on my web site athttp://www.gmayor.com/ExtractDataFromForms.htm

Your previous post suggests that you only want the data from the form so
there is no need to copy and paste (or unprotect it). Read the content of
the fields and write it to the new document in the locations that you want
it to appear.

Can I suggest that you investigate the use of ranges - see the section on
ranges in Greg Maxey's vba primer athttp://gregmaxey.mvps.org/VBA_Basics.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
















- Show quoted text -

thanks Graham - We are using about 10 different forms in our team and
I am trying to sort some macros for each of them - as they all have
different uses - (its quite mad but that's the problem working in a
multi disiplinary setting LOL ; )

John
 
Lines are a volatile entity in Word and you cannot use them in this manner
for the reason you have found.

We have already discussed the issue of creating reports from data in a form
and you can find more information about it on my web site athttp://www.gmayor.com/ExtractDataFromForms.htm

Your previous post suggests that you only want the data from the form so
there is no need to copy and paste (or unprotect it). Read the content of
the fields and write it to the new document in the locations that you want
it to appear.

Can I suggest that you investigate the use of ranges - see the section on
ranges in Greg Maxey's vba primer athttp://gregmaxey.mvps.org/VBA_Basics.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
















- Show quoted text -

cant I just copy the completed "table" and paste it to the new "blank"
doc ?

John
 
Try the following:

Dim oDoc As Document, oNewDoc As Document
Dim oTable As Table
Dim oRng As Range
Dim oField As Field
Set oDoc = ActiveDocument
If oDoc.ProtectionType <> wdNoProtection Then
oDoc.Unprotect
End If
Set oNewDoc = Documents.Add
oNewDoc.PageSetup.Orientation = wdOrientLandscape
With oNewDoc.PageSetup
.LeftMargin = CentimetersToPoints(0.75)
.RightMargin = CentimetersToPoints(0.63)
.TopMargin = CentimetersToPoints(0.75)
.BottomMargin = CentimetersToPoints(0.63)
End With
Set oTable = oDoc.Tables(1)
oTable.Range.Copy
oNewDoc.Range.Paste
oDoc.Protect Type:=wdAllowOnlyFormFields, _
Noreset:=True
Set oRng = oNewDoc.Range
oNewDoc.Range.Fields.Unlink
With oRng.Find
.Text = "END OF PAGE ONE"
Do While .Execute(Forward:=True) = True
oRng.Delete
Loop
End With

which will work with the document you mailed to me.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Try the following:

Dim oDoc As Document, oNewDoc As Document
Dim oTable As Table
Dim oRng As Range
Dim oField As Field
Set oDoc = ActiveDocument
If oDoc.ProtectionType <> wdNoProtection Then
    oDoc.Unprotect
End If
Set oNewDoc = Documents.Add
oNewDoc.PageSetup.Orientation = wdOrientLandscape
With oNewDoc.PageSetup
    .LeftMargin = CentimetersToPoints(0.75)
    .RightMargin = CentimetersToPoints(0.63)
    .TopMargin = CentimetersToPoints(0.75)
    .BottomMargin = CentimetersToPoints(0.63)
End With
Set oTable = oDoc.Tables(1)
oTable.Range.Copy
oNewDoc.Range.Paste
oDoc.Protect Type:=wdAllowOnlyFormFields, _
Noreset:=True
Set oRng = oNewDoc.Range
oNewDoc.Range.Fields.Unlink
With oRng.Find
    .Text = "END OF PAGE ONE"
    Do While .Execute(Forward:=True) = True
        oRng.Delete
    Loop
End With

which will work with the document you mailed to me.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>




- Show quoted text -

Yep that did the tick (really well!!!!) thanks again for you
support. Like I said before what ever happened to the paperless
office idea !

Johnnyboy
 
Back
Top