Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select file in WORD 1

Status
Not open for further replies.

wmbb

Technical User
Joined
Jul 17, 2005
Messages
320
Location
NL
I need some help with the code for a macro in WORD.
I need to select a file by a dialog and store the path and filename of the selected file in two variables.
Is it possible ?
 
Hi wmbb,

What is the purpose and is the selected file being opened, or just 'selected'?

Cheers

[MS MVP - Word]
 
Unless you use office2007 you may consider the FileDialog object.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Please I'm a newbee in VBA and I don't know all about the possible codes but what I want is to create a macro to import a selected PDF-file as an object, displyed as icon and the caption-name only the filename.
So I recorded the code and want to replace the filename in the recorded macro by a dialog-box and change the caption-name with the filename.



 
Use the macrorecorder.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Addition to my previous post:

This is the code I have...
Code:
Sub test()
'
' test Macro
' Macro recorded 11-04-2008 by Hans Bolten
'
    Selection.InlineShapes.AddOLEObject ClassType:="AcroExch.Document.7", _
        FileName:= _
        "R:\SEM\Analyseresultaten\2008.1383\2008.1383 location a.pdf", _
        LinkToFile:=False, DisplayAsIcon:=True, IconFileName:= _
        "C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-A70900000002}\PDFFile.ico" _
        , IconIndex:=0, IconLabel:="2008.1383 location a.pdf"
End Sub

I want to replace "R:\SEM\Analyseresultaten\2008.1383\2008.1383 location a.pdf" with the file selected in the dialog and the IconLabel "2008.1383 location a.pdf" with the filenaam of the selected file without the path.

 
Hi Hans,

You don't really need a macro to do what you're asking - it can all be done through the standard Insert Object dialog box, which gives you the choice of what caption to use.

Assuming you're wedded to using a macro, it seems you don't need to store the path in a variable at all (see your original post) - just the filename.

Cheers

[MS MVP - Word]
 
I know this seems a little bit overdone but this will be a part of a bigger macro to ease some options for users who are not every-days users.
So please can you help me with this...?
 
Hi Hans,

Here are two macros to get you started. I haven't figured out how to get the 'correct' icons for the file types but, since you're embedding the objects, that may not matter. The first macro inserts the filename with the extension (eg '.pdf'), the second macro removes the extension. As a bonus, you can select more than one file at a time.
Code:
Sub InsertObject1()
Dim FoundFile As Variant
Dim vName As Variant
'create FileDialog object as File Picker dialog box
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
  'use Show method to display File Picker dialog box and return user's action
  If .Show = -1 Then
    'step through each string in the FileDialogSelectedItems collection
    For Each FoundFile In .SelectedItems
      vName = FoundFile 'gets new filepath
      Selection.InlineShapes.AddOLEObject FileName:=vName, LinkToFile:=False, _
        DisplayAsIcon:=True, IconLabel:=Split(vName, "\")(UBound(Split(vName, "\")))
    Next
  End If
End With
End Sub

Code:
Sub InsertObject2()
Dim FoundFile As Variant
Dim vName As Variant
Dim strName As String
'create FileDialog object as File Picker dialog box
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
  'use Show method to display File Picker dialog box and return user's action
  If .Show = -1 Then
    'step through each string in the FileDialogSelectedItems collection
    For Each FoundFile In .SelectedItems
      vName = FoundFile 'gets new filepath
      strName = Split(vName, "\")(UBound(Split(vName, "\")))
      strName = Left(strName, Len(strName) - 4)
      Selection.InlineShapes.AddOLEObject FileName:=vName, LinkToFile:=False, _
        DisplayAsIcon:=True, IconLabel:=strName
    Next
  End If
End With
End Sub
Cheers

[MS MVP - Word]
 
Thanks macropod !

Almost perfect....
I have used the first code and it works fine for a pdf-file and for a xls-file but not for a txt-file or jpg-file !

Choosing a txt-file WORD is looping in the program and I need to end the program in the file manager.

Choosing a jpg-file I get an error:
The server application, source file, or item can not be found
Make sure the application is properly installed, and that it hasn't been deleted removed or renamed.


What is going wrong with those types of files ?
 
Hi Hans,

Try replacing:
Code:
      Selection.InlineShapes.AddOLEObject FileName:=vName, LinkToFile:=False, _
        DisplayAsIcon:=True, IconLabel:=strName
with:
Code:
      On Error GoTo Alternate
      Selection.InlineShapes.AddOLEObject FileName:=vName, LinkToFile:=False, _
        DisplayAsIcon:=True, IconLabel:=strName
      GoTo NextShape
Alternate:
      Selection.InlineShapes.AddOLEObject ClassType:="Package", FileName:=vName, _
        LinkToFile:=False, DisplayAsIcon:=True, IconLabel:=strName
NextShape:
Cheers

[MS MVP - Word]
 
Macropod, Thanks for your quick response.

On my other PC, script 1 works fine for all file-types so I think it has something to do with the installation of WORD ??!!
If I have the solution I will let you know...


One last option I would like to build-in;

Is there a possibility to set the default folder in the dialogbox to select from to "R:\group20\word\attachments\" ?

 
Hi Hans,

Try adding:
Code:
  .InitialFileName = "R:\group20\word\attachments\"
after the 'With Application' line.

Cheers

[MS MVP - Word]
 
This works fine !!!

Thanks for your help, A STAR for you !!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top