Although the program still run fine I am getting a little tired of the warning on my program that I can't
 get rid of.
 First one is with a process:


          Dim NewProcess As Process = New Process
                 If Attachment2TextBox.Text <> "" Then
                     NewProcess.Start(Attachment2TextBox.Text)


 Here is the warning:
 Warning    5   Access of shared member, constant member, enum member or nested type through an instance;
 qualifying expression will not be evaluated.   
 It gives me the warning on the NewProcess.Start I dont know how else I can use a process!
 The second is using the word interface:


          Dim oDoc As Word.Document
           oDoc.Bookmarks("FirstName").Range.Text = FirstNameTextBox.Text


 heres the warning:
 Warning    6   Variable 'oDoc' is used before it has been assigned a value. A null reference exception
 could eesult at runtime.
 Once again how can I give oDoc a value at dimention?
 Makes no sense to me any how would be apreciated.

Recommended Answers

All 4 Replies

For you first question regarding Process. "Process.Start" is a shared function that returns the started process. The correct syntax would be:

Dim NewProcess As Process ' No New keyword
If Attachment2TextBox.Text <> "" Then NewProcess = Process.Start(Attachment2TextBox.Text)

For you second question, I am not going to re-invent the wheel, but rather point you to Microsoft's code example on this. It provides a nice description of how to do this.

The root page for their samples is:

All-In-One Code Framework

Hi
Thank you very much for the first part it now works a treat with no errors.

As for the word part, well I have checked the code example and many others but still have not found a solution. Perhaps I had better explain the problem:

Dim oWord As Word.Application
Dim oDoc As Word.Document
oWord = CreateObject("Word.Application")
oDoc = oWord.Documents.Add(OpenFileDialog1.FileName)

All ok so far but when it comes to bookmarks, I think I can only use the Odoc.Bookmarks to use this function. Ie Odoc already has a value and has been used so why do I get the warning when I use

oDoc.Bookmarks("Contact1").Range.Text = Contact1TextBox.Text

I have tried to make a new variable

Dim oBook As Word.Bookmarks

and used

oBook.add("Contact1").Range.Text = Contact1TextBox.Text
But this does not work at all.

A last little help would be great.

oBook

Give this code pattern a try:

Imports Microsoft.Office.Interop

Public Class Form1

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

      ' When working with ComObjects like a WordApp or any of its parts like Docement, Range etc.
      ' define a variable for each part.  

      ' I follow the Single Dot Rule when accessing the component classes.
      '     i.e. donot code doc.BooKmarks("Contact1").Range.Text 
      '     (Bookmark class and Range Class referenced)
      '
      ' This can create unbound references to a ComObject that will cause Word to remain running as 
      ' a hidden application.

      Dim wordapp As Word.Application = CType(CreateObject("Word.Application"), Word.Application)

      ' load a document template file

      Dim doc As Word.Document = wordapp.Documents.Add("D:\My Documents\temp\Word Example Bookmark Template.dot")

      Dim bookmark As Word.Bookmark = doc.Bookmarks("Contact1")

      Dim rng As Word.Range = bookmark.Range
      bookmark = Nothing ' done with bookmark, so release it

      rng.Text = "some text"
      rng = Nothing ' done with this range, so release it

      doc.SaveAs("D:\My Documents\temp\Word Example Bookmark Template.doc", Word.WdDocumentType.wdTypeDocument)
      doc.Close(False)

      doc = Nothing ' done with doc, so release it
      wordapp.Quit()
      wordapp = Nothing 'done with app, so release it
   End Sub
End Class

Thanks for this. I haven't used your code but I get the gist of it works now. What I was doing wrong was that I couldn't dimention the oDoc (doc) because I was using an unknown template. IE one the user browses to see which was within an if then statement after the dims. So I have moved the if then to encompas the whole sub routine with the dim statements within it, now it works without the warnings.

It was readiong your code that got me to find it and I have changed the way it dimentions the app to your sugestion, I am not sure why it is better but I will take you expertise over mine.

Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.