Solved

VB/MSWord: add new page and copy contents + bookmarks

Posted on 2002-06-02
7
259 Views
Last Modified: 2008-02-01
Hello,
Using VB I have to add new page to the beginning of the word document and put there same contents including
bookmarks and fields.Need urgent help,please send example!
0
Comment
Question by:soferstam
[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
7 Comments
 
LVL 4

Accepted Solution

by:
Monchanger earned 300 total points
ID: 7049785
This should get you started, I covered the basics.
Help on this is available in your VBA help or at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odeopg/html/deovroffice2000visualbasicprogrammersguide.asp

    ' Open word
    Dim myWord As Word.Application
    Set myWord = CreateObject("Word.Application")
   
'This is good for debuging while writing your code
'myWord.Visible = True

    ' Open our document
    Dim myDoc As Word.Document
    Set myDoc = myWord.Documents.Open(App.Path & "\Doc1.doc")  ' Enter the path to your document here

    ' Ranges are a basic part of all Office applications. Do some reading about them at :
' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odeopg/html/deovrtherangeobject.asp   
    Dim myRange As Word.Range
   
    '********************************************
    ' Creation of new page at start of document
    ' Go to start of document
    Set myRange = myDoc.GoTo(wdGoToPage, wdGoToAbsolute, 1)
'Debug note: This would cause the document to center on the set range:
'myRange.Select
    ' Insert a pagebreak at the current location
    Selection.InsertBreak wdPageBreak
    Set myRange = Nothing
    '********************************************
   
   
    '********************************************
    ' Adding content to the first page
    ' Select start of this page
    Set myRange = myDoc.GoTo(wdGoToPage, wdGoToAbsolute, 1)
    ' Add text before cursor - this is like typing text using the keyboard,
    ' where you move the cursor forward.
    myRange.InsertAfter "This is new text"
    ' Add a carridge return
    myRange.InsertAfter vbCr
    ' Add a field
    myRange.InsertAfter "More text"
    Set myRange = Nothing
    '********************************************
   
   
    '********************************************
    ' Adding fields
    ' Let's put the filename at the start of page one
    Set myRange = myDoc.GoTo(wdGoToPage, wdGoToAbsolute, 1)
'    myRange.Select
    myDoc.Fields.Add myRange, , "FILENAME"
    '********************************************

    '********************************************
    ' Adding bookmarks
    ' Select the first page
    Set myRange = myDoc.GoTo(wdGoToPage, wdGoToAbsolute, 1)
    ' Add a bookmark - note that bookmark names cannot contain spaces (and various
    ' other characters)
    myDoc.Bookmarks.Add "Page_one", myRange
    '********************************************
   
    '********************************************
    ' Cleanup process:
    ' Save the document
    myDoc.Save
    ' Close the document
    myDoc.Close
    Set myDoc = Nothing
    ' Close word
    myWord.Quit
    Set myWord = Nothing
    '********************************************

BTW - I use Word 2000 (9.0). It is possible you might have a need a few slight changes if you use something else.

Hope this helps.
Good luck :-)
0
 
LVL 4

Expert Comment

by:Monchanger
ID: 7049786
I forgot to mention ....
Save your document before testing your code on it !!!
:-)
0
 
LVL 3

Expert Comment

by:n_narayanan
ID: 7050839
This may help you,

http://www.freevbcode.com/ShowCode.Asp?ID=1539

Full Source Code available there.

Narayanan
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 1

Author Comment

by:soferstam
ID: 7051721
Hi,
I'm getting error on Selection.InsertBreak saying object refurs to a "framed paragraph".It means probably that I have frames and Selection object got a frame in it.
Do you know how can I make sure to copy everything on a page,including frames,to a newly created page?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7851375
Hi soferstam,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept Monchanger's comment(s) as an answer.

soferstam, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7912918
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0
 
LVL 5

Expert Comment

by:dc197
ID: 8260490
Argh!
On one machine, office 10 (word 2002), it works fine.
But on the office 9 machine (word 2000) I had to change
myRange.Select
Selection.InsertBreak wdPageBreak

to
myRange.InsertBreak wdPageBreak

because the selection objected was allegedly not defined (error 91) on the Word 2000 machine!

Be careful!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 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