Solved

Problems in VBA and Word

Posted on 2013-01-04
4
529 Views
Last Modified: 2013-01-07
I have an issue with the VB code I have written in VBA 6.5.  We have today moved to a new environment and all our systems have been upgraded from Office 2003, Windows XP, VBA 6.5 to Office 2010, Windows 7, VBA 7.
I have a word document which I have now saved from .doc to .docm because of all the macros.  When a command button on this document is pressed it opens up another word document that is not visible to the user.  It adds numerous data to the new document and saves it.  One of the things it does is adds information in the header.  This all used to work fine until the changes today.  Now everything works fine but the information that is meant to go into the header is actually put into the body of the document where the code is running from, i.e the wrong document.  Can anyone see anything in the code that might be causing this to happen?  I wouldn’t say I am particularly advanced in VB.


'Add job name and ref to the header
       If Documents(matrixdoc).ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        Documents(matrixdoc).ActiveWindow.Panes(2).Close
    End If
    If Documents(matrixdoc).ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then
        Documents(matrixdoc).ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    Documents(matrixdoc).ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.EndKey Unit:=wdLine
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:=job_Name
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.TypeText Text:=job_Ref
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.EndKey Unit:=wdLine
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:=Date
    Selection.MoveUp Unit:=wdLine, Count:=1
    Selection.TypeText Text:=Initials
    Documents(matrixdoc).ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Open in new window

0
Comment
Question by:jdc1944
[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
  • 2
  • 2
4 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38744201
There is an inherent problem in using the Selection object. It isn't easy to keep track of where it is.

Try replacing lines 1 to 8 with this line:
   
 Documents(matrixdoc).Sections(1).Headers(wdHeaderFooterPrimary).Range.Select

Open in new window

0
 
LVL 2

Author Comment

by:jdc1944
ID: 38744288
Thanks for that but i'm a little confused how I can use that to replace lines 1-8 with what you have suggested.  

I take it you don't mean literally replace lines 1-8 with that?  I just can't see how i can integrate it.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 38744416
Yes. Replace all the lines. It should ensure the Selection is in the main header of the first section of the matrixdoc document.

On thinking about it, it would be better to ensure that the Selection is at the start of the Header range, so try these two lines instead of just the one.
Documents(matrixdoc).Sections(1).Headers(wdHeaderFooterPrimary).Range.Select
Selection.Collapse wdCollapseStart

Open in new window

0
 
LVL 2

Author Closing Comment

by:jdc1944
ID: 38750216
Thanks for that, solves the issue.

Just had to append:

Documents(matrixdoc).ActiveWindow.View = wdPrintView
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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