简体   繁体   中英

Use VBA to Insert a Word File From its Shortcut

How can a Word file be inserted programmatically by using a shortcut? Our company needs to mix and match source files into larger release documents, and a single file may be used in multiple release documents.

The approach is to have one copy of a source file. Then, each release document has its own directory containing shortcuts to all the source files it should contain. To sort them in proper order, the shortcut names have a prefix number, which means that the shortcut has a different name from the source file.

This works fine when I use the actual path and file name, but I get an error when using a shortcut: Run-time error '5273': The document name or path is not valid.

I am using the code:

Dim strSourceFolder As String   'location of source files

    ChangeFileOpenDirectory strSourceFolder

    Selection.InsertFile FileName:=  strSourceFolder & "[#-SHORTCUT NAME HERE].docx" _, Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False 

Shortcuts contain the folder and file name (right-click Properties and see Target path) so you have some redundancy. Also, shortcuts do not contain file extensions. Therefore, just leave shortcut name for the FileName:

Selection.InsertFile FileName:="[#-SHORTCUT NAME HERE]" _, 
Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM