Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Coping and pasting into particular documents with macros

    I have found this site to be extreamly helpful and solved almost all my challenges except when trying to copy and past into particular documents with macros

    below is my code and unfortunatly it isnt working as it is getting stuck at "Selection.EndKey Unit:=wdLine"

    ThisDocument.Activate 'start in THIS Document
    'Opens source file (this filename never changes)
    Application.Documents.Open FileName:="S:\ARRANGEMENT FILES\0 Secretarial Masters\0 FILE START-UP\Old Arrangement File\part 2 NEW MASTER - Arrangement File.docx", ReadOnly:=True
    Documents("S:\ARRANGEMENT FILES\0 Secretarial Masters\0 FILE START-UP\Old Arrangement File\part 2 NEW MASTER - Arrangement File.docx").Activate 'switch to source file
    Selection.WholeStory
    Selection.Copy 'Copy data to clipboard
    ActiveWindow.Close
    ThisDocument.Activate 'Return to THIS Document
    Selection.EndKey Unit:=wdLine
    Selection.PasteAndFormat (wdFormatOriginalFormatting)
    Documents("S:\ARRANGEMENT FILES\0 Secretarial Masters\0 FILE START-UP\Old Arrangement File\part 2 NEW MASTER - Arrangement File.docx").Close

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Are you sure you want to go to the end of the line? I don't think this unit works but I would have thought that you should move to the start of the next PARAGRAPH since you are inserting an entire document and wouldn't want this to be inserted in the middle of a paragraph.
    Try fiddling this code to get it working exactly right
    Code:
      Dim oDocA As Document, oDocB As Document, myRange As Range
      Set oDocA = ActiveDocument
      Set myRange = Selection.Range
      myRange.MoveStart Unit:=wdParagraph, Count:=1  'Collapses insertion point to the start of the following paragraph.
      Set oDocB = Documents.Open(FileName:="S:\ARRANGEMENT FILES\0 Secretarial Masters\0 FILE START-UP\Old Arrangement File\part 2 NEW MASTER - Arrangement File.docx", ReadOnly:=True)
      oDocB.Range.Copy
      oDocB.Close SaveChanges:=False
      myRange.PasteAndFormat wdFormatOriginalFormatting
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    New Lounger
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    your suggestion worked perfectly ...except

    your suggestion worked perfectly ...except...when I lock (restrict editing to filling in forms). It no longer works and I get “run-time error ‘4198” Command failed and it highlights the following line in yellow

    oDocB.Close SaveChanges:=False

    To run the marco in a locked document I am running the marco upon entry into a form field

    Any suggestions would be greatly appreciated

    Thank you in advance for your help

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    I assume it is the active document that is protected (oDocA in the code). You may need to add lines to disable the document protection while the main lines of code run and then turn it on again at the end.

    You can record the act of turning on and off the protection to see what the code needs to look like.

    Alternatively, you may be trying to insert the content from oDocB inside a formfield. This might be done more directly by using the formfield's range as the target location instead of the next paragraph. Is that what you are trying to do?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •