PDA

View Full Version : Solved: Copying a style from an Addin



Digilee
10-22-2012, 10:31 PM
Hi,

I have an Addin that is stored in the STARTUP folder, so it shows up when a document is loaded (it has a toolbar of its own). I use the addin to reformat a certain table found in the files. Included in the addin are several styles required by the reformat macro. My question is, how do I get these styles into a document that was not created with a template that contained these styles?

In other words, I want to copy several styles (not the whole set) from the addin.dotm to the ActiveDocument. I tried the following.



Sub CopyStyleFromAddIn()
Dim adin As AddIn
Dim addinPath As String
For Each adin In AddIns
If adin.Name = "Addin.dotm" Then
addinPath = adin.Path & Application.PathSeparator & adin.Name
End If
Next adin
Application.OrganizerCopy addinPath, ActiveDocument.FullName, "SiTableHeader", wdOrganizerObjectStyles
End Sub

This does not give an error when run, and in fact the style dialog box blinks like it is being refreshed, but the style does not show up. When I substitute a target template file, such as "doc3.dotm", it works fine (assuming doc3.dotm exists).

Is this not working because my target is a docx file or is the open document?

Any suggestions?

Frosty
10-23-2012, 02:20 PM
I think there is an easier way to do what you're doing... your addin (which is a global template), is the one containing the CopyStyleFromAddin code, right?

You should just be able to reference it by using the ThisDocument object (which is always the document object from which the calling code is being run).

For example, if you have a style called "SiTableHeader" in your global addin (Addin.dotm located in your Word Startup folder), the following code should work without needing to iterate through the Addins collection to properly identify your global template.

Alternatively, if the style is in some other template from the procedure, you can iterate through the Templates collection instead of the Addins collection (assuming it's a global template) to test for the name... but I don't think you need to do any of that, based on your description of the problem.


Public Sub CopyStyleFromThisDocument()
Dim sStyleName As String

sStyleName = "SiTableHeader"

Application.OrganizerCopy ThisDocument.FullName, ActiveDocument.FullName, sStyleName, wdOrganizerObjectStyles
End Sub

Digilee
10-23-2012, 02:32 PM
Frosty,

Thanks for your reply. I will change to that method. However, it still does not solve the problem of why the style is not being copied to the active document. It does not give an error message, but the style does not show up either immediately or after I save and reopen the file (although I'd rather have it show up immediately).

Thanks,
Lee

Frosty
10-23-2012, 02:36 PM
It's something to do with your ActiveDocument. The code is too simple (and too widely used) to be a problem with the code. I've used .OrganizerCopy in this way for years. So I would guess it has to do with either the style you're attempting to copy or the document you are attempting to copy it into.

My suggestion would be to create a style from scratch in a demo template with just that code. Then attempt to run the code on a blank new document. If it works, then you will have narrowed down the range of possibilities for the failure in your environment.

Digilee
10-24-2012, 01:09 PM
As it turns out, the code worked from the beginning. The Joker who gave me the file I was testing restricted editing permissions, including the available styles (which did not include my new styles, of course). Not sure why the macros did not mention any problem, although I suspect that it copied the styles just fine - I just could not access them because of the permissions.

Thanks for your help, Frosty

Frosty
10-24-2012, 03:39 PM
Lots of things will go wrong with a protected document. And the error messages are not always that helpful... glad it's solved :)