borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Next Topic (Visual Basic) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Creating and editing a Word document
Poster Message
iliekater
Level: Master

Registered: 04-02-2005
Posts: 132

icon Creating and editing a Word document

I have made a program that it has a button with which it opens Microsoft Word and it writes some data in it . Well , the first time it does that thing there is no problem . However , if I close Word and then press again the specific button which opens Word and writes the data , then my program crashes and returns an error saying :

quote:
Run time error '462' : The remote server machine does not exist or is unavailable .



I have located the problem and it only happens when I I try to modify the left and right margins of the Word document . The funny thing is that anything else from the PageSetup attributes can be modified with no problems at all . Here's my code :
  WordApplication.ActiveDocument.PageSetup.PaperSize = wdPaperA4
  WordApplication.ActiveDocument.PageSetup.Orientation = wdOrientLandscape
  WordApplication.ActiveDocument.PageSetup.LeftMargin = CentimetersToPoints(1.48)
  WordApplication.ActiveDocument.PageSetup.RightMargin = CentimetersToPoints(0.92)


What can I do about it ?
Up to now the only thing I did was to create a predefined Word file , but I'd like to modify the document's margins by code . Is it possible ?
14-06-2008 at 07:29 PM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 1034
icon Re: Creating and editing a Word document

Hi
THe first problem indicates that your pointer to the WordApplication is destroyed - this happens when you use "set wordapplication = GetObject" or "set wordapplicatino = CreateObject " then someone closes word, then you attempt to resuse the wordapplciation without rerunning the  getobject or createobject code.
You cannot modify an activedocument if there is no document open (you will get a similar problem to the above)
Pagesettings only work if you have a printer installed (and you have an activedocument open in word).
I suggest that you run your application to open word, then BEFORE YOU CLOSE WORD open the taskmanager, look in the processes tab for WinWord.exe, then close word (leave taskamanger open), now, how many winword.exe's do you have running?
I suspect it's either zero (your error 462 will occur if its zero) or one (your activedocument will fail)

Don't let the user close word whilst you have a WordApplication pointer that you expect to be connected to word. Let the user see the document after you have finished.
ie.


set objword = new word.application
set objdoc = objword.documents.open "c:fred.doc"
objdoc.inserttext "mooooo"
objdoc.save
objdoc.close
set objdoc = nothing
objword.quit
set objword= nothing
if msgbox("Want to see the document?",vbQuestion or vbYesNo) = vbyes then
  shellexecute "c:fred.doc",1
end if


Hope this helps
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

24-06-2008 at 09:26 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : Creating and editing a Word document
Next Topic (Visual Basic) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Il portale per lui e lei | Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder