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
 

juddaaaa

Board Regular
Joined
Jan 4, 2020
Messages
207
Office Version
365
Platform
Windows
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
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,325
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"
 

Gregm66

Board Regular
Joined
Jan 23, 2016
Messages
170
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
 

Forum statistics

Threads
1,085,849
Messages
5,386,331
Members
401,995
Latest member
mg07p929

Some videos you may like

This Week's Hot Topics

Top