Open word docx file from amy user computer

Gregm66

Board Regular
Joined
Jan 23, 2016
Messages
170
Hi everyone,

I have tried a few different ways to get this to work but allas no success so far. My original code involved the complete directory path to open a word file,
similar to my last post, i do not wish to have to put the complete dir path into my vba, as this would involve me changing code for different users computers.
is there away to point to a folder without using the whole dir. see code below, i did try USERPROFILE, but failed couldnt find the path/file

Thanks in advance for any help with this one
VBA Code:
Sub Open_Word_Document()

Dim objWord As Object

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\Users\Users Name\Desktop\My Social Club\clubs constitution.docx"

End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
This will check if the folder and file exist on the users Desktop. If it exists it will open the file. If not it will create the file and then open it.

VBA Code:
Sub Open_Word_Document()

    Dim objFSO As Object
    Dim objWord As Object
    Dim objDoc As Object
    Dim strFolder As String
    Dim strFile As String
  
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    strFolder = Environ$("USERPROFILE") & "\Desktop\My Social Club"
  
    If Not objFSO.FolderExists(strFolder) Then
        objFSO.CreateFolder (strFolder)
    End If
  
    strFile = strFolder & "\clubs constitution.docx"
  
    If objFSO.FileExists(strFile) Then
        Set objWord = CreateObject("Word.Application")
            objWord.Visible = True
            objWord.Activate
            objWord.Documents.Open _
                Filename:=strFile
    Else
        If MsgBox("File does not exist. Do you want to create it now?", vbYesNo, "No such file.") = vbYes Then
            Set objWord = CreateObject("Word.Application")
                  objWord.Visible = True
                  objWord.Activate

            Set objDoc = objWord.Documents.Add
                  objDoc.SaveAs2 strFile
        End If
    End If

    Set objFSO = Nothing
    Set objWord = Nothing
    Set objDoc = Nothing

End Sub
 
Upvote 0
If you store the document in the same folder as the workbook, you can simply use the workbook's path:
VBA Code:
objWord.Documents.Open ThisWorkbook.Path & "\clubs constitution.docx"
 
Upvote 0
Thankyou for your replies, i did eventually figure it out with a work around, i used a userform to display the name of the word file to open then click ok and presto file opens, rather quickly to.
i will add the codes i did below so others can see also....Thanks For your help and input though Juddaaaa & Macropod.

UserForm Code Below..

VBA Code:
Private Sub UserForm_Initialize()
TextBox1.Value = "clubs constitution"
End Sub


Private Sub CommandButton1_Click()
Set wordApp = CreateObject("word.application")
wordApp.documents.Open ThisWorkbook.Path & "/" & TextBox1.Value & ".docx"
wordApp.Visible = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,670
Messages
6,173,704
Members
452,528
Latest member
ThomasE

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top