+ Reply to Thread
Results 1 to 6 of 6

Copy an embedded word document and paste into a folder

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-13-2017
    Location
    Durham
    MS-Off Ver
    2013
    Posts
    126

    Copy an embedded word document and paste into a folder

    Hi Guys,

    As my title states - Is it possible to copy 'multiple' embedded word documents from a workbook and then place them into a designated folder?

    Having done a little research I see people with varying success of copying OLEobjects however it all seems to evolve around having the actual name of the document. In my case the names will always be different so it would have to be a general sweep on either the worksheet or the Colum they're held in

    Thanks

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Copy an embedded word document and paste into a folder

    Try something like this. Change the save path to suit.

    Sub Save_Embedded_Word_Docs()
        
        Dim ws As Worksheet
        Dim oleObj As Object
        Dim counter As Long
        Dim strPath As String
        
        strPath = "C:\Test\"    'Word doc save folder
        
        Application.ScreenUpdating = False
        For Each ws In Worksheets
            For Each oleObj In ws.OLEObjects
                If oleObj.progID Like "Word.Doc*" Then
                    oleObj.Verb Verb:=xlPrimary
                    Application.Goto ws.Range("A1")
                    counter = counter + 1
                    oleObj.Object.SaveAs strPath & oleObj.Parent.Name & Format(Now, " yyyymmdd hhmmss ") & counter & ".doc"
                End If
            Next
        Next ws
        Application.ScreenUpdating = True
        
        MsgBox counter & " files saved.", , "Save Word Docs Complete"
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Copy an embedded word document and paste into a folder

    Public Sub SaveEmbDocs()
    
        Dim sht As Worksheet
        Dim Word As Word.Application
        Dim Doc As Word.Document
        Dim embDoc As Object
        Dim DocPath As String
        
        Set sht = ThisWorkbook.Sheets("Sheet1") 'Change the sheetname to you requirements
        DocPath = ThisWorkbook.Path & "\" 'Add the desired folder here
        
        
        On Error Resume Next
        Set Word = GetObject(, "Word.Application")
        If Err.Number <> 0 Then 'Check if Word is already running
            Set Word = CreateObject("Word.Application")
        End If
        On Error GoTo 0
    
        'Go thru all oleobjects on Sheet1
        For Each oleObj In sht.OLEObjects
            If LCase(Left(oleObj.progID, 4)) = "word" Then 'Only save Word docs
                Set embDoc = oleObj
                embDoc.Verb Verb:=xlOpen
                'Do not show the documents
                Word.Visible = False
                Word.WindowState = wdWindowStateMinimize
                Set Doc = Word.ActiveDocument
                'Haven't succeeded to retrieve the document filename (yet)
                Doc.SaveAs DocPath & sht.Name & "_EmbDoc_" & oleObj.Name & ".docx", FileFormat:=wdFormatXMLDocument
                Doc.Close False
            End If
        Next oleObj
        
        Set Word = Nothing
        Set Doc = Nothing
        Set embDoc = Nothing
    
    End Sub
    Cheers!
    Tsjallie




    --------
    If your problem is solved, pls mark the thread SOLVED (see Thread Tools in the menu above). Thank you!

    If you think design is an expensive waste of time, try doing without ...

  4. #4
    Forum Contributor
    Join Date
    02-13-2017
    Location
    Durham
    MS-Off Ver
    2013
    Posts
    126

    Re: Copy an embedded word document and paste into a folder

    Hi Guys,

    Apologies for the late reply!

    Both your codes seem to work in regards to searching the sheet/column for the word doc however they don't seem to save in the designated path. Any ideas?

    Thanks

  5. #5
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Copy an embedded word document and paste into a folder

    Retrieving the full name and path of the documents is only possible when "link to file" has been selecting when they where inserted.
    Are the files in your workbook linked or just embedded?

  6. #6
    Forum Contributor
    Join Date
    02-13-2017
    Location
    Durham
    MS-Off Ver
    2013
    Posts
    126

    Re: Copy an embedded word document and paste into a folder

    Hi Tsjallie,

    They were imbedded however I managed to get it resolved thank you
    The code grabs them and pulls them through to a folder, naming them object 1, object 2 etc. It's a shame they don't get named what they are in the actual workbook but that's fine.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 09-04-2014, 03:32 AM
  2. Macro to copy and paste from another Word 2007 document.
    By Excelnoub in forum Word Programming / VBA / Macros
    Replies: 3
    Last Post: 04-08-2014, 09:06 PM
  3. Make a copy of a word document and paste it in a different folder with a name 'Test'
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-08-2013, 01:09 PM
  4. Copy data from Excel and paste it in a table in word document
    By shekhar in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-04-2012, 05:28 AM
  5. Replies: 0
    Last Post: 06-07-2012, 02:21 PM
  6. Embedded Word document
    By bforster1 in forum Excel General
    Replies: 0
    Last Post: 11-02-2005, 04:50 PM
  7. Embedded Word Document
    By purplegerbil in forum Excel General
    Replies: 2
    Last Post: 11-01-2005, 05:46 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1