Consulting

Results 1 to 14 of 14

Thread: Solved: Create a new word doc for every worksheet?

  1. #1

    Solved: Create a new word doc for every worksheet?

    How do I go around doing a macro or a loop to create a new word document for every worksheet I have in a workbook

    Lets say in a workbook I got 10 worksheet and if I run the macro or something I will have 10 word document.

    Thanks

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [vba]Sub MakeDocs()
    'This code requires a referece to the Word object model
    Dim sh As Worksheet, Appword As New Word.Application
    Set Appword = CreateObject("Word.Application")
    Appword.Visible = True
    For Each sh In Sheets
    Appword.Documents.Add
    Next
    Set Appword = Nothing
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    mdmackillop one more question, how about making every new word doc to have the same name as the worksheet? And after creating all these new word doc they will be move over to a newly created folder

    I got the code to create a new folder but how do I place all these newly created word doc to the correct folders?

    Thanks for your help..
    Last edited by thedark123; 06-18-2006 at 04:15 AM.

  4. #4
    Here is the code I have done:

    [VBA]Private Sub CommandButton1_Click()

    Dim x
    Dim lCount As Long
    Dim lMax As Integer
    Dim lMax1() As Integer
    Dim lCount1() As Integer
    Dim title() As String
    Dim WS_Count As Integer
    Dim i As Integer
    Dim LastCol() As Integer



    ' Prompt the user for the folder to list.
    x = InputBox("What folder do you want to list?" & Chr$(13) & Chr$(13) _
    & "For example: C:\My Documents")

    If x = "" Or x = " " Then
    Response = MsgBox("Please Enter a Directory Location" _
    & Chr$(13) & Chr$(13) & _
    "To enter directory location, click No." & Chr$(13) & _
    "To Exit, click Yes.", vbYesNo)
    If Response = "6" Then
    End If
    Else

    ' Search Drive
    ChDrive "C"
    ChDir x

    On Error Resume Next

    ' Place .xls files into Worksheet and tabulate data
    outrow = 2
    filess = Dir("*.xls")

    While Not filess = ""
    Workbooks.Open Filename:=filess, UpdateLinks:=False


    Dim newfol As String
    newfol = filess
    ChDir "C:\Documents and Settings\Administrator\Desktop"
    On Error Resume Next
    MkDir (newfol)

    ' requires a reference to the Word Object library:
    ' in the VBE select Tools, References and check the Microsoft Word X.X object library
    Dim wdApp As Word.Application, wdDoc As Word.Document, ws As Worksheet
    Application.ScreenUpdating = False
    Application.StatusBar = "Creating new document..."


    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Add
    Dim totalcolumn As Integer

    ' Appword.Visible = True
    'For Each ws In Sheets
    ' wdApp.Documents.Add
    'Next
    ' Set wdApp = wdDoc


    For Each ws In ActiveWorkbook.Worksheets
    Application.StatusBar = "Copying data from " & ws.Name & "..."
    totalcolumn = WorksheetFunction.Max(ws.Range("3:3")) + 3

    For i = 3 To totalcolumn
    If Not ws.Cells(3, i).Value = "" Then
    ws.Range(ws.Cells(3, i), ws.Cells(9, i)).Copy
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste
    Application.CutCopyMode = False
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter

    ' insert page break after all Worksheets except the last one
    If Not i = totalcolumn Then
    With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range
    .InsertParagraphBefore
    .Collapse Direction:=wdCollapseEnd
    .InsertBreak Type:=wdPageBreak
    End With
    End If
    End If
    Next i
    Next ws

    Set ws = Nothing
    Application.StatusBar = "Cleaning up..."

    ' apply normal view
    With wdApp.ActiveWindow
    If .View.SplitSpecial = wdPaneNone Then
    .ActivePane.View.Type = wdNormalView
    Else
    .View.Type = wdNormalView
    End If
    End With
    Set wdDoc = Nothing
    wdApp.Visible = True
    Set wdApp = Nothing
    Application.StatusBar = False

    filess = Dir()
    Wend
    End If
    End Sub
    [/VBA]

  5. #5
    See this part:

    [VBA]
    ' Appword.Visible = True
    'For Each ws In Sheets
    ' wdApp.Documents.Add
    'Next
    ' Set wdApp = wdDoc
    [/VBA]

    I used your code but seems to be something wrong wif it.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [vba]see below[/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    mdmackillop how do I do a preformat to the cells i copied over from excel and I wan to paste to word.

    Here is the data i copied from excel:




    And should appear in this format in word:


  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You have already asked this question in another thread, and if I feel that I can contribute, would do so there.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    How do I create respective folder to store all the newly created word document?

    "filess" in this case are the name of the excel file


    [VBA]
    Dim newfol As String
    newfol = filess



    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Add
    Dim MyFol As String
    MyFol = ActiveWorkbook.Path
    ChDir (MyFol)
    MkDir (newfol)
    MyFol = ActiveWorkbook.Path & "\" & filess & "\"
    On Error Resume Next
    [/VBA]

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA] MyFol = Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) - 4) & "\"
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    [vba]Dim newfol As String
    newfol = filess



    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Add
    Dim MyFol As String
    MyFol = ActiveWorkbook.Path
    ChDir (MyFol)
    MkDir (newfol)
    MyFol = Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) - 4) & "\"

    On Error Resume Next [/vba]


    Is it like that? No new folder seems to be created...

    here is my full code

    [vba]Private Sub CommandButton1_Click()

    Dim x
    Dim lCount As Long
    Dim lMax As Integer
    Dim lMax1() As Integer
    Dim lCount1() As Integer
    Dim title() As String
    Dim WS_Count As Integer
    Dim i As Integer
    Dim LastCol() As Integer



    ' Prompt the user for the folder to list.
    x = InputBox("What folder do you want to list?" & Chr$(13) & Chr$(13) _
    & "For example: C:\My Documents")

    If x = "" Or x = " " Then
    Response = MsgBox("Please Enter a Directory Location" _
    & Chr$(13) & Chr$(13) & _
    "To enter directory location, click No." & Chr$(13) & _
    "To Exit, click Yes.", vbYesNo)
    If Response = "6" Then
    End If
    Else

    ' Search Drive
    ChDrive "C"
    ChDir x

    On Error Resume Next

    ' Place .xls files into Worksheet and tabulate data
    outrow = 2
    filess = Dir("*.xls")

    While Not filess = ""
    Workbooks.Open Filename:=filess, UpdateLinks:=False



    ' requires a reference to the Word Object library:
    ' in the VBE select Tools, References and check the
    ' Microsoft Word X.X object library
    Dim wdApp As Word.Application, wdDoc As Word.Document, ws As Worksheet
    Application.ScreenUpdating = False
    Application.StatusBar = "Creating new document..."


    Dim newfol As String
    newfol = filess



    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Add
    Dim totalcolumn As Integer
    Dim MyFol As String
    MyFol = ActiveWorkbook.Path

    ChDir (MyFol)
    MkDir (newfol)
    ' MyFol = ActiveWorkbook.Path & "\" & filess & "\"
    MyFol = Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) - 4) & "\"
    On Error Resume Next





    'Dim oTbl As Table
    'Set oTbl = wdDoc.Tables.Add(Selection.Range, 11, 2)
    'This code requires a referece to the Word object model

    For Each ws In ActiveWorkbook.Worksheets
    Application.StatusBar = "Copying data from " & ws.Name & "..."
    totalcolumn = WorksheetFunction.Max(ws.Range("3:3")) + 3

    For i = 3 To totalcolumn
    If Not ws.Cells(3, i).Value = "" Then
    ws.Range(ws.Cells(3, i), ws.Cells(9, i)).Copy
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste
    Application.CutCopyMode = False
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter

    ' insert page break after all Worksheets except the last one
    If Not i = totalcolumn Then
    With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range
    .InsertParagraphBefore
    .Collapse Direction:=wdCollapseEnd
    .InsertBreak Type:=wdPageBreak
    End With
    End If
    End If
    Next i

    'Save as sheet name and close
    wdDoc.SaveAs Filename:=MyFol & ws.Name & ".doc"

    Next ws
    'wdDoc.Close

    Set ws = Nothing
    Application.StatusBar = "Cleaning up..."

    ' apply normal view
    With wdApp.ActiveWindow
    If .View.SplitSpecial = wdPaneNone Then
    .ActivePane.View.Type = wdNormalView
    Else
    .View.Type = wdNormalView
    End If
    End With
    Set wdDoc = Nothing
    wdApp.Visible = True
    Set wdApp = Nothing
    Application.StatusBar = False

    filess = Dir()
    Wend
    End If
    End Sub
    [/vba]

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sorry,
    I missed the new folder. Here's my revised code
    [VBA]
    Sub MakeDocs()
    'This code requires a referece to the Word object model
    Dim sh As Worksheet, Appword As New Word.Application
    Dim MyFol As String
    Set Appword = CreateObject("Word.Application")
    'Create a save path
    MyFol = Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) - 4) & "\"
    MkDir MyFol
    For Each sh In Sheets
    'Create documents
    Appword.Documents.Add
    'Save as sheet name
    Appword.ActiveDocument.SaveAs Filename:=MyFol & sh.Name & ".doc"
    'Close document
    Appword.ActiveDocument.Close
    Next
    Set Appword = Nothing
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Malcolm,
    I had to add:
    [vba]sh.UsedRange.Copy
    Appword.ActiveDocument.Range.Paste
    Application.CutCopyMode = False[/vba]
    after:
    [vba]
    Appword.Documents.Add

    [/vba]
    to get this to work...probably a better way....but?!
    Works great though.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  14. #14
    Ok Solved thanks guys : mdmackillop and lucas =)

Posting Permissions

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