Blinocac200sx

Ok, I want to assign a the name of the currently loaded document to a variable, how would I go about doing this

Re: Visual Studio Tools for Office Document Name?

Cindy Meister

Which programming language are you using




Re: Visual Studio Tools for Office Document Name?

Blinocac200sx

Whoops, my bad, C#



Re: Visual Studio Tools for Office Document Name?

Cindy Meister

Roughly, like this:

string docName = wordApp.ActiveDocument.FullName;

or just name if you don't want the path






Re: Visual Studio Tools for Office Document Name?

Blinocac200sx

When you say roughly, how rough do you mean Because wordApp gives a context error.



Re: Visual Studio Tools for Office Document Name?

X4U

Hi,

within VSTO you can say:

this.Application.ActiveDocument.FullName;

Hope this helps,

greets, Helmut






Re: Visual Studio Tools for Office Document Name?

Cindy Meister

Roughly means I was typing it from memory, rather than creating and testing a code project, so there could be a typo or two.

wordApp is an object assigned to a Word.Application (using Word = Microsoft.Office.Interop.WordWink object. Since you gave absolutely no details about what kind of project you're working on, nor provided any sample code, it's not possible to give you an answer in the context of what you're using so that you can immediately recognize all the objects...






Re: Visual Studio Tools for Office Document Name?

Blinocac200sx

using System;

using System.IO;

using System.Text;

using System.Windows.Forms;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using Microsoft.VisualStudio.Tools.Applications.Runtime;

using Word = Microsoft.Office.Interop.Word;

using Office = Microsoft.Office.Core;

using Microsoft.Office.Core;

using Microsoft.Office.Interop;

using Microsoft.Office.Tools;

using Microsoft.Office.Interop.Word;

namespace Save_Doc_Button

{

public partial class ThisAddIn

{

private Office.CommandBar AppCommandBar;

private Office.CommandBarButton cbbSaveData;

private Office.CommandBarButton cbbEPAID;

private Office.CommandBarButton cbbdate;

string docname;

string filename = file_path_check(); //variable to hold file path for saving form data and file

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

BuildFormCommandBar();

}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

{

}

private void Application_NewDocument(Microsoft.Office.Interop.Word.Document Doc)

{

//Handles Application.NewDocument

}

private void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)

{

// Handles Application.DocumentOpen

}

private void Application_WindowActivate(Microsoft.Office.Interop.Word.Document Doc, Microsoft.Office.Interop.Word.Window wn)

{

// Handles Application.WindowActivate

}

private void CreateCommandBarButton(string Caption, string Tag, ref Office.CommandBarButton Btn)

{

//Add the button with the given Caption and Tag properties.

Btn = (Office.CommandBarButton)(AppCommandBar.Controls.Add(

Office.MsoControlType.msoControlButton,

missing, missing, missing, missing));

Btn.Caption = Caption;

Btn.Tag = Tag;

//Set up the event handler for the button when it is clicked.

Btn.Click += new Office._CommandBarButtonEvents_ClickEventHandler(cbbSaveData_Click);

}

#region VSTO generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InternalStartup()

{

this.Startup += new System.EventHandler(ThisAddIn_Startup);

this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);

}

#endregion

private void BuildFormCommandBar()

{

{

try

{

//Add the command bar to the document.

AppCommandBar = this.Application.CommandBars.Add("Forms Commands", Office.MsoBarPosition.msoBarPopup, missing, true);

//Add command bar buttons to the pop-up.

this.CreateCommandBarButton("Extract and Save", "EXTRACT_SAVE", ref cbbSaveData);

this.CreateCommandBarButton("EPA ID", "EPA_ID", ref cbbEPAID);

this.CreateCommandBarButton("Date", "DATE", ref cbbdate);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message, "Error adding Forms Command Bar");

}

}

}

private Microsoft.Office.Core.CommandBar CreateCommandBar(string name)

{

return this.Application.CommandBars.Add(name, Microsoft.Office.Core.MsoBarPosition.msoBarFloating, false, true);

}

static string file_path_check() //checks the config file to determine which files path should be used

{

string docname;

string[] asConfigText = new string[2];

string compline;

string flnm;

docname = Microsoft.Office.Interop.Word.Application.ActiveDocument.Name;

StreamReader sr = new StreamReader(@"C:\Filetest\FATtext.txt"); //set this to correct path later

compline = sr.ReadLine();

while ((asConfigText = compline.Split('@'))[0] != docname)

{

compline = sr.ReadLine();

asConfigText[0] = compline.Split('@')[0];

}

flnm = asConfigText[2];

sr.Close();

return flnm;

}

static string Data_File_Name(string pathvar) //checks for files in local save folder and decides save name.

{

int namevar = 01;

string namevar_2 = namevar.ToString();

string flnm = "Test'namevar_2'.txt";

string path = @"'pathvar'+'flnm'";

while (File.Exists(path))

{

namevar++;

namevar_2 = namevar.ToString();

}

flnm = path;

return flnm;

}

private void cbbSaveData_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)

{

this.Application.ActiveDocument.SaveFormsData = true;

Data_File_Name(filename);

this.Application.ActiveDocument.save(filename);

}

}

}





Re: Visual Studio Tools for Office Document Name?

Blinocac200sx

The hang up is in the file_path_check method where we try to assign the name of the current document to the string docname.



Re: Visual Studio Tools for Office Document Name?

X4U

Hi,

maybe try this one - (changed a little)

Code Snippet

static string Data_File_Name(string pathvar) //checks for files in local save folder and decides save name.

{

//initial filename = bla/bla/yabba/yabba/Test01.txt

int fileIndex = 01;

// variable holds the filepath

string filepath;

do

{

// calculate the filename

filepath = CalculateFileName(fileIndex, pathvar, "Test", ".txt");

fileIndex++;

}

while (File.Exists(filepath));

return filepath;

}

static string CalculateFileName(int index, string directoryPath, string fileName, string extensionName)

{

string filename = string.Format ("{0}_{1:00}{2}", fileName, index, extensionName );

return Path.Combine(directoryPath, filename);

}

Hope this helps,

greets Helmut






Re: Visual Studio Tools for Office Document Name?

Scorphg

Hi

how would you do this using vb (vsto)

as the language.




Re: Visual Studio Tools for Office Document Name?

Cindy Meister

To whom are you replying

One possibility is

Me.Application.ActiveDocument.FullName






Re: Visual Studio Tools for Office Document Name?

Scorphg

I was replying to the original poster of this thread.

I tried your response...(me.application.activedocument.fullname) and that does not

exist in the object library as i received the error... activedocument is not a member

of microsoft.office.infopath.application... any other ideas I am using VB for this

well...managed code






Re: Visual Studio Tools for Office Document Name?

Cindy Meister

This discussion was about working with Word - you didn't mention InfoPath.

To get the syntax for InfoPath, the best place to ask is the InfoPath newsgroup. It's native language is VSTA (Visual Studio Tools for APPLICATIONS; VSTO, the topic of this forum, is something else), so you should have no problem getting the correct information.

http://msdn.microsoft.com/newsgroups/default.aspx dg=microsoft.public.infopath &lang=en&cr=US






Re: Visual Studio Tools for Office Document Name?

Scorphg

Thank you Cindy,

I appreciate your comment/help