473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Sub with ByRef to a Word.Application

125 100+
I have problem creating a private sub to replace text in a wordfile, is the use of ByRef good? The problem is that this works on my developing computer but not at the target computers.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Replace(ByRef WordApp As Word.Application, ByVal OLDText As String, ByVal NEWText As String)
  2.         WordApp.Selection.Find.ClearFormatting()
  3.         WordApp.Selection.Find.Text = OLDText
  4.         WordApp.Selection.Find.Replacement.ClearFormatting  ()
  5.         WordApp.Selection.Find.Replacement.Text = NEWText
  6.         WordApp.Selection.Find.Execute(Replace:=Word.WdRep  lace.wdReplaceAll, Forward:=True, Wrap:=Word.WdFindWrap.wdFindContinue)
  7.     End Sub
  8.  
I get this error message during runtime:

Expand|Select|Wrap|Line Numbers
  1. System.NullReferenceException: Object reference not set to an instance of an object.
  2.   at MyProg.Form1.Replace(Application& WordApp, String OLDText, String NEWText)
  3.  
Here is some source code where i create the word object, in my project i have added a reference to Microsoft Word 11 Object Libary, COM.

Expand|Select|Wrap|Line Numbers
  1. Dim WDApp As Word.Application
  2. WDApp = CreateObject("Word.Application")
  3. WDApp.Visible = False
  4.  
  5. WDApp.Documents.Add(Template:="C:\template.dot", Visible:=False)
  6.  
  7. Replace(WDApp, "<Replace ME>", "New Text")
  8.  
  9. WDApp.Documents.Item(1).SaveAs("C:\newdoc.doc")
  10.  
  11. WDApp.Quit()
  12. WDApp = Nothing
  13.  
Jul 1 '08 #1
0 989

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from...
3
by: tinman | last post by:
Hi.... Assume Function A in an application calls Function GetSomeData in another assembly..... which then is the prefered method to return the SqlDatareader object back to Function A (and why...
2
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
1
by: katzky | last post by:
My VB6 application has some properties which are set ByRef but upgrade to ByVal. How can I assure that the new code behaves the same as the old?
3
by: doriengard | last post by:
Dear VB gurus, A long question, thanks for your patience in advance :-) Part 1) I have a VB windows application that contains a start-up form: frmMain
6
by: ari | last post by:
hey all, i have the following 2 classes: Public Class DataAccessLayer .... .... Public Sub GetRecords(ByRef ds As DataSet1) ds = New DataSet1
7
by: Monty | last post by:
Silly question: If I return an object from a property, is it returned ByRef or ByVal? Is there a way to specify one way or the other? For instance, will the code that calls the MyObject() property...
9
by: Samuel Shulman | last post by:
Hi I wander there is a way to return ByRef just like passing ByRef What I want to achieve is the following: Call a method that returns an object Use that call as an argument to a ByRef...
9
Frinavale
by: Frinavale | last post by:
In VB.NET you can pass parameters by reference (ByRef) or by value (byVal). Is there a word that can be used to refer to this? -Frinny
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.