MS Word 2000, XP, 2003

O

Oliver Young

How can I get current document path & file name? Or how can I get
current document content from my COM plug-in?

Thank you.
 
W

Wei-Dong Xu [MSFT]

Hi Oliver,

You can retrieve the path property of the current document. I list the sample code for you.
'Code begin
Application.ActiveDocument.Path
'Code End

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
O

Oliver Young

You can retrieve the path property of the current document. I list the sample code for you.
'Code begin
Application.ActiveDocument.Path
'Code End
Thank you very much for your help. However, I'm using VC++6, and have no
idea how to get (MS Word) Application object from C++.

Best regards.
 
W

Wei-Dong Xu [MSFT]

Hi Oliver,

Thank you for replying!

For the automation of word in VC++, I'd suggest the kb article will help you a lot. Please go to:
238972 INFO: Using Visual C++ to Automate Office
http://support.microsoft.com/?id=238972

After importing the word type library into your c++ project, you can use the get_path() method of CDocument class to retrieve the doc file path.

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
O

Oliver Young

I'm not sure if automation is what I need. I have written MS Word
plug-in (adds toolbar with 1 button on MS Word) in VC++ 6. Now, I have to
get current opened document path (or content), when user clicks on my button
in MS Word.

Thank you.
 
W

Wei-Dong Xu [MSFT]

Hi Oliver,

Thank you for replying!

You are very welcome!

Thank you once more for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Wei-Dong Xu [MSFT]

Hi Oliver,

For your convenience, I list my sample codes for you to obtain the documentation path.

//Code begin---------------------

//first, please import the word type library fowllowing the kb article 238972
//I mentioned in the above message

//define variables
CApplication objWnd;
CDocuments objDocs;
//the CDocument1 type is CDocument type. The 1 is appended
//by Visual studio C++ to avoid the same name
CDocument1 objDoc;
CString cstrAddr;

// Activate a new word application
objWnd.CreateDispatch("Word.Application.11");
objWnd.put_Visible(TRUE);

//For optional args
COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);

objDocs = objWnd.get_Documents();

//The open method has different parameter amount in different office.
//My sample is based on Office11. Please check your office documentation for
//this
objDoc = objDocs.Open( COleVariant("c:\\Documents and Settings\\v-wdxu\\My Documents\\3723.doc"),
vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);

cstrAddr = objDoc.get_Path();

//Code end----------------------

The path will return as one CString type object. You can retrieve the result from cstrAddr. You can obtain the address string from CString method.

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
O

Oliver Young

//first, please import the word type library fowllowing the kb article 238972
//I mentioned in the above message
There is one problem. I have loaded this project:

http://support.microsoft.com/?id=230689

and View | ClassWizard (Ctrl+W) menu option is disabled, so I can't create
wrapper classes from MSWord.olb using ClassWizard. (Like described here:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;178749 , steps 8, 9
and 10 )
//define variables
CApplication objWnd;
CDocuments objDocs;
//the CDocument1 type is CDocument type. The 1 is appended
//by Visual studio C++ to avoid the same name
CDocument1 objDoc;
CString cstrAddr;

// Activate a new word application
objWnd.CreateDispatch("Word.Application.11");
objWnd.put_Visible(TRUE);
Because I'm writing COM plug-in for MS Word, I belive it should be something
like this:

STDMETHODIMP CMyAddin::OnConnection(IDispatch* Application, ext_ConnectMode
ConnectMode, IDispatch* AddInInst, LPSAFEARRAY* custom)
{
if (NULL == Application) return E_POINTER;

m_pParentApp = Application;
m_pParentApp->AddRef();

CApplication *objWnd = (CApplication)Application;
// Here I have to get current opened document
// and to get its filename or content

if (ConnectMode != ext_cm_Startup)
OnStartupComplete(custom);

return S_OK;
}
 
W

Wei-Dong Xu [MSFT]

Hi Oliver,

Thank you for replying!

If you build the word add-in in C++ without MFC or ATL, you will need to follow the kb article 183758. This kb artilce will introduce the steps for you
to build one in C++. Please go to:
183758 HOWTO: Build a Microsoft Word Add-in (WLL) Using Visual C++
http://support.microsoft.com/?id=183758

Furthermore, I'd suggest you can use MFC with ATL to write the com add-in so that you can directly import the word.olb through class wizard. Based
on my experience on this issue, the sample from code project is very good for you to obtain more information regarding this. Please go to:
Writing an MS Word addin
http://www.codeproject.com/com/adwordaddin.asp

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
------------------------------------------------
This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft
does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any
representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any
software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from
the Internet.
 
O

Oliver Young

Thank you very much for those links. On the second link it is stated (sorry
if I misinterpreted that) that everything you can do with VC++ COM add-in
you can do from MS Word built-in VBA. Is that the truth? Can I add one
toolbar and add one button (with OnClick event handler) on Word toolbar from
VBA? That would be very cool.

Best regards.
 
W

Wei-Dong Xu [MSFT]

Hi Oliver,

Thank you for posting in MSDN managed newsgroup!

I have written two sample vba codes for you below which is used to insert one temporary button into the Tools menu.

1: button in menu
'Code "button in Menu" begin---------------------------------------------------

'insert one button into "Tools" menu
Sub InsertOneButtonInToolsMenu()

'You can specify the menu name to insert the button as your wish
'set temporary to true means the button will be automatically deleted
'when Excel is closed
Set objcontrol = Application.CommandBars("Tools").Controls.Add( _
Type:=Office.MsoControlType.msoControlButton, Temporary:=True)

'set the button's name
objcontrol.Caption = "Test button"
'set the click event handler function to the button. The function name can be
'specifeid as you wish. You can define the event handler function in the VBA
'project module so that Excel can locate the function.
objcontrol.OnAction = "Action"

End Sub

'Delete the button from "Tools" button
Sub deletebutton()

Dim obj As Office.CommandBarControl
For Each obj In Application.CommandBars("Tools").Controls
If obj.Caption = "Test button" Then
obj.delete
End If
Next

End Sub

'Define the 'Action' function in one module for your excel workbook
'so that Excel can locate it when you click the button
sub Action()
'write the event handler code as you wish
'this is for test usage
msgbox "click test button"
end sub

'Code end-----------------------------------------------------

2: button in toolbar
'Another sample begin------------------------------------------

sub insertToolbarButton()
Dim taskBar as CommandBar
Dim objButton as CommandBarControl
Set TaskBar = Application.CommandBars.Add("test")
With TaskBar
.Visible = True
.Position = msoBarTop
End With

'2950 is built-in icon for drawing. This is just for illustration. You can change that
'according to your scenario
Set objButton = Application.CommandBars("test").Controls.Add( _
Type:=msoControlButton, ID:=200, Before:=1, Temporary:=True)
With objButton
.Caption = "New Button"
'as the above sample, create the Action function in the workbook module
.OnAction = "Action"
End With

End Sub

sub deleteTaskBarbutton()
'specify the "test" commandbar name to delete
Application.CommandBars("test").Delete
End sub

'Code end-----------------------------------------------------

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Wei-Dong Xu [MSFT]

Hi Oliver,

Thank you for the replying!

So far as I know, the office automation in VC++ and VBA are the similar. In word, you can change the ID:=200 to ID:=30 which is the built-in icon of
word.

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
O

Oliver Young

Thank you very much for those examples. :)

I'm new in VBA world. I have put your code for adding/removing toolbar
in Document_Open() and Document_Close() events and it works perfect. Is
there a way to make those functions permanent (allways visible)? I'd like my
toolbar to be visible (and button to be functional) whenever user opens any
Word document.

Thank you in advance.
 
W

Wei-Dong Xu [MSFT]

Hi Oliver,

Thank you for replying!

For troubleshooting the VBA codes, I always set the temporary parameter of Add method in Commandbar object to true. In this way, the button I
have added will be automatically deleted when I close word or excel. If you want to keep them existing in the toolbar, you can set the temporary to
false. Please see my sample codes:

'Code begin-----------------------------------------------------
...
'for the first sample
'set temporary to false
Set objcontrol = Application.CommandBars("Tools").Controls.Add( _
Type:=Office.MsoControlType.msoControlButton, Temporary:=false)

'for the second sample
'set temporary to true
Set objButton = Application.CommandBars("test").Controls.Add( _
Type:=msoControlButton, ID:=200, Before:=1, Temporary:=false)
...
'Code end-------------------------------------------------------

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top