Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Excel to Word doc / File Patch -> cell
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Nov 2016
    Location
    Poland
    Posts
    49
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Excel to Word doc / File Patch -> cell

    Hi there

    I am not advanced Vba programmer and I need your help. Could please review my code and tell how to change the DOC(template) file path ? In other words, I would like to use 3 different word templates so I can't put file path in VBA code as it is. I need to put a reference to cell where there is a file path.
    A1 - C:\Users\Tom\Desktop\doc1.docx
    A2 - C:\Users\Tom\Desktop\doc2.docx
    A3 - C:\Users\Tom\Desktop\doc1.docx

    Another thing, I would also like to add a possiblity to save DOCs and PDFs in created folder by VBA.

    My current VBA code:

    Code:
    Sub CreateWordTemplate()
    
    
    
    
        Dim wdApp As Word.Application
        Dim SaveName As String
        Dim FileExt As String
        Set wdApp = CreateObject("Word.Application")
    
    
        With wdApp
            '.Visible = True
            '.Activate
            
            .Documents.Add "C:\Users\name\Desktop\doc1.docx"
    
    
    
    
        Range("A1", Range("A1").End(xlDown).End(xlToRight)).Copy
            
        
        .Selection.GoTo What:=-1, Name:="bookmark1"
        .Selection.PasteSpecial
    
    
    FileExt = ".pdf"
                                
                     SaveName = Environ("UserProfile") & "\Desktop\doc1.docx " & _
            Format(Now, "yyyy-mm-dd hh-mm-ss") & ".pdf"
    
    
    
    
                    .ActiveDocument.ExportAsFixedFormat OutputFileName:=SaveName, ExportFormat:=17
                  
            
           SaveName = Environ("UserProfile") & "\Desktop\doc1.docx " & _
            Format(Now, "yyyy-mm-dd hh-mm-ss") & ".docx"
    
    
    
    
        If .Version <= 12 Then
            .ActiveDocument.SaveAs SaveName
        Else
        .ActiveDocument.SaveAs2 SaveName
        End If
    
    
        .ActiveDocument.Close
        .Quit
    
    
        End With
    
    
        Set wdApp = Nothing
    End Sub
    Thank you in advance !!!

    Tomasz
    Last edited by Macropod; Nov 21st, 2016 at 04:36 PM. Reason: Repaired code tags

  2. #2
    Board Regular
    Join Date
    May 2011
    Location
    Russia
    Posts
    2,834
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel to Word doc / File Patch -> cell

    Code:
    Dim rng As Range
    Set rng = Range("A1:A3")
    ....
    .Documents.Add rng(1) 'A1
    '.Documents.Add rng(2) 'A2
    '.Documents.Add rng(3) 'A3

  3. #3
    New Member
    Join Date
    Nov 2016
    Location
    Poland
    Posts
    49
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel to Word doc / File Patch -> cell

    Hi Sektor, thanks for quick reply. Unfortunately it doesn't work. I think I did something wrong. Could you pls take a look ?

    Sub CreateBasicWordReport()


    Code:
        Dim wdApp As Word.Application
        Dim SaveName As String
        Dim FileExt As String
        Dim rng As Range
        Set rng = Range("A1:A3")
        Set wdApp = CreateObject("Word.Application")
    
    
        With wdApp
            '.Visible = True
            '.Activate
            
            .Documents.Add rng(1) 'A1
            .Documents.Add rng(2) 'A2
            .Documents.Add rng(3) 'A3

  4. #4
    Board Regular
    Join Date
    May 2011
    Location
    Russia
    Posts
    2,834
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel to Word doc / File Patch -> cell

    Try .Documents.Open

  5. #5
    New Member
    Join Date
    Nov 2016
    Location
    Poland
    Posts
    49
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel to Word doc / File Patch -> cell

    The same

  6. #6
    Board Regular
    Join Date
    May 2011
    Location
    Russia
    Posts
    2,834
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel to Word doc / File Patch -> cell

    Oh, uncomment lines:
    Code:
    '.Visible = True
    '.Activate

  7. #7
    New Member
    Join Date
    Nov 2016
    Location
    Poland
    Posts
    49
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel to Word doc / File Patch -> cell

    Sektor, I'm really gratefull for your support but it still doesn't work (

  8. #8
    Board Regular
    Join Date
    May 2011
    Location
    Russia
    Posts
    2,834
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel to Word doc / File Patch -> cell

    I am sorry for not carefully reading your post. You're talking about templates rather documents. Templates have extensions DOTX (macro-free) and DOTM (with macro) and DOT (for Word < 2007). As I see, you have DOCX file extension - which is not template but rather ordinary document.

  9. #9
    New Member
    Join Date
    Nov 2016
    Location
    Poland
    Posts
    49
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Excel to Word doc / File Patch -> cell

    Yes I know but it is not a problem. I changed it before so it is fine. Anyway it still doesn't work

    VBA error -> .Documents.Add rng(1) 'A1

  10. #10
    Retired Moderator Macropod's Avatar
    Join Date
    Aug 2007
    Location
    Canberra, Australia
    Posts
    3,290
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)

    Default Re: Excel to Word doc / File Patch -> cell

    beczer: there are numerous problem with your code, but I guess you knew that already.

    One thing that isn't clear is why you're using the Documents.Add method for an existing document instead of using it for a template or, alternatively, why you're not using the Documents.Open method for the existing document.

    Another oddity is your retention of the .docx extension in the middle of the output filenames - for both the document and the pdf.

    You also refer to saving the outputs to a folder created by VBA, which is quite possible, but you given no indication of how that folder's name might be determined.

    Try the following code. It generates output files from your data in A1-A3.
    Code:
    Sub CreateWordOutputs()
    Dim wdApp As Word.Application, wdDoc As Word.Document
    Dim xlWkSht As Worksheet, StrName As String, i As Long
    'Define our worksheet
    Set xlWkSht = ActiveSheet
    'Initialize Word
    Set wdApp = CreateObject("Word.Application")
    With wdApp
      'Loop through A1-A3 for our filenames
      For i = 1 To 3
        StrName = xlWkSht.Range("A" & i).Value
        'Open the file
        Set wdDoc = .Documents.Open(StrName)
        'Reconfigue the filename for output usage
        StrName = Split(StrName, .docx)(0) & " " & Format(Now, "yyyy-mm-dd hh-mm-ss")
        'Get our data
        xlWkSht.Range("A1", Range("A1").End(xlDown).End(xlToRight)).Copy
        With wdDoc
          'Add the data to our document
          .Bookmarks("bookmark1").Range.Paste
          'Save our document as both a Word document and as a PDF
          .SaveAs Filename:=StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
          .SaveAs Filename:=StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
          'Close our file without re-saving
          .Close False
        End With
      Next
      'Exit Word
      .Quit
    End With
    'Cleanup
    Set wdDoc = Nothing: Set wdApp = Nothing: Set xlWkSht = Nothing
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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