Get the active file name and path for Office Com addin with .net VB

J

James

I'm creating an add-in for Word, Excel, and powerpoint that needs to
pass the Filename and save path when the button is clicked. name seems
to be in Microsoft.Office.Core.DocumentProperty but can't get it and
not sure how to get saveas values. Using VS 2005 .NET VB
 
C

Cindy M.

Hi James,
I'm creating an add-in for Word, Excel, and powerpoint that needs to
pass the Filename and save path when the button is clicked. name seems
to be in Microsoft.Office.Core.DocumentProperty but can't get it and
not sure how to get saveas values. Using VS 2005 .NET VB
I'm not exactly sure what you're asking, but to get the file name, it's
path, or the full information most Office applications provide the Name,
Path and FullName properties of the object you'd save (Document object
in Word, Workbook object in Excel, for example).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
J

James

Hi James,


I'm not exactly sure what you're asking, but to get the file name, it's
path, or the full information most Office applications provide the Name,
Path and FullName properties of the object you'd save (Document object
in Word, Workbook object in Excel, for example).

Cindy Meister
INTER-Solutions, Switzerlandhttp://homepage.swissonline.ch/cindymeister(last update Jun 17 2005)http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)

I have tried applicationObject.ActiveDocument() that doesn't work in
word. then i tried

Dim properties As Microsoft.Office.Core.DocumentProperties

properties = DirectCast(applicationObject.BuiltinDocumentProperties,
Microsoft.Office.Core.DocumentProperties)

Dim prop As Microsoft.Office.Core.DocumentProperty
prop = properties.Item("Name")

my addin still can't get the filename or path. What are you talking
about?
 
C

Cindy M.

Hi James,
I have tried applicationObject.ActiveDocument() that doesn't work in
word. then i tried

Dim properties As Microsoft.Office.Core.DocumentProperties

properties = DirectCast(applicationObject.BuiltinDocumentProperties,
Microsoft.Office.Core.DocumentProperties)

Dim prop As Microsoft.Office.Core.DocumentProperty
prop = properties.Item("Name")

my addin still can't get the filename or path. What are you talking
about?
In each of the object models (but I'm most conversant with Word), the
main object (document, workbook, etc.) has a Name, Path and FullName
property. What do you get if you do somethin like
MessageBox.Show(applicationObject.ActiveDocument.FullName)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
J

James

Hi James,






In each of the object models (but I'm most conversant with Word), the
main object (document, workbook, etc.) has a Name, Path and FullName
property. What do you get if you do somethin like
MessageBox.Show(applicationObject.ActiveDocument.FullName)

Cindy Meister
INTER-Solutions, Switzerlandhttp://homepage.swissonline.ch/cindymeister(last update Jun 17 2005)http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)

I get an error message. All samples i have tried don't work. here is
my code

imports Extensibility
imports System.Runtime.InteropServices

#Region " Read me for Add-in installation and setup information. "
' When run, the Add-in wizard prepared the registry for the Add-in.
' At a later time, if the Add-in becomes unavailable for reasons such
as:
' 1) You moved this project to a computer other than which is was
originally created on.
' 2) You chose 'Yes' when presented with a message asking if you
wish to remove the Add-in.
' 3) Registry corruption.
' you will need to re-register the Add-in by building the $SAFEOBJNAME
$Setup project,
' right click the project in the Solution Explorer, then choose
install.
#End Region

<GuidAttribute("A28B54E2-5A81-4381-9112-5A9387F32CC8"),
ProgIdAttribute("MyAddin1.Connect")> _
Public Class Connect

Implements Extensibility.IDTExtensibility2
Dim WithEvents MyButton As Microsoft.Office.Core.CommandBarButton
Dim applicationObject as Object
Dim addInInstance As Object
Dim activedocument As Microsoft.Office.Core.DocumentProperty

Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnBeginShutdown
On Error Resume Next
' Notify the user you are shutting down, and delete the
button.
MsgBox("Our custom Add-in is unloading.")
MyButton.Delete()
MyButton = Nothing
End Sub

Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub

Public Sub OnStartupComplete(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnStartupComplete
Dim oCommandBars As Microsoft.Office.Core.CommandBars
Dim oStandardBar As Microsoft.Office.Core.CommandBar

On Error Resume Next
' Set up a custom button on the "Standard" command bar.
oCommandBars = applicationObject.CommandBars
If oCommandBars Is Nothing Then
' Outlook has the CommandBars collection on the Explorer
object.
oCommandBars =
applicationObject.ActiveExplorer.CommandBars
End If

oStandardBar = oCommandBars.Item("Standard")
If oStandardBar Is Nothing Then
' Access names its main toolbar Database.

oStandardBar = oCommandBars.Item("Database")

End If

' In case the button was not deleted, use the exiting one.
MyButton = oStandardBar.Controls.Item("My Custom Button")
If MyButton Is Nothing Then

MyButton = oStandardBar.Controls.Add(1)
With MyButton
.Caption = "SpeechTags"
.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption

' The following items are optional, but recommended.
' The Tag property lets you quickly find the control
' and helps MSO keep track of it when more than
' one application window is visible. The property is
required
' by some Office applications and should be provided.

.Tag = "SpeechTags for Docs"

' The OnAction property is optional but recommended.
' It should be set to the ProgID of the add-in, so
that if
' the add-in is not loaded when a user clicks the
button,
' MSO loads the add-in automatically and then raises
' the Click event for the add-in to handle.

.OnAction = "!<MyCOMAddin.Connect>"

.Visible = True
End With
End If

' Display a simple message to show which application you
started in.
'MsgBox("Started in " & applicationObject.Name & ".")

If applicationObject.Name = "Word" Then

ElseIf applicationObject.Name = "Excel" Then

Else 'powerpoint

End If

oStandardBar = Nothing
oCommandBars = Nothing
End Sub

Public Sub OnDisconnection(ByVal RemoveMode As
Extensibility.ext_DisconnectMode, ByRef custom As System.Array)
Implements Extensibility.IDTExtensibility2.OnDisconnection
On Error Resume Next
If RemoveMode <>
Extensibility.ext_DisconnectMode.ext_dm_HostShutdown Then _
Call OnBeginShutdown(custom)

applicationObject = Nothing

End Sub

Public Sub OnConnection(ByVal application As Object, ByVal
connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As
Object, ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnConnection
'MsgBox("On Connection In MyAddin")
applicationObject = application
addInInstance = addInInst


' If you aren't in startup, manually call OnStartupComplete.
If (connectMode <>
Extensibility.ext_ConnectMode.ext_cm_Startup) Then _
Call OnStartupComplete(custom)
End Sub

Private Sub MyButton_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As
Boolean) Handles MyButton.Click
' MsgBox("Our CommandBar button was pressed!")
On Error GoTo Etrap
'Dim properties As Microsoft.Office.Core.DocumentProperties

'properties =
DirectCast(applicationObject.BuiltinDocumentProperties,
Microsoft.Office.Core.DocumentProperties)

'Dim prop As Microsoft.Office.Core.DocumentProperty
'prop = properties.Item("Name")
Dim DocName As String
DocName = applicationObject.ActiveDocument()
MsgBox(DocName)

Etrap:
MsgBox(Err.Description)
End Sub

End Class
 
J

James

Hi James,






In each of the object models (but I'm most conversant with Word), the
main object (document, workbook, etc.) has a Name, Path and FullName
property. What do you get if you do somethin like
MessageBox.Show(applicationObject.ActiveDocument.FullName)

Cindy Meister
INTER-Solutions, Switzerlandhttp://homepage.swissonline.ch/cindymeister(last update Jun 17 2005)http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)

I get an error message. All samples i have tried don't work. here is
my code

I will send you the code to your email from your site
 
C

Cindy M.

Hi James,
I get an error message. All samples i have tried don't work.
Among other things, you're missing Imports for the various
Office object models you want to work with. Such as that for
Word and Outlook. And if you haven't already done so, you must
Add a Reference to the PIAs for each of them from the COM tab.
If you don't, you're going to have to work with late-binding
(which is also possible, but not something you - who have no
idea about working with these object models - want to try as
there will be no Intellisense to support you).

This line of code is wrong

Dim activedocument As
Microsoft.Office.Core.DocumentProperty

ActiveDocument is an object of the Word application (which you
haven't defined anywhere yet in your code). After you have an
Imports for the Word object library, you can do something like
this
Imports Word = Microsoft.Office.Interop.Word

Dim doc as Word.document
doc = WordApp.ActiveDocument

However, you also haven't set up a variable (class level) for
the Word application (or any other Office application) and
assigned it to the appropriate application. You must do that
before you can do anything with any application. You have the
skeleton structure for it

If applicationObject.Name = "Word" Then

ElseIf applicationObject.Name = "Excel" Then

Else 'powerpoint

End If

But you aren't doing anything with it. It needs to be
something along these lines

Private WordApp as Word.Application

If applicationObject.Name = "Word" Then
WordApp = applicationObject as Word.Application

ElseIf applicationObject.Name = "Excel" Then

Else 'powerpoint

End If

I highly recommend you get Andrew Whitechapel's ".NET
developement for Microsoft Office" and work your way through
it. He goes into quite some detail about how to create Shared
Add-ins that work with multiple Office applications. Including
a lot of pitfalls and "gotchas".

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun
17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 

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