1

How ThisDocument object refers to the docm file contents, which containing the code?

I had insert a module under the *.docm project which contains this code:

Sub test()
   Debug.Print ThisDocument.Name
End Sub

Returns Normal.dotm as result.

But it is expected returning the current docm file's name.

How can set 'Thisdocument' Object subroutines refers to the current docm file?

Any help would be greatly appreciated.

braX
  • 11,506
  • 5
  • 20
  • 33
mgae2m
  • 1,134
  • 1
  • 14
  • 41
  • From the tagging and the question it's not clear exactly what you have. This code is running in Excel? But the code is working with Word? `ThisDocument` is an object in the Word object model, not in Excel, and refers to the "code container". – Cindy Meister Jul 06 '19 at 13:34
  • @CindyMeister@: Actually you right. I was edited my question. – mgae2m Jul 07 '19 at 07:31
  • 2
    Are you absolutely certain `Sub test` is in that docm Project and not in the Normal project? In the VB Editor click in the Sub then look in the Project window (usually to the left, where Normal is certainly one of the projects listed). By default, Normal is where Word stores new macros, so code often ends up there instead of where the coder thinks it is... – Cindy Meister Jul 07 '19 at 07:38
  • It may happens. I'm not at work and that project is at my work. I will discover tomorrow. – mgae2m Jul 07 '19 at 07:45

2 Answers2

1

Get the name of the Excel file which contains the code you are running:

ThisWorkbook.Name

Get the name of the Excel file that your code is currently referencing:

ActiveWorkbook.Name

If you are running code from a Word doc that is referencing an Excel file, you would use ActiveWorkbook.Name.

This post goes into more detail on ActiveWorkbook vs ThisWorkbook. Difference between Thisworkbook.name and Activeworkbook.name in VBA

Jenn
  • 612
  • 1
  • 4
  • 7
  • Excuse me. I was wrong in my question. my mean was `docm`, but because some mind confuse! I typed `xlsm` at wrong. I apologize – mgae2m Jul 07 '19 at 07:38
1

As commented by Cindy Meister, From the tagging and the question it's not clear exactly what you have (or have in your mind). However assuming that you are aiming something highly tricky and unconventional with both Excel & Word applications involved. In normal condition ThisDocument will never refer to Normal.dotm(from a module under the *.xlsm project) but since you made it happen and also aim to "set Thisdocument Object refers to the current xlsm file", I would have try it like this from a module in xlsm file.

Global ThisDocument As Object
Sub calltest()
Dim wd As Word.Application
Set wd = CreateObject("Word.Application")
wd.Visible = True                 'for demo only
Set ThisDocument = wd.Templates(1)   'This will result Normal.dotm (as you are experiencing - may be similar code is already in the calling routine)
test
Set ThisDocument = ThisWorkbook     'This will result workbook Containing the module with code
test
End Sub
Sub test()
Debug.Print ThisDocument.Name
End Sub

Obviously it is assumed Reference to Microsoft Word XX Object Library had aleady been taken.

Ahmed AU
  • 2,757
  • 2
  • 6
  • 15
  • Excuse me. I was wrong in my question. my mean was `docm`, but because some mind confuse! I typed `xlsm` at wrong. I apologize – mgae2m Jul 07 '19 at 07:38
  • 1
    Never mind. It happens to all of us. But one thing for sure, I have learned greatest techniques from silliest mistakes. Hope problem resolved with Cindy Meister ‘s comments. – Ahmed AU Jul 07 '19 at 08:57