Consulting

Results 1 to 4 of 4

Thread: Insert text from a file

  1. #1

    Insert text from a file

    Hello all,

    So I'm trying to insert text from a file into an email with a macro, the macro needs to open the file view to a certain folder and insert text from whatever file is selected by the user. I've managed this in word with the following code

    Sub INSERT_TEST()
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    'get the number of the button chosen
    Dim FileChosen As Integer
    fd.InitialFileName = "S:\Clause bank"
    FileChosen = fd.Show
    If FileChosen <> -1 Then
    'didn't choose anything (clicked on CANCEL)
    MsgBox "You chose cancel"
    Else
    'Insert the file contents
    Documents.Open FileName:=fd.SelectedItems(1), ReadOnly:=True
    End If
    End Sub

    Now this doesn't work at all in outlook, but it gives you an idea of what I'm trying to get out of the new macro for outlook. Not having much luck looking this up online, but will keep trying while awaiting a response on this.

    Thanks

  2. #2
    depending on version, outlook has no getopenfile name or other dialogs to select files

    you would need to use an API file dialog to do this
    OR
    automate word or excel to use their built in dialogs from outlook, requires word or excel to be installed

    later versions of outlook probably have built in file dialog which should be easily adaptable to your code, apart from documents.open

    to open and read text file

    f = freefile
    open "filename\path" for input as f
         mystr = input(lof(f), #f)
    close f
    mystr now contains the content of the textfile, to insert where ever in your email

  3. #3
    Hello, thanks for your advice on this. This is where I have got to, trying to adapt some code I found online.

    Function GetFolderPathWithWord()
            Dim objWord As Word.Application
            Dim dlg As Office.FileDialog
            Dim lngWidth As Long
            Dim lngHeight As Long
            Dim objOL As Outlook.Application
            Dim objOLWindow As Object
            Dim FileChosen As FileDialog
            
            
            ' get active Outlook window
            Set objOL = CreateObject("Outlook.Application")
            Set objOLWindow = objOL.ActiveWindow
            
            ' start Word if necessary
            Set objWord = New Word.Application
            If objWord Is Nothing Then
                Set objWord = CreateObject("Word.Application")
            End If
            If Not objWord Is Nothing Then
                ' get current Word window dimensions
                lngWidth = objWord.Width
                lngHeight = objWord.Height
                ' reduce size of Word window
                objWord.Width = 0
                objWord.Height = 0
                ' switch to Word
                objWord.Activate
                
                ' show file dialog and handle users choice
                Set dlg = objWord.FileDialog(msoFileDialogFilePicker)
               dlg.Show
                With dlg
                    .InitialFileName = "S:\Clause bank"
                If .Show = -1 Then
                    GetFolderPathWithWord = dlg.SelectedItems(1)
                Else
                    GetFolderPathWithWord = "User canceled dialog"
                End If
            End With
        Else
            GetFolderPathWithWord = "Could not open Word"
        End If
        
        ' see if any Word documents are open and if not, quit Word
        If objWord.Documents.Count = 0 Then
            objWord.Quit
        Else
        
        ' return Word window to normal dimensions
        objWord.Width = lngWidth
        objWord.Height = lngHeight
        End If
            ' return to active Outlook windown
            objOLWindow.Activate
            
            
           If dlg.Show = -1 Then
    'didn't choose anything (clicked on CANCEL)
    MsgBox "You chose cancel"
    Else
    'Insert the file contents
    Selection.InsertFile FileName:=dlg.SelectedItems(1)
    End If
    
            
        ' dereference objects
        Set objWord = Nothing
        Set dlg = Nothing
        Set objOL = Nothing
        Set objOLWindow = Nothing
    End Function
    It keeps stopping on "objword.activate" give the error "cannot activate application". Trying to figure out why but no luck so far...

  4. #4
    It keeps stopping on "objword.activate"
    not required anyway
    you may need objwork.visible = true at the same place in code

    i can not tell from the code if you are doing this in outlook or word, though i guess outlook

    If dlg.Show = -1 Then
    'didn't choose anything (clicked on CANCEL)
    MsgBox "You chose cancel"
    Else
    'Insert the file contents
    Selection.InsertFile FileName:=dlg.SelectedItems(1)
    End If
    dlg is probably out of scope as you already closed word, else would duplicate the opening of the dialog anyway,
    selection is unqualified and may refer to anything that can be selected, presumably in outlook
    you should use getfolderpathwithword (if it is not one of the error strings) as the filename to insertfile,
    or the filename to open file and read contents as posted above

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •