Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-21-2015, 12:15 AM
jenniferfoster jenniferfoster is offline VBA scripting / table Windows 8 VBA scripting / table Office 2010 32bit
Novice
VBA scripting / table
 
Join Date: Aug 2015
Posts: 2
jenniferfoster is on a distinguished road
Default VBA scripting / table

Hello, I hope I can get some help here. I need to edit the script below. I have a table which has variables in it. This table is always the last page of every document. My boss now wants this variable table to be situated at the front of documents. The macro (named macro2) does not run and the bookmark moves to other tables throughout the document when I moved the table to the front of documents. Seems like it finds and replaces fields and after each find and replace it goes to the top of the document, not sure - maybe it is set up to go to the last table in the document - I'm not sure and cannot work it out from looking at the script. Can someone who is knowledgeable with script writing please help me...... Our workplace has upgraded their systems from 2003 to 2010 and updating everything including changing them. Thank you in advance and I hope I make sense!

Sub Macro2()
Dim vTest As Long

Dim vFlag, dummy, xx, yy, zz As Integer
ct = 1
vFlag = 0

Application.ScreenUpdating = False
ActiveWindow.View.ShowHiddenText = True
On Error Resume Next ' Defer error handling.
Err.Clear

'On Error GoTo NotAPrecedentLetter
Selection.GoTo What:=wdGoToBookmark, name:="PLF"
If Err.Number <> 0 Then
' Msg = "Error # " & Str(Err.Number) & " was generated by " _
' & Err.Source & Chr(13) & Err.Description
' MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
Else
Call sInsertLetterSetup
End If

'NotAPrecedentLetter:
On Error GoTo NoPrecedentCodes
Selection.GoTo What:=wdGoToBookmark, name:="start"
Selection.Tables(1).Select
tblSize = Selection.Rows.Count

Selection.MoveUp Unit:=wdLine, Count:=1

ReDim List1(tblSize + 1)


yy = ActiveDocument.Tables.Count


'this macro assumes variable code labels have the following
'format only : [number{s}][letter(s)][period]
'for example : 12A. or 12A or 12 or 1
'where [letter(s)] and [period] are optional

For xx = 1 To tblSize Step 1
ActiveDocument.Tables(yy).Cell(xx, 1).Select
List1(xx).sNumber = Selection


'store integer value of variable number
'used in comparison in PrecdentForm code
dummy = Val(List1(xx).sNumber)
List1(xx).iNumber = Str(dummy)


'check to see if a "Return" was included in selection
MyPosCR = InStr(1, List1(xx).sNumber, vbCr, vbTextCompare)
If MyPosCR > 0 Then


List1(xx).sNumber = Mid(List1(xx).sNumber, 1, MyPosCR - 1)
End If

'check to see if a "period" was included in selection
MyPosPeriod = InStr(1, List1(xx).sNumber, ".", vbTextCompare)
If MyPosPeriod > 0 Then
List1(xx).sNumber = Mid(List1(xx).sNumber, 1, MyPosPeriod - 1)
End If



ActiveDocument.Tables(yy).Cell(xx, 2).Select
List1(xx).sLabel = Selection

'check to see if a "Return" was included in selection
MyPosCR = InStr(1, List1(xx).sLabel, vbCr, vbTextCompare)
If MyPosCR > 0 Then
List1(xx).sLabel = Mid(List1(xx).sLabel, 1, MyPosCR - 1)
End If

Next xx

Load PrecedentForm
PrecedentForm.Show

Unload PrecedentForm

If ct <= tblSize Then
ActiveDocument.Tables(yy).Cell(ct, 1).Select
Selection.HomeKey Unit:=wdLine
ActiveDocument.Bookmarks("start").Delete
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, name:="start"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.SplitTable

MsgBox "Make edits; then click GO button to continue updating"
vFlag = 1
Else
Selection.HomeKey Unit:=wdStory
'Selection.GoTo What:=wdGoToBookmark, Name:="text"
MsgBox "Precedent Variable Update Complete"
vFlag = 0
End If

Selection.HomeKey Unit:=wdStory
Selection.Find.Replacement.ClearFormatting
' With Selection.Find
' .Text = "vbCR"
' .Replacement.Text = "^p"
' .Forward = True
' .Wrap = wdFindContinue
' End With
With Selection.Find
.Text = Chr(13) + Chr(10)
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
If vFlag = 1 Then
Selection.HomeKey Unit:=wdStory
Selection.Find.Execute FindText:="*" + Trim(List1(ct).sNumber) + "*"
End If

NoPrecedentCodes:
End Sub
Reply With Quote
  #2  
Old 08-21-2015, 12:58 AM
gmayor's Avatar
gmayor gmayor is offline VBA scripting / table Windows 7 64bit VBA scripting / table Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Without access to the document it is difficult to test, but all seems to revolve around
Code:
yy = ActiveDocument.Tables.Count
i.e. the last table in the document. If you want process a different table e.g. the first table in the document, you need to address that table here. e.g.
Code:
yy = ActiveDocument.Tables(1)
What the effect of the bookmarks in the document might be I cannot say.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 08-21-2015, 06:28 PM
jenniferfoster jenniferfoster is offline VBA scripting / table Windows 8 VBA scripting / table Office 2010 32bit
Novice
VBA scripting / table
 
Join Date: Aug 2015
Posts: 2
jenniferfoster is on a distinguished road
Default VBA/Tables/Bookmarks

Thank you GMayor.... so if in every template it will be the 3rd table I want the Macro 2 to go to would it be then:

yy = ActiveDocument.Tables(3)?

Thank you
Reply With Quote
  #4  
Old 08-21-2015, 09:07 PM
Guessed's Avatar
Guessed Guessed is offline VBA scripting / table Windows 7 32bit VBA scripting / table Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Jennifer

Your code has some unclear bits in it. If you know that the table is always the third one then you simply use that number rather than creating a variable for it.

The following declaration line is probably not giving you what you expect.
Dim vFlag, dummy, xx, yy, zz As Integer

This is a trap for young players but this declares zz as an Integer whilst all the others are of variant type. This matters a lot because the two lines listed in Graham's post assign yy to different things
yy = ActiveDocument.Tables.Count 'this is an integer
yy = ActiveDocument.Tables(1) 'this is a table
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Tags
vba tables macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA scripting / table Move table cell contents from one table to another table or cell in same table donaldadams1951 Word VBA 4 02-04-2015 03:54 PM
Pivot Table Question- Can the table display in the order the boxes were checked/selected? blackjack Excel 0 09-13-2014 08:57 PM
VBA scripting / table help with scripting mrokoua Outlook 1 08-26-2014 03:52 AM
How to change the path of a table copied by excel in word as linked table RTF? Cristin7 Word VBA 3 03-10-2014 05:25 AM
Scripting error presented when emails are opened from one specific sender Mr Davo Outlook 0 05-30-2012 07:01 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:15 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft