Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    gbroomfield
    Guest

    Using code in another template w'out setting a Ref

    Does anybody know how to access code in a separate Word template without manually setting a Reference? I want to check if a template exists and run code in that template only if it exists. As it may not always exist, I have found that setting a Reference to a missing template sometimes causes problems. Using code to set the reference only once I had determined that the template existed would solve my problem. Thanks.

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using code in another template w'out setting a Ref

    Hi,

    You can check if a file exists by a "dir" command:

    if dir("c:mydocumentssomething.dot") <> "" then
    msgbox "something.dot exists"
    end if

    But can you set a reference by code? I wasn't aware that it could be done.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    gbroomfield
    Guest

    Re: Using code in another template w'out setting a Ref

    Thanks Geoff but I was already using that technique to determine if the template existed. My problem is that to call the code in the template I need to set a reference to it, but if the template has not been loaded on the machine, I do not want to run the code and the Missing Reference causes other strange problems. It would be better to either find a way to run the code without a Reference being set, or set a Reference with code only after the presence of the template has been detected.

    Thanks Greg.

  4. #4
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using code in another template w'out setting a Ref

    Greg,

    I don't quite understand what you mean by "if the template has not been loaded on the machine".

    Do you want to determine if a template is open (you could use "For Each aDoc In Application.Documents" and examine aDoc.AttachedTemplate)? Or do you want to determine if it is in the startup path (ie, an add-in- you could use "For Each aAddIn In AddIns")? Or do you want to know if the macro exists in the template? Or something else?

    I haven't been able to set a reference by code, so if you can do it, I'd be glad to see it (that's the value of this forum- everybody can learn something).

    It's possible to run code by"
    Application.Run Macroname = "myMacro"

    without setting a reference, if that's any good.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Using code in another template w'out setting a Ref

    Hi,

    Would the "Installed" property help in this case?
    From Word VBA help:

    <hr>True if the specified add-in is installed (loaded). Add-ins that are loaded are selected in the Templates and Add-ins dialog box (Tools menu). Read/write Boolean.

    Note Uninstalled add-ins are included in the AddIns collection. To remove a template or WLL from the AddIns collection, apply the Delete method to the AddIn object (the add-in name is removed from the Templates and Add-ins dialog box). To unload all templates and WLLs, apply the Unload method to the AddIns collection.<hr>
    And the following example is given, which may be relevant:

    <hr>If AddIns("Dot1.dot").Installed = True Then _
    StatusBar = "Dot1.dot is loaded"
    <hr>

  6. #6
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using code in another template w'out setting a Ref

    In Excel I use the following to do what you are asking:

    <pre> On Error Resume Next
    Cancel = Run(sPgmBook & "!AI_Workbook_BeforePrint", Cancel)
    On Error GoTo 0
    </pre>


    Where sPgmBook is the filename of the workbook containing the macro. I do not know if a similar capability exists in Word, or if the file containing the macro can be a template.
    Legare Coleman

  7. #7
    Star Lounger
    Join Date
    Jan 2001
    Location
    L.A., California
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using code in another template w'out setting a Ref

    This is a real problem, as I believe Word VBA will not let you modify the references of a "running" document (you have code in a document which is trying to modify the references collection of the same document). I forget the exact error, but it definitely pops one up.

    I had the same problem, as several of my documents needed a reference to a "global" template, which contained general code and forms which I didn't want to duplicate in every document, but just "use" from the documents. Due to the quirkiness of Word, the references often got "lost", and would have to be re-done manually, after we got an error running the document. I tried code to auto-reference a document, but as I mentioned, it gave an error.

    The solution I found was to write a document whose only purpose was to copy the desired document(s) from our test environment to the production environment. During the copy, it would open the desired document, loop thru the references collection, take out the old or missing ones, add the desired ones, and finally save and close the desired document. This is perfectly fine, as long as you are not trying to modify the references of the document which is running the code.

    You would use code similar to the following, perhaps in a button click event:

    dim RefDoc as string 'Doc you want to reference
    dim MyDoc as string 'Doc you want to add the reference to
    dim Doc as document

    if dir$(RefDoc)>"" then
    Set Doc = Documents.Open(FileName:=MyDoc)
    Doc.VBProject.References.AddFromFile RefDoc
    Doc.Save
    Doc.Close
    end if

    In order to execute this code, you have to have a reference to the Microsoft Visual Basic 6 Extensibility Layer. I'm not sure if this means you actually have to have VB6 installed on your system or not.

    Feel free to contact me if you need the actual code to check out.

  8. #8
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Using code in another template w'out setting a Ref

    I'm a neophyte at VBA so please forgive me if I'm showing off my ignorance.

    You can run code in the other template by adding it to the Add-Ins installed (using the code in your template to do this). Once you have run the code, you could uninstall the template as an Add-in.

    The syntax is something like:
    Application.Run Macro:=macroname

    If you need the exact syntax on that let me know and I'll find it for you. I usually just copy it from my junk collection. It runs the code in an Add-In or normal.dot without there being a formal reference.

    Hope this helps,
    Charles Kyle Kenyon
    Madison, Wisconsin

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using code in another template w'out setting a Ref

    If AddIns(strTargetFolder & Application.PathSeparator & strFileArray(lngInd, 0)).Installed


    Above is a snippet from my "Installer" template. Once I have unzipped files/templates, I want to make sure that the template appears in the addins list.

    I'm not sure if this is anywhere your need for checking if a template (and hence a procedure within that template) is available.

    Let me know if you think this sort of test might be useful and I'll try to craft a real example to post here.

  10. #10
    gbroomfield
    Guest

    Re: Using code in another template w'out setting a Ref

    Thanks Geoff

    Using Application.Run Macroname:= "myMacro" nearly works but seems to not quite do what I want. The code I want to run is in a template document that may or may not exist on a particular users computer. If it does not exist I do not want the code to run but that is not a problem. What is the problem now is that the code I want to run requires parameters passed to it.

    Your suggestion works if I call a sub without any parameters. I was using a function, but using a sub passing pointers to parameters should be okay. However as soon as I try to pass a parameter as per the Word 2000 help for the Run Method, I get the below error message

    Run time error '438':
    Object doesn't support this property or method

    I get this error message using this code:
    Application.Run macroname:="'MyMacro.dot'!MyModule.MySub", varg1:= MyVariant

    This code works okay (but without parameters):
    Application.Run macroname:="'MyMacro.dot'!MyModule.MySub"

    This is the simple structure of the sub thats works okay:
    Sub MySub()
    MyCode
    End Sub

    As soon as I give the sub a parameter I get the error:

    Run-time error '-2147352573 (80020003)':
    Unable to run the specified macro

    Do you have any more clues on calling a function or sub in an un-referenced Word template that can pass parameters to the sub?
    Many Thanks
    Greg

  11. #11
    gbroomfield
    Guest

    Re: Using code in another template w'out setting a Ref

    Thanks Chris

    Finding out if the template exists on the user's machine is not the difficulty. The problem has been that if the template file has never been placed onto a particular user's computer then I do not want any problems to occur, I just want the additional functionality provided by that template to be ignored.

    However running a function or sub in that template, if it does exist, without manually setting a reference is the challenge. Elsewhere in this thread a way to do this was suggested, but it seems to only work when no parameters are required by the sub. Therefore I am still looking for a way to run a sub in an un-referenced template and be able to pass parameters to it.

    Many Thanks

    Greg

  12. #12
    gbroomfield
    Guest

    Re: Using code in another template w'out setting a Ref

    Thanks Chas

    I have tried out your suggestion, but it does not seem to allow the passing of parameters to the sub that I want to call. Unless of course I am missing something. I would appreciate the exact syntax particularly with respect to passing parameters. The Word 2000 help indicates that it is possible to pass parameters but I could not get it to work. I have discussed the error messages etc elsewhere in this thread.

    If you have an answer for me I would appreciate it.

    With thanks

    Greg

  13. #13
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Using code in another template w'out setting a Ref

    Hi Greg,

    Just checking on some terminology: you're mentioning a template "existing" on a user's machine - "existing" is not the terminology with which this would get addressed.

    A template can "exist" on a user's machine in one of three states:

    o It exists in a directory somewhere on the user's machine, but is neither "added" nor "loaded" (it will not show up in the "Global Templates and Add-Ins" list in Tools>Templates and Addins).

    o It is "added" to the Add-Ins collection, but is not "loaded" - this corresponds to a template that is listed under "Global templates and add-ins" )in Tools>Templates and Add-ins), but that does not have a checkmark next to it.

    o It is both "added" and "loaded" (added into the Add-ins collection, and loaded into active memory). The "Installed" property mentioned in earlier posts, refers specifically to whether the template is "loaded", as opposed to whether it "exists".

    A template can "exist" in any of the above three states, but can only be accessed for your purposes if it is both added and loaded a/k/a "Installed".

    You should be able to set a reference to a template that is loaded - like Geoff, I've never done so programatically so if there is a way to do this, let us know.

    If there is a way to do that, then you should be able to programatically determine whether the template is loaded, and set a reference only if it is.

    If all of the above is restating the obvious, I apologize - it just seemed the discussion is being led offtrack by discussing the template's status in terms of whether it "exists".

    Hope this helps,
    Gary

  14. #14
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using code in another template w'out setting a Ref

    Greg,

    I don't pass parameters directly.

    What I've done is to have public variables at the start of my routine, ie, scoped for the project. In my case, I'm calling routines from Normal.dot. I'm calling a routine via an add-in- the routine I'm calling has a reference back to Normal.dot, and is able to reference the public variables in Normal.dot.

    I'm probably going to get some flack for using Nornal.dot at all. But I have in there a single module with shell calls to the add-in. When I open a new template, I want it to execute the "AutoExec". I can get the template to execute and AutoExec in Normal.dot but not in the add-in. So the "AutoExec" has a single "Application.Run" which is then executed from the add-in.

    The add-in manages Normal.dot, and updates the code that shell if necessary- so I don't have to copy normal.dot to the user's machine.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  15. #15
    gbroomfield
    Guest

    Re: Using code in another template w'out setting a Ref

    Hi Geoff

    I can't use Normal.dot because we use other software that is very sensitive to Normal.dot and may return it back to a default state. Yes I suppose I could use public variables. It is just that I try and educate others to rarely use public variables and I therefore didn't actually try it that way, but that may be the simplest way.

    Thanks

    Greg

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •