Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 01-10-2023, 03:42 PM
Guessed's Avatar
Guessed Guessed is offline Unable to add tab stops to Word document via Excel VBA Windows 10 Unable to add tab stops to Word document via Excel VBA Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
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

If there is a reference to the Word Object Library then the wdConstants appear to work for me so my earlier advice should be ignored. I had a fiddle with your code to actually test it and adapt it for various page setups. It works for me on multiple files so I'm not sure why you would get variable results. Can you test this code on your machine?
Code:
Sub createFooter()
  'Needs a reference to Microsoft Word xx.x Object Library
  Dim wdApp As Word.Application, wdDoc As Word.Document, rng As Word.Range
  Dim k As Integer, strFile As String
  
  With Application.FileDialog(msoFileDialogOpen)
    .Filters.Clear
    .Filters.Add "Word Files", "*.doc*", 1
    If .Show = -1 Then strFile = .SelectedItems(1)
  End With
  If strFile = "" Then Exit Sub
        
  On Error Resume Next
  Set wdApp = GetObject(, "Word.Application")
  If Err.Number <> 0 Then
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True
  End If
  Set wdDoc = wdApp.Documents.Open(Filename:=strFile, AddToRecentFiles:=False, Visible:=True)
  
  For k = 1 To wdDoc.Sections.Count
    wdDoc.Sections(k).Footers(wdHeaderFooterPrimary).LinkToPrevious = False
    Set rng = wdDoc.Sections(k).Footers(wdHeaderFooterPrimary).Range
    With rng
      .Delete         'delete everything in current footer
      .ParagraphFormat.TabStops.ClearAll
      .Font.Name = "Times New Roman"
      .Font.Size = 12
      .Text = "Confidential Information"
      .Collapse wdCollapseStart
      .InsertAlignmentTab Alignment:=wdRight, RelativeTo:=wdMargin
      .Collapse wdCollapseStart
      .Fields.Add rng, wdFieldPage, , False
      .Collapse wdCollapseStart
      .InsertAlignmentTab Alignment:=wdCenter, RelativeTo:=wdMargin
      .Text = "Company"
    End With
  Next
  
  Set wdDoc = Nothing
  Set wdApp = Nothing
  MsgBox "Footer added or replaced"
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
 

Tags
excel 365, word 365



Similar Threads
Thread Thread Starter Forum Replies Last Post
Tabs in word like Excel kdawgtx Word 1 01-16-2021 08:09 PM
Word Document Restricted to using Tabs to move to next place for input swalsh96 Word 2 09-20-2016 04:41 AM
HELP: Unable to save word document SteveJ Word 0 06-28-2013 04:06 AM
Unable to select anything in an MS Word Document PaulT Word 0 08-08-2011 07:45 PM
Creating custom tabs for Word & Excel 2003 on a Windows 7 OS Skarab Office 1 07-05-2010 09:23 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:45 AM.


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