I have a word document that the user can input data. I have a button that will run VB to gather information. I am having trouble getting VB to write the output of the script to the document while it is open. Can VBS write to an open/active word document? Sorry if this seems basic....new to VB.
You can use the method - getObject to set the reference to an already opened Word document as shown in the following code:
Dim word, docs, docName, reqdDoc
Set word = getObject(,"word.application")
Set docs = word.documents
docName = "Document1" 'Enter the name of the document in which you want to write the data
'MsgBox docs.count
For Each doc In docs
If StrComp(doc.fullName,docName,1)=0 Then
Set reqdDoc = doc
Exit For
End If
Next
reqdDoc.Activate
word.selection.typeText "Hello There:"
Related
I am trying to create a VBScript that opens Word and inputs text into it.
My code:
::Set wshshell = WScript.CreateObject("WScript.Shell")
::Set objShell = CreateObject("WScript.Shell")
::objShell.AppActivate "word"
::userProfilePath = objShell.ExpandEnvironmentStrings("%UserProfile%")
::WScript.Sleep 10000
::wshshell.SendKeys "Hello World!"
Perhaps surprisingly, you cannot interact with Word unless you actually started the program in the first place. As documented the AppActivate method activates an application window, i.e. brings the window of an already running application to the foreground.
To start Word programmatically use something like this:
Set wd = CreateObject("Word.Application")
wd.Visible = True
You can open a document like this:
Set doc = wd.Documents.Open("C:\path\to\your.docx")
or create a new document like this:
Set doc = wd.Documents.Add
Text can be entered for instance via the TypeText method:
doc.Selection.TypeText "some text"
Save the document like this:
doc.Save
doc.Close
or like this (if it's a new document or you want to save it under a different name/path):
doc.SaveAs "C:\path\to\new.docx", 16
doc.Close
Exit from the application like this:
wd.Quit
Do NOT use SendKeys for anything. EVER. Not unless you're being forced at gunpoint.
Also do NOT write Frankenscript that mixes different languages (like batch and VBScript) in the same file. It's a pain in the rear to debug and maintain.
Besides programming, I am also a professional photographer. I recently set up an extremely successful photobooth... so successful I have about 500 photos, and a list of 60 emails (some pictures have multiple recipients)to send them to.
Email is the only way I can get the pictures to their respective owner, and I do not have time to manually send each photo to its owner in an email app, nor do I want to risk making mistakes in the process. All of the email addresses are in a text file, with the file locations of each image following the intended recipient. In addition to the pictures, I have one universal body text to send.
I have already looked into ASP, Macros, CDOSys, and some slightly confusing options with SMTP, but answers using these are more than welcome.
Any ideas on how to use the information in the text file to email each recipient with their photos?
tl;dr: I need to get 500 individual photos to 60 email addresses, without sending each picture manually, or giving someone someone else's photos. All of the information is organized in a text file.
Update: I have a few different versions of the text file. I have a text file straight out of a notepad editor, I have a csv file, and I have a version which delimits file paths with # and email addresses with tabs.
Using classic asp and web email server and assuming you can have created a csv file.
step 1. upload textFile.csv into server
with say emailAddress, pictureFileAddress etc.
step 2. import and emailer script (classic asp)
downloadFile = Server.MapPath("textFile.csv")
Set objFSO = CreateObject("Scripting.FileSystemObject")
if objFSO.FileExists(downloadFile) then
firstLine = true '... assume first line is header
Do Until objFile.AtEndOfLine OR finished
StrTemp = objFile.ReadLine
dataArray = Split(StrTemp, ",") '... assume just two fields for this e.g.
dataCount = UBound(dataArray)
email = dataArray(0)
picturefile = dataArray(1)
'... perform any data validation or Regexp
If email <> "" AND firstLine = false then
Set myMail=CreateObject("CDO.Message")
myMail.From = "yourfromemail"
myMail.ReplyTo = "yourreplyfromemail"
myMail.Subject = "Your picture"
'...create target file for attachment
fn1 = picturefile
fn2 = Server.MapPath(fn1)
myMail.AddAttachment fn2
myMail.HTMLBody = strMessage
myMail.Send
Set myMail = Nothing
end if
if firstLine then firstline = false
loop
End if
I hope this helps
I created a template for my test suite in QTP where the level of abstraction (parameterization) is sufficiently good.
I would now need to populate a new test suite from the existing pattern, thus replacing certain entries with other ones in various files.
For example one of the words I deliberately put in the script suite pattern is [Template], therefore I would need to copy and paste the template with a different name, change all the entries by [Template] to the new string and so forth.
Any code would be appreciated as my VBScript skills are not optimal ;)
Thanks in advance!
Use this demo script:
Option Explicit
Dim gMap : Set gMap = Createobject("Scripting.Dictionary")
Function replGMap(sM, nPos, sSrc)
replGMap = gMap(sM)
End Function
Dim reMap : Set reMap = New RegExp
reMap.Global = True
reMap.Pattern = "\[\w+\]"
gMap("[A]") = "abra"
gMap("[B]") = "cadabra"
WScript.Echo reMap.Replace("1[A]2[A]3[B]4[A]5", GetRef("replGMap"))
output:
abra2abra3cadabra4abra5
as a list of keywords to look up in the VBScript Docs. For using a function in .Replace, see here.
The FileSystemObject provides the means (Open/CreateTextFile, ReadAll, Write) to read and write files.
Here is a short code that opens my "fox.doc" :
Set wd = CreateObject("Word.Application")
wd.Visible = True
Set doc = wd.Documents.Open("c:\fox.doc")
I need to add only one word "bottle" in the end of this document (after it has been opened). Which lines should I add to my small vbs script for that?
You can learn a lot by recording macros in Word and then making the, usually small, changes that will make the code work in VBScript.
Set r = doc.Words.Last
r.InsertAfter (" bottle")
firts excuse me for my English it`s super Freak. Sorry
I have a big problem , i need finish my applicatión in VB6.0 for a test in my High Schooll and i can`t find the solution, My app open a FFmpeg.EXE file which open a cmd window Prompt and start a trascoding process, i need link the last line generated into the Prompt of the CMD window (Or top Bottom) , in this line exists Values what change , in this trascoding process the result are bit Rates , which fluctuates acording to others var.
The idea it´s what into the form of my app i can read this line in real time to bulid a progress bar (File Size/Bitrate average)=time to process.
Can you help me. Thanks for the answer....
Put a reference to Windows Scripting Host Object Model and try this snippet
Option Explicit
Private Sub Command1_Click()
Dim oExec As WshExec
Dim sRow As String
With New WshShell
Set oExec = .Exec("tasklist.exe")
End With
Do While oExec.Status = WshRunning
sRow = oExec.StdOut.ReadLine
If InStr(1, sRow, "vb6.exe", vbTextCompare) > 0 Then
MsgBox sRow, vbExclamation
End If
Loop
End Sub
Basicly try executing FFmpeg.EXE and ReadLine until you find some key text.
Send the output to a textfile then read this textfile.
Should look something like this:
ping >e:\test.txt
Where ping is the FFmpge.EXE and e:\test.txt the output textfile
rdkleine
I read your answer and this is great work very good , only that it shows in the log a death value "text", and i need the value of fluctuates bitrates of conversion , which changes in real time in the prompt of the cmd window. i'm trying now with the source code of wqw , i'm working in there.
Thak's for your answer..