+ Reply to Thread
Results 1 to 3 of 3

VBA Excel to Word copy and pasting

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    405

    VBA Excel to Word copy and pasting

    Hi guys,

    Here is my complete code:

    Option Explicit
    
    Sub Connection11()
    
    'On Word document
    'Developer
    'Design mode
    'Properties
    
    
    Dim OldLocation As String
    Dim NewLocation As String
    Dim X1 As Range
    Dim BlankstoSkip As Long
    Dim WsName1 As String
    Dim WsName2(0 To 4) As String
    Dim WsName3(0 To 1) As String
    Dim Tempname As String
    Dim Filename As String
    Dim i As Long
    Dim ii As Long
    Dim strOldPath As String
    
    
    
    OldLocation = "C:\Users\james.shaw\Desktop\New folder (2)"
    NewLocation = "C:\Users\james.shaw\Desktop\New folder (2)\New folder"
    
    BlankstoSkip = 10
    ThisWorkbook.Worksheets(1).Select
    line1:
    
    Do
    WsName1 = ActiveWorkbook.ActiveSheet.Name
    '    WsName2(0) = "Construction Water Pump"
    '   WsName2(1) = "Constr.WaterPump"
        WsName2(2) = "CW1 Pump Station"
        WsName2(3) = "ESDD Pump_Service"
        WsName2(4) = "ESDD Pump"
            For i = 0 To 4
            WsName1 = Replace(WsName1, WsName2(i), "")
            Next i
        If WsName1 = "" Or WsName1 = "Construction Water Pump" Or WsName1 = "Constr.WaterPump" Then
            ActiveSheet.Next.Select
            GoTo line1:
        End If
    
    Filename = Range("j2")
        WsName3(0) = "TEMPLATE "
        WsName3(1) = ".DOCX"
            For i = 0 To 0
    Filename = Replace(Filename, WsName3(i), "")
            Next i
            For i = 1 To 1
    Tempname = Replace(Filename, WsName3(i), "")
            Next i
    
    Set X1 = ActiveWorkbook.ActiveSheet.Range("B4")
    X1.Select
    For ii = 1 To BlankstoSkip + 1
    Do While Not IsEmpty(X1.Offset(ii, 0))
    Selection.End(xlDown).Select
    Set X1 = Selection.Offset(0, 2)
    ii = 1
    Loop
    Next ii
    Range("A1", X1).Select
    Selection.Copy
    
    Dim rng As Range
    strOldPath = OldLocation & "\" & Filename
    Set rng = Range("A1", X1)
    
    
    Application.ScreenUpdating = False
    Application.EnableEvents = True
    
        Dim WordApp As Object
        Dim WordDoc As Word.Document
        Dim WordTable As Word.Table
        Set WordApp = CreateObject("Word.Application")
        Set WordDoc = WordApp.Documents.Open(Filename:=strOldPath)
    
    rng.Copy
    
      WordDoc.Paragraphs(220).Range.PasteExcelTable _
        LinkedToExcel:=False, _
        WordFormatting:=False, _
        RTF:=False
    
    'Autofit Table so it fits inside Word Document
    '  Set WordTable = WordDoc.Tables()
    '  WordTable.AutoFitBehavior (wdAutoFitWindow)
      
      
    
    
    WordDoc.SaveAs Filename:=NewLocation & "\" & Filename & WsName1 & ".DOCX"
    
    
    WordDoc.Close
        Set WordDoc = Nothing
        Set WordApp = Nothing
        
    
    
    ActiveSheet.Next.Select
    Loop
    
    'WordDoc.Close
    '    Set WordDoc = Nothing
    '    Set WordApp = Nothing
    
    End Sub

    I am curious about this part here:
      WordDoc.Paragraphs(220).Range.PasteExcelTable _
        LinkedToExcel:=False, _
        WordFormatting:=False, _
        RTF:=False

    It has take me all night, by trial and error, guessing the "220" to get my excel tables in the right spot of my word document.

    I am curious to know if there is a way to get this property within a word document.

    I am thinking something like "Word count" where it gives you information about an area you have highlighted?



    2) I keep getting messages saying "This word document is already opened by another user", any ideas how to get around that?




    Thanks guys,

    Jimmy

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Excel to Word copy and pasting

    What about adding a Bookmark...? Then you can go directly to that spot.

  3. #3
    Forum Contributor
    Join Date
    04-02-2017
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    405

    Re: VBA Excel to Word copy and pasting

    Quote Originally Posted by cytop View Post
    What about adding a Bookmark...? Then you can go directly to that spot.
    I like the sound of that!

    What would be the code to do that? I only learnt about bookmarks about two days ago.

+ 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: 2
    Last Post: 08-06-2012, 11:15 AM
  2. [SOLVED] Auto word searching just by copy pasting a keyword
    By Raju Radhakrishnan in forum Excel General
    Replies: 5
    Last Post: 05-22-2012, 09:36 AM
  3. Copy/Pasting an entire worksheet to Word with VBA
    By aduma in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-23-2012, 06:23 PM
  4. Pasting from word into Excel
    By Dave-R-W in forum Excel General
    Replies: 6
    Last Post: 04-03-2009, 10:34 AM
  5. Pasting from Word into Excel
    By Dave-R-W in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-03-2009, 09:27 AM
  6. pasting excel graphs into word
    By mufan in forum Excel General
    Replies: 2
    Last Post: 07-09-2008, 10:41 AM
  7. Pasting Between Excel and Word
    By Alex in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-19-2006, 04:30 PM
  8. [SOLVED] Pasting a Word Table into Excel
    By Dee in forum Excel General
    Replies: 1
    Last Post: 04-05-2005, 09:06 PM

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