Results 1 to 3 of 3

Excel VBA to create Word file and add a custom property to it

Threaded View

  1. #1
    Registered User
    Join Date
    10-15-2008
    Location
    Belgium
    Posts
    6

    Excel VBA to create Word file and add a custom property to it

    Hi all,

    I have an Excel sheet with a long list of data. A short example is shown below:
    Section | Title | Item
    1 | INTRODUCTION | a
    1.1 | title2 | b
    1.2 | title3 | c
    1.2.1 | title4 | d
    1.2.2 | title5 | e

    ...

    I made a VBA macro in Excel that runs through this list and creates a new Word file for each item. The filename of the document is based on the data in the Excel file (section and title). Now I would like to add a custom property to each of the newly created Word files, i.e. the value in the 'item' column. Does anyone of you know how I should do this? Or should it be better if I write a macro in Word that runs through the Excel data to create the word files?

    Here is the code I use to generate the word files:
    Sub createdocuments()
    
        Dim wrdApp As Word.Application
        Dim wrdDoc As Word.Document
        Dim i, n As Integer
        Dim bestandsnaam As String
        
        Set wrdApp = CreateObject("Word.Application")
        wrdApp.Visible = False
        n = 2
        
        For i = 1 To 5
        Set wrdDoc = wrdApp.Documents.Add
    
        bestandsnaam = "P:\test\" & Cells(n, 1).Value & " - " & Cells(n, 2).Value & ".doc"
    
        With wrdDoc
    
            If Dir("bestandsnaam") <> "" Then
                Kill bestandsnaam
            End If
    
            .SaveAs (bestandsnaam)
            .Close ' close the document
        End With
    
        
        n = n + 1
    
        Next
        wrdApp.Quit ' close the Word application
        Set wrdDoc = Nothing
        Set wrdApp = Nothing
    
    End Sub
    I tried this to add a custom property to the word file but this didn't seem to work:

    Documents(bestandsnaam).CustomDocumentProperties("Item").Value = "jj"
    Last edited by sverhaeghe; 04-02-2009 at 12:32 PM.

Thread Information

Users Browsing this Thread

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

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