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
82
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,312
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,082,231
Messages
5,363,872
Members
400,767
Latest member
hnasrat

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top