How to access another workbook's ThisWorkbook

thatoneguy102

New Member
Joined
Mar 23, 2016
Messages
4
Hello everyone, this is my first post!

I am trying to make some code to copy a module from my add-in, to the active workbook's ThisWorkbook module. I can't seem to figure out how to access it though. Here's the code I'm using:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit;">Dim FName As String

With ThisWorkbook
FName = .Path & "\code.txt"
.VBProject.VBComponents("TestExport").Export FName
End With

ActiveWorkbook.VBProject.VBComponents.Import FName</code>
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
You can try referencing the ThisWorkbook module like the below (on my phone so I can't test)

Code:
ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
 
Upvote 0
You can try referencing the ThisWorkbook module like the below (on my phone so I can't test)

Code:
ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule

This threw a Run Time Error 438 : Object doesn't support this property or method
 
Upvote 0
You can't import a ThisWorkbook module. You need to copy the code text to the existing codemodule.

Why do you need to do this?
 
Upvote 0
You can't import a ThisWorkbook module. You need to copy the code text to the existing codemodule.

Why do you need to do this?

The code I need to copy over has Workbook_Open events, which need to be in ThisWorkbook as far as I know. I am somewhat new to VBA so if you know any other way that I can get a Workbook_Open event to run in something other than ThisWorkbook I'd appreciate that
 
Upvote 0
How did you use this in your code?
Code:
ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
 
Upvote 0
How did you use this in your code?
Rich (BB code):
ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule

I used it like this, as compared to my original post:

Code:
[COLOR=#574123][FONT=monospace]Dim FName As String[/FONT][/COLOR]

[COLOR=#574123][FONT=monospace]With ThisWorkbook[/FONT][/COLOR]
[COLOR=#574123][FONT=monospace]FName = .Path & "\code.txt"[/FONT][/COLOR]
[COLOR=#574123][FONT=monospace].VBProject.VBComponents("TestExport").Export FName[/FONT][/COLOR]
[COLOR=#574123][FONT=monospace]End With[/FONT][/COLOR]

[COLOR=#574123][FONT=monospace]ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").Import FName[/FONT][/COLOR]
 
Upvote 0
My question is really why do you need to do this rather than say using a template workbook that already has the code in it?
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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