Solved

Get word to return filename to vb.net

Posted on 2009-04-12
5
415 Views
Last Modified: 2012-05-06
Hi Experts,
I need to open word from vb.net
(can do this part no problem)
then once the user has completed editing the new document, I want to get the filename and path the document was saved as back to the vb app
please help
0
Comment
Question by:esps
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24128518
There are three properties that could be useful to you.

strFileName = objWordDoc.Name
strFilePath = objWordDoc.Path
strFileSpec= objWordDoc.FullName
0
 

Author Comment

by:esps
ID: 24128536
Hi Graham,
That is fine, thanks.
However, as indicated word is opened from vb (word as object ....)
User completes his editing and saves the document.
I want word to return to  the app and get the filename returned at that point.
Maybe it could work to have some sort of effective wait... waiting for word to close
Any ideas
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24128572
If you give control of the word application to the user, you will have to activate a 'With Events' Word application object and trap the BeforeSave event. Personally I have never done that in .Net.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24128642
I have tried it now, and it doesn't work as simply as I hoped. The event occurs before the user has decided on the new name. I'll try a few more things.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 24128681
No you will have to use the DocumentBeforeClose event.


Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim WithEvents oApp As New Word.Application()
 
#Region " Windows Form Designer generated code "
 
    Public Sub New()
        MyBase.New()
 
        'This call is required by the Windows Form Designer.
        InitializeComponent()
 
        'Add any initialization after the InitializeComponent() call
 
    End Sub
 
    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
 
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
 
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "Form1"
        Me.Text = "Form1"
 
    End Sub
 
#End Region
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim oDoc As New Word.Document()
 
        'Start a new document in Word            
        oApp.Visible = True 'while testing - to avoid hidden instances of Word after aborts
 
        oDoc = oApp.Documents.Add()
        ' Set up the header information.
        Dim rng As Word.Range = oDoc.Range(0, 0)
        rng.Font.Name = "Verdana"
        rng.Font.Size = 16
 
        Dim tlb As Word.Table
        tlb = oDoc.Tables.Add(rng, 5, 5)
 
        'Filling data to table
        'Header Row
        tlb.Cell(1, 1).Range.Text = "Code"
        tlb.Cell(1, 2).Range.Text = "Name"
 
        'Data Row
        tlb.Cell(2, 1).Range.Text = "0001"
        tlb.Cell(2, 2).Range.Text = "John"
        tlb.Cell(3, 1).Range.Text = "0002"
        tlb.Cell(3, 2).Range.Text = "Fred"
        tlb.Cell(4, 1).Range.Text = "0003"
        tlb.Cell(4, 2).Range.Text = "Alf"
 
        'oDoc.SaveAs("MyNewDocument.doc")
        'CType(oDoc, Word._Document).Close()
        'CType(oApp, Word._Application).Quit()
    End Sub
 
      Private Sub oApp_DocumentBeforeClose(ByVal Doc As Word.Document, ByRef Cancel As Boolean) Handles oApp.DocumentBeforeClose
        MsgBox(Doc.FullName)
    End Sub
End Class

Open in new window

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question