Link to home
Start Free TrialLog in
Avatar of john thornton
john thornton

asked on

word 2013 vba share macros

Word 2013; VBA. I have a series of macros that I would like to use on another computer. (All the macros are initiated by a form.)  All I need to do is initiate the form and I am off and running.

I cannot change the other computer's normal.dotm file. So I put the macros in another dotm file (I'll call it mydotm.dotm), which I can make available on the other computer.

On that other computer, I open mydotm.dotm. I can see my macros there. In another window, I open the document on which I wish to use the macros. But from there I cannot see (View/Macros) the macros that are in my mydotm.dotm file. All I get is a blank slate.

I'm stuck.

So I thought if I started with the mtdotm.dotm file active (and the other file open in another window), initiated the startup macro from there, had the macro first switch to the other document, then I could continue with the macro execution there.

As the first statement in the startup macro, I used      ActiveWindow.Next.Activate to switch to the other document. That blows up with an error 91 - "Object variable or With block variable not set" message.

Huh?

Am I taking the wrong approach, or is my switch document command wrong?

Can you point me in the right direction, please?

Thank you.
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

If the Word documents and templates are opened manually, they should all be in the same instance of the application. This means that you should be able to see their projects in the VBA IDE. You will also be able to see the project for any attached templates which will include the Normal template. It is likely that that is the only one.

Programatically, It is possible to get extra instances of the application running but that is likely to give some problems, because the Normal template will be attached to each and it shouldn't be shared.
You can use the existing application to open multiple documents and templates via File/Open.

In VBA, it is best to capture the document object and then work on that. For speed we can sometimes refer to the ActiveDocument, but in well-formed code there shouldn't be any need to activate each document that you need to work on.

Sub EditTextFile()
    Dim strFilePath As String
    Dim doc As Document
    
    strFilePath = "C:\MyFolder\MyFile.Txt"
    Set doc = Documents.Open(strFilePath)
    With ActiveDocument.Range.Find
        .Text = "my word"
        .Replacement.Text = "new word"
        .Execute Replace:=wdReplaceAll
    End With
End Sub

Open in new window

Avatar of john thornton
john thornton

ASKER

VBA IDE - ?

That's what I thought. But when I switch to the document and click View/Macro, I do not see any macro names. When I click the Organizer, all active templates and documents, it shows that my normal.dotm is available, but not mydot.dotm.

For some reason, it is not seeing mydot.dotm. Could it be a security issue?
In the trust center, Options/Trust Center/Trust Center Options/Trusted Locations, I have added the folder in which mydotm.dotm resides.

Oh; attached is the  macro organizer I referenced earlier.
Have you checked in the Task Manager to make sure that you only have the one instance of Word running?
It might help it you post a sample of your code.
Yes, I have only one instance of Word running. I will try to prepare and upload a version of the code tomorrow, but much of it is client confidential. Though I am not certain how the code itself would help. The code works; only the sharing of it does not. It must be some kind of security or configuration problem. But then, I am just a newcomer. My expertise is... far from expert.

Very far.

Thanks/
Perhaps we should skip the code, then. I was trying to get a better picture of what you are trying to do.

The usual way of working with template is to create a new document from the template. If that is done, the template becomes 'attached'. Subsequently, the macros and styles of a document's attached template are available to the document. The attached template can be changed via the Developer tab, Document template button.
I understand the concept. But I do not understand how a document becomes "attached" to a template, or how to create a document from the template.  (That may be documented somewhere... but I have been unable to find where it is.)

But that is not possible in my case. The document was created earlier by the user, on the user's computer. I will not have access to the user's document or their computer.

I am trying to make the macros available in the user's document . (This is a situation that will occur numerous times.)

Thanks for your attention to this matter.
The template is normally attached by doing File/New and choosing the template. However the template doesn't have to be attached. It's code can work on any document in the same instance of the application.

It the question you say: "In another window, I open the document".
What other window? Where does it come from?

Try opening the template (File/Open). Use Alt + F11 to see the VBA IDE and assure yourself that you can see the code. Switch back to the same document viewing window that should now be displaying your template document (probably devoid of text), and then open the document that is to be worked on. The application should now have two document and the VBA window available for you to switch between, You should be able to see at least three projects in the VBA IDE - Normal, your code template's, and the document's.
I think that's what I did.

1.  I open the template; Alt-F11. Yes, I can see the macros. Alt-F11 to return to the Word editor.

2.  I open the user's document (File/Open). It opens the user's document in another window.

3.  Alt-F11 to show the macro editor - see figure1.jpg (attached). I see Normal and  2 projects: my template the the user's document. Note that the reference in the user's project refers to Normal. (I have no macros in normal.dotm.) Shouldn't it refer to mydotm.dotm?

4.  Alt-F11 to return to the use's document. File/View/Macros - figure2. jpg The option is "All templates and active documents." And yet, it shows no macros. The drop-down list shows normal.dotm and the user's docx fie. But it does not rec  ognize mydotm.dotm, which is also open. However, when I switch to mydot.dotm window, and click File/View/Macros, it shows the macros in mydotm.dotm. (figure3.jpg) So the macros are there, but in the user's document, I cannot get to them.

Thus, my confusion.

Thanks.

User generated image
User generated image
User generated image
OK, I got what VBA IDE stands for. No big deal. I just never saw the macro editor called that.

Thanks.
I added the file open macro command to mydotm.dotm:

Sub start_here()
    Dim strFilePath As String
    Dim doc As Document
 
    strFilePath = "C:\myfolder\myresume.docx"
    Set doc = Documents.Open(strFilePath)
End Sub

Indeed, it opens myresume.docx. Should that "attach" the document to mydotm.dotm?

With myresume.docx active, File/View/Macros... I still see no macros.

Still stuck.

Thanks,
I added the .show command to the file open macro.

Sub start_here()
  Dim strFilePath As String
  Dim doc As Document
   
  strFilePath = "C:\myfolder\myresume.docx"
  Set doc = Documents.Open(strFilePath)
  macroform.Show       ' display the macro menu form
End Sub

The form runs the whole show:

User generated image
From there, the user can click buttons to execute the macros that are in mydotm.dotm

(If I cancel the form and click File/View/Macros, it still snows no macros.)

And that meets my objective. That does what I want.

Now I have to modify the start_here() macro allow the user to identify the name of the file he/she wishes to open, and  I think that will make this thing workable.... even though the issue of recognizing the mydotm.dotm file remains unsolved.

Do you agree?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. I had trouble earlier passing variables from one macro to another. I'll have to work on that.

I have used neither the Word Dialog or the Office File Dialogue. Yet another challenge awaits me.

So this leaves the mystery unsolved, but the problem adequately addressed.

Thanks,