Good afternoon,
I am wondering how to open the Word file, which has some fixed part of the string in its name.
Prevously I was trying to open in under the fixed name:
VBA Excel problem with opening the Word file
Now, I am going a step further and want to make my file more flexible.
I tried the following code:
Sub Rams()
Dim appWD As Word.Application
Set appWD = New Word.Application
Dim docWD As Word.Document
Dim DocName As String
DocName = "*RAMS*"
Set docWD = appWD.Documents.Open(ActiveWorkbook.path & "\" DocName & ".docx.docm")
appWD.Visible = True
But the debugger sets, that it's the syntax error (some parenthesis is missing here, but I don't know where.
I also tried:
Sub Rams()
Dim appWD As Word.Application
Set appWD = New Word.Application
Dim docWD As Word.Document
Set docWD = appWD.Documents.Open(ActiveWorkbook.path & "\*RAMS*.docx.docm")
appWD.Visible = True
But I am getting pretty much similar error to the previous situation. Theoretically, I know, that these symbols mustn't be there, although I don't know how to write it properly.
UPDATE:
With reference to the hints in the comments I tried sth like this:
Sub Rams2()
Dim appWD As Word.Application
Dim iIndex As Integer
Dim strPath As String
Dim strFile As String
strPath = ActiveWorkbook.path
strFile = Dir(strPath & "*RAMS*.docx.docm")
Do While strFile <> ""
Set wb = Workbooks.Open(filename:=strPath & strFile)
For iIndex = 1 To wb.Worksheets.Count
Set ws = wb.Worksheets(iIndex)
'Do something here.
Next Index
strFile = Dir 'This moves the value of strFile to the next file.
Loop
Set appWD = New Word.Application
Dim docWD As Word.Document
appWD.Visible = True
End Sub
NEXT UPDATE:
I found some solutions here:
https://www.techonthenet.com/excel/formulas/dir.php
and here
https://www.exceltrick.com/formulas_macros/vba-dir-function/
and finally used the following code:
Sub Rams3()
path = ActiveWorkbook.path & "\RAMS*.docm"
File = Dir(path)
Dim appWD As Word.Application
Set appWD = New Word.Application
Dim docWD As Word.Document
If Len(File) > 0 Then
Set docWD = appWD.Documents.Open(File)
appWD.Visible = True
Else
MsgBox ("File Doesn't Exist")
End If
End Sub
Basically the DIR function works correctly as well as the file opening command, but I don't know, why the path is redirected to WINDOWS/System32/ whereas I set the path for my workbook?
Only Word application is being opened without any documents...