Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with referencing a file 1

Status
Not open for further replies.

engrfurqan

Programmer
Jan 5, 2009
20
I have a strange problem with file reference. I am trying to reference a word template which has some macros that I want to use. The problem is that whenever I try to reference this file the link got broken even though the file is present in the specified location but vba start to look for the file in Temp folder and file name changes to ~WRC0003.tmp. Can anybody help me out?
 
Try posting some code.

"vba start to look for the file in Temp folder and file name changes to ~WRC0003.tmp. ?"

1. how do you know that is looking in the Temp folder?

2. WHAT file "changes"....when you state it is not making a reference? Where are you seeing this "change"?

3. what do you even mean by "reference"? You do not make reference to templates. They are either Attached (to use their styles and procedures), or Loaded as add-ins (to use their procedures).

4. "whenever I try to reference this file the link got broken " Again, please post your code. What are you doing to "reference" the file? WHAT "link"? What do you even mean by "link"?

Normally, if you want to access procedures, you load the template as an add-in - a global template. Once loaded, all its procedures (macros) are available.

Or, you Call the procedure (macro) directly. Are you doing that? That is not a "link".

Try posting some code. Then we may be able to help.

Gerry
 
Thanks for your reply now this is the code

ActiveDocument.VBProject.References.AddFromFile "complete file name as string"


1. how do you know that is looking in the Temp folder?

In tools->References menu you can see the location of referenced files, so there I can see that it is pointing towards Temp folder.

2. WHAT file "changes"....when you state it is not making a reference? Where are you seeing this "change"?

the change is in the project name for instance if I am referencing a file having project name abc.dot then it changes to ~WRC0003.tmp

Maybe I can elaborate what I want to do ... I have some macros written in one template file that is abc1.dot and I want to call these macros from macros present is some other template file that is abc2.dot. Now can you give me appripriate code to do that

Regards,
Furqan



 
Load the file as an add-in.

ActiveDocument.VBProject.References.AddFromFile is NOT the way to access Word template procedures.

Look up Add-ins in Help. The code is there.

Be sure you also look up Installed Property, because you can add an add-in to the available list, but unless you make Install:=True, it is not fully loaded. Once added to the list you can dynamically load and unload access to the macros whenever you want. This is handy, and I would recommend that you DO unload - but you do NOT have to remove it from the list - the add-ins if you not using it.

Further, once added to the list, the syntax for installing/uninstalling is different from adding the add-in in the first place.

It is all there in Help.

Note that add-ins (.dot files used as code containers) do NOT, repeat NOT, have to be in a standard Word folder such as the UserTemplates, or WorkgroupTemplates. An add-in file (a .dot file used as a code container) can be in ANY folder, including network drive folders.

The reason you are seeing the ~WRC0003.tmp is because you are using "AddFromFile". So it is opening the file. You do NOT want to open the file. You want access to the procedures (macros), which again, is done - or at least one way of doing it - by using it as an Add-in. NOT a Reference. It is not a Reference. DOT files are not References. References, for the most part, are DLL files.

Gerry
 
Which help you are talking about?

thnaks,
Furqan
 
In the VBA help, topic: AddIns, collection.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yes, in Word go the VBE (Visual Basic Editor) and type "Add-in" into the Help box at the top right (usually). Select Add-in Object from the Help choices. Read it. Sample code is there.

NOTE: make sure you use Help from the VBE,- i.e. VBA Help - not Help from the document.

If you put "add-in" in Help from the document, you get help on using the menus to load or unload global templates (add-ins).

If you put "add-in" in Help from the VBE, you get help on loading and unloading global templates (add-ins) programatically, which, it seems, is what you want.

Gerry
 
I have loaded the the template and installed it but I cant find any code to run the macro, I mean if I reference the file I just need to write the name of macro to run it.

Also if I have some function which returns some value then how to use this addins to have that value. for instance im using the code below to get that value from the function when it is been referenced

sub module()
dim test as string
test = Module1.main
end sub

in the similar way I can pass the values to the function. I dont know how should I do this using addins?
 
I have loaded the the template and installed it but I cant find any code to run the macro,"

If this is true, then you can call any procedure by name.

" I mean if I reference the file I just need to write the name of macro to run it. "

I still do not know what you mean by "reference the file".

I will say it again. If you load and install a .dot file as an add-in (a global template), then any procedure in that .dot file is available by name.

sub module()

This is a BAD name.

test = Module1.main

What is Module1.main? This means nothing as you have posted it.

Say you have a .dot file (DemoMe.dot), and it has a Sub named "Blah" in a standard module. DemoMe.dot is in the folder c:\zzz\. The following code will:

1. add the add-in to the available list
2. install it
3. fire the Sub Blah that is in DemoMe.dot
Code:
Sub Install_And_Execute()
   AddIns.Add "c:\zzz\DemoMe.dot", Install:=True
   Application.Run Macroname:="Blah"
End Sub

Gerry
 
test = Module1.main

the above statement means, main is a function inside module named Module1 and it returns a value which is stored in test

your code is working fine,thanks, but what if I want to pass some value to function and function is returning a value. What we usually do when fucntion is in same file

ReturnStr = ReturnValue(InputStr)

the function definition for ReturnValue would be:

Function ReturnValue (InputStr As String) As String
 
but what if I want to pass some value to function and function is returning a value. "

Huh? It works just like the normal passing of arguments and parameters. Please put your cursor over "Run" in my example and press F1 - it gives help on the Run method, including the use of arguments.

Just to repeat, I have changed the Sub in the .dot file I am loading and installing as a global to exactly your names.
Code:
[COLOR=red] ' this is a Function in the DemoMe.dot file
' being loaded and installed as a global[/color red]
Function ReturnStr(InputStr As String) As String
ReturnStr = "whatever " & InputStr
End Function

[COLOR=red]' this is a Sub that can be in any module[/color red]
Sub Install_And_Execute()
   AddIns.Add "c:\zzz\DemoMe.dot", Install:=True
   MsgBox Application.Run(Macroname:="ReturnStr", _
         varg1:=InputBox("type something"))
End Sub

It will:

1. add the add-in to the available list
2. install it
3. fire the Sub Blah Function ReturnStr that is in DemoMe.dot

Result? #3 has an argument - varg1:=InputBox("type something"). So you get...an Inputbox with the prompt "type something". Suppose you type "yadda yadda".

The result will be a messagebox "whatever yadda yadda"

The "whatever" (hard code in the Function), plus the "yadda yadda" from the Inputbox. Although technically,
Code:
   MsgBox Application.Run(Macroname:="ReturnStr", _
         varg1:=InputBox("type something"))
displays the returned value of the Function ReturnStr.

It works just like any other passing of arguments to and from Functions or Subs.

Hope this helps.

Gerry
 
it worked for me, Thanks Gerry for your help

Regards,
Furqan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top