简体   繁体   中英

Name folder and file from cell, and save the file in the folder

Here is, what I want the macro to do:

  1. Create new folder defined by a cell in Excel.
  2. Open xxx numbers of word-documents to update chain references.
  3. Save Word-documents with different filenames defined by cells in Excel.
  4. Save Word-documents (in the new folder) with different filenames defined by cells in Excel.
  5. Close Word-documents.

Almost all works fine, but so far I can only save files into an existing folder (New customer).

Code looks like this:

Sub openWord()

Dim folderPath As String
folderPath = "C:\Users\klebg\OneDrive\Zitekick\Kunder\Byg & Brand\TEST\New customer"
'Check if the folder exists
If Dir(folderPath, vbDirectory) = "" Then

    'Folder does not exist, so create it
    MkDir folderPath
    
     Else
        MsgBox "Mappe med samme navn eksisterer allerede"
        
 End If
    
Set wordapp = CreateObject("word.Application")
    wordapp.Documents.Open "C:\Users\Path\TEST\test.docx"
    wordapp.Visible = True
    'Auto save
   
    wordapp.ActiveDocument.SaveAs "C:\Users\Path\TEST\New customer\" & Sheets("Testark").Range("A8")
    wordapp.Documents.Close
       
    wordapp.Documents.Open "C:\Users\Path\TEST\test2.docx"
    wordapp.Visible = True
    'Auto save
    wordapp.ActiveDocument.SaveAs "C:\Users\klebg\OneDrive\Zitekick\Kunder\Byg & Brand\TEST\New customer\" & Sheets("Testark").Range("B8")
    wordapp.Documents.Close
              
End Sub

Your line:

folderPath = "C:\Users\klebg\OneDrive\Zitekick\Kunder\Byg & Brand\TEST\New customer"

Tells your code where to save it. You need to change the definition of folderpath to something other than "New customer" if you want to save elsewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM