Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Aug 2006
    Location
    Gtr London, England
    Posts
    256
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Copy content to new Word document

    Hi,

    Im trying to put together a macro that copies the content of the active document as a new document. The name of the new document is liable to change and therefore gets the name from a text file.

    Im having problems creating the new document in which to copy to as per my code so far;

    <code>
    Code:
    Sub import()
     
       Dim MyFileName As String 'This will be the name of the new document
    
        Open "C:\Sol\test.txt" For Input As #1
        Line Input #1, MyFileName
        Do
          While Not EOF(1)
          Wend
        Loop While EOF(1) = False
         Close #1
        Call Copy_Content(MyFileName)
    End Sub
    
    
    Sub Copy_Content(MyFileName As String)
        Dim targDoc  As Word.Document
        
        'MsgBox MyFileName 'Used to test the variable was passed
        ActiveDocument.Range.Copy
            
        Set targDoc = Documents.Add("c:\sol\" & MyFileName & ".doc") 'Here Im trying to create a new document to paste to
        targDoc.Range.PasteAndFormat wdPasteDefault
        
        
    End Sub
    <\code>

    I would be grateful for any advice please.

    Best Regards
    Lee
    Last edited by macropod; 2013-12-05 at 16:47. Reason: Added the correct [code] tags

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,146
    Thanks
    352
    Thanked 1,417 Times in 1,290 Posts
    Lee,

    Here's a slightly different approach to your problem. You can just use a SaveAs command to make the copy and then close it and reopen the original document.
    Code:
    Option Explicit
    
    Sub NewFile()
    
       Dim oCurrentDocument As Document
       Dim zMyFileName      As String
       Dim zCurDocName      As String
       Dim zCurDocPath      As String
       
       Set oCurrentDocument = ActiveDocument
       zCurDocName = oCurrentDocument.Name
       zCurDocPath = oCurrentDocument.Path
       
       Application.ScreenUpdating = False
       
    '*** Change the file path as appropriate below.
    
       Open "G:\BEKDocs\Word\Test.txt" For Input As #1
       Line Input #1, zMyFileName
    
    '*** Note: Add the drive path to the FileName if you want it saved to a
    '***       other than where the current document is stored
    
        ActiveDocument.SaveAs2 FileName:=zMyFileName, FileFormat:= _
            wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
            :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
            :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
            SaveAsAOCELetter:=False, CompatibilityMode:=14
            
    '*** Close the document contining the COPY!
        ActiveDocument.Close wdDoNotSaveChanges
        
    '*** Re-open the original document
        Documents.Open FileName:=zCurDocPath & "\" & zCurDocName
                
    End Sub      '*** NewFile() ***
    Note: This macro has to be stored in your Normal template since the Document file will be closed!

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. The Following User Says Thank You to RetiredGeek For This Useful Post:

    leepurton (2013-12-06)

  4. #3
    3 Star Lounger
    Join Date
    Aug 2006
    Location
    Gtr London, England
    Posts
    256
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Retired Geek,

    That is a better approach, thanks for the tip and advice, it works a treat.

    If its not too early, Merry Christmas.

    Best Regards.

    Lee

Posting Permissions

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