EMONTES149
New Member
- Joined
- Dec 20, 2016
- Messages
- 8
Help Needed With step 5.
The following macro lets the user select a project folder with ".doc" files then inserts these subdocuments into a Master Document. It works exactly the way I envision it; however, I have to hard code the path. Can someone help me modify the code so that It can loop through all the files in the folder regardless of file name. I do not want to hard code the path with file name because the files vary from project to project. I am not a code writer, but an architect so any guidance is very much appreciated.
Sub CreatingRelativeSubDocuments()
' Step 1 - The following code sets the variables and lets Excel Know that it will be controlling Word.
Dim fldr As FileDialog
Dim objWord
Dim objDoc
Dim SaveName As String
SaveName = (Sheets("Header-Footer").Range("H5") & "-" & Sheets("Header-Footer").Range("H6"))
' Step 2 - The following code lets you pick the project folder.
MsgBox "On Next Screen Select Project Folder"
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
On Error Resume Next
FolderName = .SelectedItems(1)
Err.Clear
On Error GoTo 0
End With
' Step 3 - The following code opens a Word Document so you can begin the Master Spec.
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
' Step 4 - The following code sets the Word Document to the Outline View and Expands the Sub Document Format.
objWord.Visible = True
objWord.ActiveWindow.ActivePane.View.Type = wdOutlineView
objWord.ActiveDocument.Subdocuments.Expanded = Not objWord.ActiveDocument.Subdocuments.Expanded
' Step 5 - The following code inserts the Sub Documents based on the Folder Path that you picked in Step 2
objWord.Selection.Range.Subdocuments.AddFromFile Name:=FolderName & "" & "011000 - summary.doc"
objWord.Selection.Range.Subdocuments.AddFromFile Name:=FolderName & "" & "012100 - allowances.doc"
objWord.Selection.Range.Subdocuments.AddFromFile Name:=FolderName & "" & "012200 - unit prices.doc"
objWord.Selection.Range.Subdocuments.AddFromFile Name:=FolderName & "" & "012300 - alternates.doc"
objWord.Selection.Range.Subdocuments.AddFromFile Name:=FolderName & "" & "013100 - Project Management and Coordination.doc"
'Step 6 - This returns word to outline view and prepares to save the file
objWord.ActiveWindow.ActivePane.View.Type = wdOutlineView
If objWord.ActiveWindow.View.SplitSpecial = wdPaneNone Then
objWord.ActiveWindow.ActivePane.View.Type = wdNormalView
Else
objWord.ActiveWindow.View.Type = wdNormalView
End If
objWord.ActiveWindow.ActivePane.View.Type = wdOutlineView
'Step 7 - This saves the file to User Defined File Name
MsgBox "Next Select Project Folder and Master Specification File Name"
'Step 8 - this Stops, Quits and releases the Word Application
objWord.ActiveDocument.Close
objWord.Quit
Set objWord = Nothing
End Sub
The following macro lets the user select a project folder with ".doc" files then inserts these subdocuments into a Master Document. It works exactly the way I envision it; however, I have to hard code the path. Can someone help me modify the code so that It can loop through all the files in the folder regardless of file name. I do not want to hard code the path with file name because the files vary from project to project. I am not a code writer, but an architect so any guidance is very much appreciated.
Sub CreatingRelativeSubDocuments()
' Step 1 - The following code sets the variables and lets Excel Know that it will be controlling Word.
Dim fldr As FileDialog
Dim objWord
Dim objDoc
Dim SaveName As String
SaveName = (Sheets("Header-Footer").Range("H5") & "-" & Sheets("Header-Footer").Range("H6"))
' Step 2 - The following code lets you pick the project folder.
MsgBox "On Next Screen Select Project Folder"
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
On Error Resume Next
FolderName = .SelectedItems(1)
Err.Clear
On Error GoTo 0
End With
' Step 3 - The following code opens a Word Document so you can begin the Master Spec.
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
' Step 4 - The following code sets the Word Document to the Outline View and Expands the Sub Document Format.
objWord.Visible = True
objWord.ActiveWindow.ActivePane.View.Type = wdOutlineView
objWord.ActiveDocument.Subdocuments.Expanded = Not objWord.ActiveDocument.Subdocuments.Expanded
' Step 5 - The following code inserts the Sub Documents based on the Folder Path that you picked in Step 2
objWord.Selection.Range.Subdocuments.AddFromFile Name:=FolderName & "" & "011000 - summary.doc"
objWord.Selection.Range.Subdocuments.AddFromFile Name:=FolderName & "" & "012100 - allowances.doc"
objWord.Selection.Range.Subdocuments.AddFromFile Name:=FolderName & "" & "012200 - unit prices.doc"
objWord.Selection.Range.Subdocuments.AddFromFile Name:=FolderName & "" & "012300 - alternates.doc"
objWord.Selection.Range.Subdocuments.AddFromFile Name:=FolderName & "" & "013100 - Project Management and Coordination.doc"
'Step 6 - This returns word to outline view and prepares to save the file
objWord.ActiveWindow.ActivePane.View.Type = wdOutlineView
If objWord.ActiveWindow.View.SplitSpecial = wdPaneNone Then
objWord.ActiveWindow.ActivePane.View.Type = wdNormalView
Else
objWord.ActiveWindow.View.Type = wdNormalView
End If
objWord.ActiveWindow.ActivePane.View.Type = wdOutlineView
'Step 7 - This saves the file to User Defined File Name
MsgBox "Next Select Project Folder and Master Specification File Name"
'Step 8 - this Stops, Quits and releases the Word Application
objWord.ActiveDocument.Close
objWord.Quit
Set objWord = Nothing
End Sub