Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-24-2023, 12:49 PM
Kosmosent Kosmosent is offline Inserting File after another insertfile Windows 10 Inserting File after another insertfile Office 2021
Novice
Inserting File after another insertfile
 
Join Date: Mar 2023
Posts: 2
Kosmosent is on a distinguished road
Default Inserting File after another insertfile

Hello,

I am trying to insert the file after finding text. After inserting first file it will add subsequent files after. However, I am having issues with inserting subsequent files, as it will insert it after the found text. Can anyone help?

Thanks



Code:
Sub InsertSpecs(dWS As Worksheet, findText As String, subPath As String, wdRange As Object, wdApp As Object, chkSpec As String, c As Long)

Set wdApp = CreateObject("Word.Application")
Set compDoc = wdApp.Documents.Open("C:\Users\nguye\OneDrive\Desktop\22 SPECS\SP_TEMPLATE_A10-21-22__2022.docx") 'SSP Template filepath
wdApp.Visible = True

wdApp.ActiveWindow.View.ShowHiddenText = True
Set wdRange = compDoc.Range 'set the search range to include headings

Dim bfound As Boolean, srng As Object, insertRange As Object, looprange As Range
    Dim filePath As String, currentFormat As Long
    
    bfound = False 'set bfound to False by default
    filePath = subPath & dWS.Range("E" & c).Value 'build file path
        
    If Dir(filePath) <> "" Then 'check if file exists in folder
        With wdRange.Find
            .ClearFormatting
            .Text = findText
            .Forward = True
            .Wrap = wdFindContinue
            bfound = .Execute(Forward:=True)
        End With
            
        If bfound Then
            'set srng to the found range and move it down one line
            Set srng = wdRange.Duplicate
            srng.Collapse wdCollapseEnd
            srng.MoveStartUntil vbCr
            srng.MoveStart wdCharacter, 1
    
            'set insertRange to the end of srng
            Set insertRange = srng.Duplicate
            insertRange.Collapse wdCollapseEnd
            
            'insert a new paragraph and the file content
            insertRange.InsertParagraphAfter
            insertRange.InsertFile filePath, "", False, False, False
            insertRange.Select
            c = c + 1
                
            Do Until chkSpec = ""
                If chkSpec = "True" Then
                    filePath2 = subPath & dWS.Range("E" & c).Value
                    If Dir(filePath) <> "" Then
                        'insert a next spec
                        Selection.InsertFile filePath2, "", False, False, False
                        Selection.MoveEnd
                        c = c + 1
                    ElseIf chkSect = "False" Then
                        c = c + 1
                    End If
                    i = c + 1
                End If
            Loop
        End If
    End If
End Sub
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
InsertFile footer does not appear A6c Word VBA 3 08-26-2019 05:54 AM
InsertFile() with literature reference texMan Word VBA 3 04-18-2018 02:18 AM
Inserting File in Powerpoint HowardC PowerPoint 2 09-04-2012 09:08 AM
Inserting File after another insertfile range.insertfile produces diff result between word 2010 and 2007 rsmedley Word 1 02-15-2012 10:48 PM
Inserting File after another insertfile Header/Footer lost with InsertFile cksm4 Word VBA 2 03-07-2011 06:09 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:18 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft