perpetual_dream

Hello,

I am trying to allow the user to save the open document in a folder I specify in office applications by clicking on a button added to startup.

Therefore, I am trying to find out the current active window in Powerpoint/Word/Excel.

I have managed to find out the active window and adding a button to it at startup in outlook by using

Code Snippet

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

Dim explorer As outlook.Explorer = Me.Application.ActiveExplorer

_cbBar = explorer.CommandBars.Add("SO.dms", Office.MsoBarPosition.msoBarTop, False, True)

' Create a CommandBarButton object.

_cbButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)

_cbButton.Caption = "SO.dms"

_cbButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption

' Make the new CommandBar visible.

_cbBar.Visible = True

explorer = Nothing

End Sub

Private Sub _cbButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbButton.Click

' a form that processes the opened document

Dim docsave2 As New ChooseProject

docsave2.showdialgo()

end sub

1-What is the similar command that should be used in Powerpoint/Word/Excel to find the current active explorer

2-Is there a way to find out the file extension of the current active window document .



Re: Visual Studio Tools for Office Powerpoint/Word/Excel Active windows

Cindy Meister

The individual Office applications have no equivalent to the Outlook "Active Explorer" (they don't have "explorers").

You can get the information about the document any window is hosting by the appropriate property for that object model. In Word, for example: ActiveWindow.Document. In Excel it would probably be .Workbook. The Office object models don't provide a way to get the extension directly, but you should be able to use the .Name (or FullName) properties then extract the extension from that using the usual .NET Framework tools.






Re: Visual Studio Tools for Office Powerpoint/Word/Excel Active windows

perpetual_dream

Now I managed to add a button to winword by using this code:

Code Snippet

Imports word = Microsoft.Office.Interop.Word

public class ThisAddIn

Private _cbBar As Office.CommandBar

Private WithEvents _cbButton As Office.CommandBarButton '

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

Try

_cbBar = Me.Application.CommandBars.Add("Save Document", Office.MsoBarPosition.msoBarTop, False, True)

_cbButton = CType(_cbBar.Controls.Add(Office.MsoControlType.msoControlButton), Office.CommandBarButton)

_cbButton.Caption = "Save Document"

_cbButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption

_cbButton.Tag = "MyBar.MyControl"

_cbBar.Visible = True

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

_cbButton.Delete()

_cbButton = Nothing

End Sub

Private Sub _cbButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbButton.Click

Try

Dim docsave2 As New ChooseProject

docsave2.ShowDialog()

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

ChooseProject form conatins a combobox which contains the projects the user is currently working on and a save as button that allows the user to save the document at the choosen project location....

Choose Project Contains the following code:

Code Snippet

Imports WORD = Microsoft.Office.Interop.Word

Public Class ChooseProject

Dim application1 As New WORD.Application

Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click

' oCurDoc = Me.application1.ActiveDocument

Dim projectname As String

projectname = CType(cmbprojects.SelectedItem, valuedescriptionpair).Description

Me.application1.ActiveDocument.SaveAs(My.Settings("mainpath") & projectname & "\" & Me.application1.ActiveDocument.Name & ".doc")

End Sub

Now when I try to save the document that I opened through open file in word. I keep getting this error:

This command is not available because no document is open. for Me.application1.ActiveDocument.SaveAs line of code...

why do I keep getting this error





Re: Visual Studio Tools for Office Powerpoint/Word/Excel Active windows

Cindy Meister

I believe the error is occurring because you're not using the same Word.Application. Look at your code:

Imports WORD = Microsoft.Office.Interop.Word

Public Class ChooseProject

Dim application1 As New WORD.Application

Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click

' oCurDoc = Me.application1.ActiveDocument

Dim projectname As String

projectname = CType(cmbprojects.SelectedItem, valuedescriptionpair).Description

Me.application1.ActiveDocument.SaveAs(My.Settings("mainpath") & projectname & "\" & Me.application1.ActiveDocument.Name & ".doc")

End Sub

You assign a *NEW* Word.application instance to application1. This will start Word again, as a seperate (and invisible) instance of the Word application. And when Word is started using the New keyword there is no document created (or opened). Probably, you need to overload the class's New procedure (the constructor) to accept some objects from your main Add-in class. Given that you want to save the document, I'd probably pass the document object. Very roughly (since I usually do this in C#, not VB):

private Word.Document wordDoc = null;

public void ChooseProject (Word.Document activeDoc)

{

wordDoc = activeDoc;

}

In VB, I think it would go something like this

Private wordDoc as Word.Document = Nothing

Public Sub New (ByVal activeDoc as Word.Document)

Me.wordDoc = activeDoc

End Sub

'default constructor

Public Sub New()

End Sub






Re: Visual Studio Tools for Office Powerpoint/Word/Excel Active windows

perpetual_dream

Thank you Cindy it worked out Smile Just in case someone needed help for the same topic... the button on click event becomes

Private Sub _cbButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles _cbButton.Click

Try

Dim docsave2 As New ChooseProject(Me.Application.ActiveDocument)

'' Dim savefile As New ClassLibrary1.savefile

''docsave2.ShowDialog()

docsave2.ShowDialog()

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub