View Full Version : Inserting object, text from file
Kion Zanfret
07-31-2014, 04:46 AM
Hello,
So I've been given the task of making a macro for this, researching into it I find that it's very easy to do if you know what text, from which document, you want pulled through and point the macro to that file directly. However I need it to come up with the browse window so users can select what file they need, also the file window needs to open up into a specific folder each time. Recording a macro doesn't work and I can't find an answer online atm.
Can some give me some advice on this please?
Tinbendr
07-31-2014, 04:56 AM
Search is your friend (http://www.vbaexpress.com/forum/showthread.php?33780-Solved-Open-File-Dialog)
Use ChDrive to change directories before calling the file dialog.
You can also use filesystemobject.
Kion Zanfret
07-31-2014, 06:22 AM
Thanks, I should have thought of using search!
Kion Zanfret
07-31-2014, 07:19 AM
Ok despite using the chdrive + chdir statements before opening file explorer, it isn't setting the explorer to open straight to the directory I want. Also I can't get the last part of my code to run, I thought it would be simple but I'm obviously missing something
Sub ScratchMacroTB()
    Dim pName As String
    Dim bDoc As Document
    Dim aDoc As Document
    Dim AppPath As String
    Set aDoc = ActiveDocument
    With Dialogs(wdDialogFileOpen)
    ChDrive "S"
    ChDir "S:\IT\Macros"
        If .Display Then
            If .Name <> "" Then
                Set bDoc = Documents.Open(.Name)
                AppPath = bDoc.Path
            End If
        Else
            MsgBox "No file selected"
        End If
    End With
   
    bDoc.Active
    Selection.WholeStory
    Selection.Copy
    aDoc.Active
    Selection.Paste
    bDoc.Active
    ActiveDocument.Close
    aDoc.Active
    
End Sub
It might be worth noting that I'm trying to point it to a network drive
macropod
07-31-2014, 04:51 PM
You could use something like:
Sub Demo()
 With Application.Dialogs(wdDialogInsertFile)
   .Name = "C:\Users\" & Environ("Username") & "\Desktop\"
   .Show
 End With
 End Sub
The above will open the dialog with the contents of the Desktop displayed.
PS: Looks like a cross-post of: http://answers.microsoft.com/en-us/office/forum/office_2010-customize/vba-for-office-2010-word-insert-object-text-from/7dd1a51c-fb6d-4026-a834-e969f77caf22
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.