Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Recommended Posts

Posted

Hi there

 

I'm using PowerShell to generate a report in which I'm creating a title, watermark and inserting a toc. Then i call a VBA script to merge word documents into the current document. However currently I have to manually select the word documents.

I've got two VBA Scripts that both work, Currently use the File selector one which does everything but manually have to select the files. The "No prompt" one creates works great however it creates a new word document and doesn't use the open one.

 

I'm sure is a really quick fix, is there any VB guru that can help with this please?

 

Thanks

 

Codes below

 

No prompt

Sub MergeMultiDocsIntoOne_NoPrompt()

Dim rng As Range
Dim MainDoc As Document
Dim strFile As String
Const strFolder = "D:\Test\" 'change to suit
Set MainDoc = Documents.Add
strFile = Dir$(strFolder & "*.doc") ' can change to .docx
Do Until strFile = ""
Set rng = MainDoc.Range
rng.Collapse wdCollapseEnd
rng.InsertFile strFolder & strFile
strFile = Dir$()
Loop
End Sub[/Code]

 

File selector

[Code]Sub MergeMultiDocsIntoOne()
Dim dlgFile As FileDialog
Dim nTotalFiles As Integer
Dim nEachSelectedFile As Integer

Set dlgFile = Application.FileDialog(msoFileDialogFilePicker)

With dlgFile
.AllowMultiSelect = True
If .Show <> -1 Then
Exit Sub
Else
nTotalFiles = .SelectedItems.Count
End If
End With

For nEachSelectedFile = 1 To nTotalFiles
Selection.InsertFile dlgFile.SelectedItems.Item(nEachSelectedFile)
If nEachSelectedFile < nTotalFiles Then
Selection.InsertBreak Type:=wdPageBreak
Else
If nEachSelectedFile = nTotalFiles Then
Exit Sub
End If
End If
Next nEachSelectedFile
End Sub[/Code]

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...