1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Word 2010 Macro Insert Text from File

Discussion in 'Business Applications' started by CDevore, Dec 3, 2014.

Thread Status:
Not open for further replies.
Advertisement
  1. CDevore

    CDevore Thread Starter

    Joined:
    Dec 3, 2014
    Messages:
    6
    I'm trying to create a macro in Word 2010 that will allow me to insert text from file but pauses when it takes me to the specific folder so that I can select which file to insert. I have a folder containing over 50 files from which I want to choose.

    I created this macro once from directions I found on line but my macros were recently lost and I'm having to start all over. I can't for the life of me remember the code for the macro and I can't even remember what question I asked to get to the code previously!
     
  2. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,553
    First Name:
    Hans
    Hi, welcome to the forum.
    I don't have an answer for you yet but I do have a question: will the whole text from that wile be inserted?

    Have you tried recording a macro to do just that? It will give you some idea how it works anfter you finish recording.

    The starting document, is that a template or just a blank doc you start all clean?
     
  3. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,553
    First Name:
    Hans
    Try this code
    I use it with Excel, made some small changes and it works here (alwo Office 2010)
    Code:
    Option Explicit
    
    Public Sub InsertTextFile()
    '
    ' InsertTextFile Macro
    Dim myFile  As String
    myFile = ChooseFile
    If Len(Trim(myFile)) > 0 Then
        Selection.InsertFile FileName:=myFile, Range:="", _
            ConfirmConversions:=False, Link:=False, Attachment:=False
    End If
    End Sub
    
    Public Function ChooseFile() As Variant
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    '1) To set the caption of the dialog box,
    ' set the Title property
    fd.Title = "Select the required file"
    '2) Set the oddly named InitialFileName property to
    ' determine the initial folder selected
    fd.InitialFileName = ""
    '3) Set the InitialView property to control how your files
    ' appear on screen (as a list, icons, etc.)
    fd.InitialView = msoFileDialogViewList '       msoFileDialogViewSmallIcons
    '4) To set the filters (you can have as many as you like)
    ' first clear any existing ones, then add them one by one
    fd.Filters.Clear
    fd.Filters.Add "Text file", "*.txt"
    ' fd.Filters.Add "Config files", "*.cfg"
    ' fd.Filters.Add "Text files", "*.txt*"
    
    ' if there's more than one filter, you can control which
    ' one is selected by default
    fd.FilterIndex = 1
    '5) Set the ButtonName property to control the text on
    ' the OK button (the ampersand means the following
    ' letter is underlined and choosable with the ALT key)
    
    fd.ButtonName = "Open file"
    
    'the number of the button chosen
    Dim FileChosen As Integer
    
    FileChosen = fd.Show
    If FileChosen <> -1 Then
        'didn't choose anything (clicked on CANCEL)
        MsgBox "You mad no choice", vbCritical, ""
        ChooseFile = vbNullString
    Else
        'display name and path of file chosen
        'MsgBox fd.SelectedItems(1), vbInformation, myCaption
        ChooseFile = fd.SelectedItems(1)
    End If
    End Function
    
    Copy and paste the code in the normal.dotm file and it will be avilabe for all documents.

    The filter is set to txt file but you can change ithis.

    Allocate a shortcut to the macro and you can run it an any time.
     
  4. CDevore

    CDevore Thread Starter

    Joined:
    Dec 3, 2014
    Messages:
    6
    Here is what I am trying to do:

    I create travel itineraries for the executives in our office. They fly private. For each leg of the trip I have to insert FBO (fixed base operator) information into the travel itinerary. I have a folder where I have a separate file for each FBO that has the information I want to insert. I have created macros for the ones they fly to frequently that automatically inserts the file. But, I can't create one for each FBO (I have way too many). So, what I want to do is create a macro that will do the steps for Insert, Text from File, take me to the correct directory that I designate in the macro and then stop so I can insert which file I want.
     
  5. CDevore

    CDevore Thread Starter

    Joined:
    Dec 3, 2014
    Messages:
    6
    Thanks, Hans. I'll try it. Can you show me the code without the dialog box? I really just want it to stop so I can choose from the directory the macro has taken me to.
     
  6. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,553
    First Name:
    Hans
    It won't work without the dialog box.
    You place the cursor where you want the text to begin, run the macro, select the foleder and then the file and it's done.

    I don't understand your question.
     
  7. CDevore

    CDevore Thread Starter

    Joined:
    Dec 3, 2014
    Messages:
    6
    I don't want to have to select the folder; it is always the same. I want the macro to take me to the folder and then stop so I can insert the file. Below is a sample of one of the macros I recorded to insert a specific file. This macro will insert information on the Aspen FBO.

    Sub ASE()
    '
    ' ASE Macro
    ' Aspen FBO
    '
    ChangeFileOpenDirectory "E:\Cherie\Flight Operations\FBO's & Inserts\"
    Selection.InsertFile FileName:= _
    "E:\Cherie\Flight Operations\FBO's & Inserts\CO-ASPEN-Atlantic.doc", _
    Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
    End Sub

    I don't know how to write the macro so that it stops (or pauses) once it gets to the "FBO's & Inserts\" folder so that I can then choose which file to insert. I think I need to somehow define a variance as whatever file I choose and then write in the code that the Selection.Insert File Filename:= that variance. I don't know how to define the variance as whatever file I choose. Sorry, if this isn't making sense. I'm trying to speak computer when I'm really not fluent! :(
     
  8. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,553
    First Name:
    Hans
    What you want cannot be doen, the macro runs and dows its work.

    The macro code I sent you is simple, it asks you to select a file,

    So you browse to that folder and slect the file and it inserts it at the spot where the cursor is. No more, no less.

    The only thing is the fildetr is set ot txt files so you won't see and doc or dcox files.

    Eidt the filter section sa foloows:


    old code
    Code:
    fd.Filters.Clear
    fd.Filters.Add "Text file", "*.txt"
    ' fd.Filters.Add "Config files", "*.cfg"
    ' fd.Filters.Add "Text files", "*.txt*"
    
    new code
    Code:
    fd.Filters.Clear
    fd.Filters.Add "Documents", "*.doc*"
    fd.Filters.Add "Text file", "*.txt"
    
     
  9. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,553
    First Name:
    Hans
    Have you booked any results?
     
  10. CDevore

    CDevore Thread Starter

    Joined:
    Dec 3, 2014
    Messages:
    6
    Unfortunately, I'm not explaining myself well. I know it can be done because I had a macro that did it up until this week. I'm going to keep looking and see if I can find the information I had previously. Thanks for all your help.
     
  11. CDevore

    CDevore Thread Starter

    Joined:
    Dec 3, 2014
    Messages:
    6
    I found it! This is what I was trying to do.

    Sub TMInsertFBO()

    '
    Dim fd As FileDialog
    Dim strFile As String
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
    .Title = "Insert File"
    .InitialFileName = "E:\Cherie\Flight Operations\FBO's & Inserts\"
    If .Show = -1 Then
    strFile = .SelectedItems(1)
    Else
    Exit Sub
    End If
    End With
    Selection.Insert
     
  12. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1138596

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice