Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
|
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2011, 01:36 AM
Registered User
 
Join Date: Jul 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Wink Newbies Word 2010 Problem

Hey All,

I'm completely new to this, so forgive my ignorance. All I'm doing is creating a Form for a template with a few fields. What I want to do is fill out the form, and then update the DocVariables in the word document. The form works fine when I run it from the VBA editor, however when I try to create a new document with the template it only updates some of the DocVariables.

It will update the Title, and the Name/ID/Course/Assignment in the header of the second page. However, it WONT update the Name/ID/Course/Assignment DocVariables on the first page. I'm assuming its just a simple error in some of my logic - the following is the code I'm using:

Code:
Sub CallUF()

Dim oFrm As Assignment
Dim oVars As Word.Variables
Set oVars = ActiveDocument.Variables
Set oFrm = New Assignment
With oFrm
    .Show
    If .boolProceed Then
        oVars("varTitle").Value = .tTitle
        oVars("varName").Value = .tName
        oVars("varStudent").Value = .tStudent
        oVars("varCourse").Value = .tCourse
        oVars("varAssignment").Value = .tAssignment
        myUpdateFields
    Else
        MsgBox "Form cancelled by user"
    End If
End With
Unload oFrm
Set oFrm = Nothing
Set oVars = Nothing

End Sub

Sub myUpdateFields()
Dim pRange As Word.Range
For Each pRange In ActiveDocument.StoryRanges
   Do
      pRange.Fields.Update
      Set pRange = pRange.NextStoryRange
   Loop Until pRange Is Nothing
Next
End Sub
Thanks Very Much Everyone!
 
Old July 27th, 2011, 12:26 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Is there anything to do with the ActiveDocument; this has always been the gray area. Try to code with something like

Documents("Docname").

etc so that you are sure that the changes happen on the document you want

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old July 27th, 2011, 05:44 PM
Registered User
 
Join Date: Jul 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Aha, thanks man, just looked into it - and it's working like a charm now.

Just on a side-note, is there any automated way to get rid of all the DocVariables and macros after running the template so that the new document appears as just a regular document?

I noticed that when I open a document made off this template on other computers - I get warnings about macros and can see the DocVariables highlight when I run my cursour over them. I don't want to give assignments to my lecturers, however, with macro warnings and docVariables all over the place if I can help it.

Is there a way to just convert the document to a regular docx text file after running the template for the first time?

Thanks again!
 
Old July 29th, 2011, 12:41 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

You can loop through the docvariable count and remove them and can also remove the code file from the VBA project before saving;

But I think you can do this by saving your file as a normal docx file (instead of docm file). If this works it save lot of work for you

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Studio 2010 in Focus at Great Indian Developer Summit 2010 shaguf5575 ASP.NET 1.x and 2.0 Application Design 0 February 24th, 2010 07:03 AM
Visual Studio 2010 in Focus at Great Indian Developer Summit 2010 shaguf5575 ASP.NET 1.x and 2.0 Application Design 0 February 24th, 2010 07:02 AM
I am NewBies. Please Help always99 .NET Web Services 4 July 20th, 2004 05:43 AM
an applet for newbies freezotic BOOK: Beginning Java 2 2 June 7th, 2004 09:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.