We help IT Professionals succeed at work.

Vb.net and Word save

FCapo
FCapo asked
on
Hi,

My visual basic application fills bookmarks in a word document, I'd like to be able to open the savefiledialog in word from vb.net.

I know I can do this by adding : appWord.ActiveDocument.Save()
but I was wondering if it would be possible to define the current path that the savefiledialog starts in (the root), I already know where to save the files but I want to give the user the option to save over the file in the directory or change the name to something else.

Is this at all possible?
Comment
Watch Question

Commented:
pathfilename = "c:\ilikethisfolder\ilikethisfoldertoo\ilikethisfile.docx"
You can use Word.ActiveDocument.SaveAs2(PathFilename)

Author

Commented:
The problem is it doesn't show the dialog, it saves the document in the directory but it doesn't give the user the option to rename it or change location?

Commented:
you could use:
Word.FileDialog(MsoFileDialogType.msoFileDialogSaveAs)

Author

Commented:
How do I implement this,

If I just paste the code it says : Property access must assign to the property or use its value

Commented:
Try
Dim fd as Word.Dialog = Word.FileDialog(MsoFileDialogType.msoFileDialogSaveAs)
fd.show()
fd.something...

Author

Commented:
I'm getting an odd error message

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Word.Dialog'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000209B8-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Commented:
is your activation wizard showing? i meant a not registered version?

Commented:
You could also use when word application quits:  <wdPromptToSaveChanges>

      Dim missing As Object = System.Reflection.Missing.Value
       Word.Quit(Word.WdSaveOptions.wdPromptToSaveChanges, missing, missing)

OR
you really like not using .Quit?
BRONZE EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
One option would be to use the .NET File Save dialog as you can customize it and once the user selects the location and gives a file name, use that with Word.ActiveDocument.SaveAs method.

Commented:
yes, right, you could use the savefilediaglog
       
Dim sfd As New SaveFileDialog
 sfd.ShowDialog()
WordApp.ActiveDocument.SaveAs2(sfd.FileName)
Great idea CodeCruiser ;-)

Author

Commented:
That's a good idea thank you,

If on the other hand I know the path to save the file and I use the following code, it sometimes gives me errors that the pathname is wrong, sometimes it works and sometimes it doesn't (sometimes it saves in the directory and most times it gives me a directory error). Why does it work randomly like this?

Dim s As String =  "S:\OUV. DOSSIER- SOUMISSION\test.doc

appWord.ActiveDocument.SaveAs(s)
BRONZE EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
It looks like a network drive so network issues may sometimes prevent word from saving.