473,411 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Replace text in Word header from Microsoft Access VBA

Hi all,

I would like to replace text in a Word template header, which is opened from MS Access.

I can open the file just fine...:

Expand|Select|Wrap|Line Numbers
  1. Set oApp = CreateObject("Word.Application")
  2. oApp.Visible = True
  3. Set doc = oApp.Documents.Add(Template:="C:\CL_Template.dotx", NewTemplate:=False, DocumentType:=0)
I can also access many properties of Word such as oApp.Selection.TypeText, but I cannot figure out how to replace text in the header of my word document with a variable declared in Access.

I know in Word, when recording the macro, it looks like this, but I can't get it to work in Access:

Expand|Select|Wrap|Line Numbers
  1. Dim myStoryRange As Range
  2. For Each myStoryRange In ActiveDocument.StoryRanges
  3.     With myStoryRange.Find
  4.         .Text = "Letterdate"
  5.         .Replacement.Text = let_date_var
  6.         .Wrap = wdFindContinue
  7.         .Execute Replace:=wdReplaceAll
  8.     End With
  9. Next myStoryRange 
May 10 '13 #1
1 4813
NeoPa
32,556 Expert Mod 16PB
Try this :
Expand|Select|Wrap|Line Numbers
  1. Private Const wdFindContinue As Long = 1
  2. Private Const wdReplaceAll As Long = 2
  3.  
  4. Dim oApp As Word.Application
  5. Dim doc As Word.Document
  6. Dim myStoryRange As Word.Range
  7.  
  8. Set oApp = CreateObject("Word.Application")
  9. oApp.Visible = True
  10. Set doc = oApp.Documents.Add(Template:="C:\CL_Template.dotx", NewTemplate:=False, DocumentType:=0)
  11.  
  12. For Each myStoryRange In doc.StoryRanges
  13.     With myStoryRange.Find
  14.         .Text = "Letterdate"
  15.         .Replacement.Text = let_date_var
  16.         .Wrap = wdFindContinue
  17.         .Execute Replace:=wdReplaceAll
  18.     End With
  19. Next myStoryRange
It's air-code, so it's just something to start you off with. Good luck.

PS. Application Automation may be of some help.
May 10 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

41
by: Ruby Tuesday | last post by:
Hi, I was wondering if expert can give me some lite to convert my word table into access database. Note: within each cell of my word table(s), some has multi-line data in it. In addition, there...
5
by: Stefania Scott | last post by:
I am trying to print a word document from Access. The code I've written works well in my computer but does not in the one were it is needed. Here the piece of code: 'doc path strObjectPath =...
8
by: brian kaufmann | last post by:
Hi, I'm new to Access and this may be a basic question but I would appreciate it if you could let me know how to do this: I've created an Access table and would like to insert a column with...
0
by: bmoos1 | last post by:
I have MS Office 2000 and upgraded Access Only to 2003. The problem is I want to use my Access queries for a mail/merge, but I get the error message: "Word could not re-establish DDE...
7
by: MLH | last post by:
Building Applications with Microsoft Access 97 is a publication I think I need. Is it available in book form? Is MicroSoft the sole vendor? Anybody got a copy they wanna sell???
1
by: galsaba | last post by:
How can I replace a word in a field in all records? Let's say that I have the record "address". and the adress are: 37-08 east linwood st 121 east smith road 22 ave west apple blvd 202 galsaba...
8
by: Tim Murphy | last post by:
How do you read/write a Word document embedded in an OLE Object field in an Access table with VB.NET? I have an Access application where users are creating and editing Word documents that are...
5
by: Casey | last post by:
Hello, Can someone give me specific code to replace text on a page using server side javascript? I need to use server-side because I need the output to be recognized in the final HTML so that...
2
by: Cliff72 | last post by:
I'm creating a database that will be uploading some text files into an access table. The problem is that the text files have a header which messes up my import specs. so what i have had to do is to...
8
by: diasdaman | last post by:
I know how to import text files, but in this case I need to import a text file line by line on an on-the-fly basis, such that the Access will look at the first two digits of a line, and then import...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.