Skip to content

Instantly share code, notes, and snippets.

@jay16
Last active August 29, 2015 14:02
Show Gist options
  • Save jay16/e320cc74425a7b01c1b3 to your computer and use it in GitHub Desktop.
Save jay16/e320cc74425a7b01c1b3 to your computer and use it in GitHub Desktop.
从chart取值计算后再赋值给chart,赋值部分未实现。
Sub JiuZheng
set chart = ActiveDocument.GetSheetObject("CH86")
set notify = ActiveDocument.GetSheetObject("TX06")
'notify.setLabel = "hello"
Call chart.GetSheet().Activate()
''chart.Maximize
ActiveDocument.GetApplication.WaitForIdle
if (chart is nothing) then
msgbox("null")
else
dim thisYear
dim lastYear
dim rate
''Call chart.CopyTableToClipboard(true)
thisYear = replace(chart.GetCell(1,4).Text," ","")
lastYear = replace(chart.GetCell(1,5).Text," ","")
'rate = Val(replace(thisYear,",",""))
' val(replace(lastYear,",",""))-1
''chart.GetCell(1,6).Text = "100"
msgbox("["&(replace(lastYear,",",""))&"]")
end if
End Sub
1) Run external program:
FUNCTION RunExe(cmd)
CreateObject("WScript.Shell").Exec(cmd)
END FUNCTION
SUB CallExample
RunExe("c:\Program Files\Internet Explorer\iexplore.exe")
END SUB
2) Export object to Excel
FUNCTION ExcelExport(objID)
set obj = ActiveDocument.GetSheetObject( objID )
w = obj.GetColumnCount
if obj.GetRowCount>1001
then h=1000
else h=obj.GetRowCount
end if
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add objExcel.Worksheets(1).select()
objExcel.Visible = True
set CellMatrix = obj.GetCells2(0,0,w,h)
column = 1
for cc=0 to w-1 objExcel.Cells(1,column).Value = CellMatrix(0)(cc).Text
objExcel.Cells(1,column).EntireRow.Font.Bold = True
column = column +1
next
c = 1 r =2
for RowIter=1 to h-1
for ColIter=0 to w-1
objExcel.Cells(r,c).Value = CellMatrix(RowIter)(ColIter).Text
c = c +1
next
r = r+1 c = 1
next
END FUNCTION
SUB CallExample
ExcelExport( "CH01"
) END SUB
3) Export object to JPG
FUNCTION ExportObjectToJpg( ObjID, fName)
ActiveDocument.GetSheetObject(ObjID).ExportBitmapToFile fName
END FUNCTION
SUB CallExample
ExportObjectToJpg "CH01", "C:\CH01Image.jpg"
END SUB
4) Save and exit QlikView
SUB SaveAndQuit
ActiveDocument.Save
ActiveDocument.GetApplication.Quit
END SUB
5) Clone Dimension Group
SUB DuplicateGroups
SourceGroup = InputBox("Enter Source Group Name")
CopiesNo = InputBox("How many copies?")
SourceGroupProperties = ActiveDocument.GetGroup(SourceGroup).GetProperties
FOR i = 1 TO CopiesNo
SET DestinationGroup = ActiveDocument.CreateGroup(SourceGroupProperties.Name & "_" & i)
SET DestinationGroupProperties = DestinationGroup.GetProperties
IF SourceGroupProperties.IsCyclic THEN
DestinationGroupProperties.IsCyclic = true
DestinationGroup.SetProperties DestinationGroupProperties
ELSE
SourceGroupProperties.IsCyclic = true
DestinationGroupProperties.SetProperties SourceGroupProperties
END IF
SET Fields = SourceGroupProperties.FieldDefs
FOR c = 0 TO Fields.Count-1
SET fld = Fields(c)
DestinationGroup.AddField fld.name
NEXT
Application.waitforidle
NEXT
END SUB
6) Open document with selection of current month
SUB DocumentOpen
ActiveDocument.Sheets("Intro").Activate
ActiveDocument.ClearAll(true)
ActiveDocument.Fields("YearMonth").Select
ActiveDocument.Evaluate("Date(MonthStart(Today(), 0),'MMM-YYYY')")
END SUB
7) Read and Write variables
FUNCTION getVariable(varName)
set v = ActiveDocument.Variables(varName)
getVariable = v.GetContent.String
END FUNCTION
SUB setVariable(varName, varValue)
set v = ActiveDocument.Variables(varName)
v.SetContent varValue, true
END SUB
set v=ActiveDocument.Variables("coeff")
v.Forcecontent "20",20
使用一个变量与另一变量相同
sub X
v = ActiveDocument.Evaluate("coeffm")
ActiveDocument.Variables("coeff").SetContent v, true
end sub
以yyyymmdd格式取得今天的日期并赋值变量
v = ActiveDocument.Evaluate("=date(today(),'YYYYMMDD')")
ActiveDocument.Variables("vDate").SetContent v, true
8) Open QlikView application, reload, press a button and close (put the code in a .vbs file)
Set MyApp = CreateObject("QlikTech.QlikView")
Set MyDoc = MyApp.OpenDoc ("C:\QlikViewApps\Demo.qvw","","")
Set ActiveDocument = MyDoc ActiveDocument.Reload
Set Button1 = ActiveDocument.GetSheetObject("BU01")
Button1.Press
MyDoc.GetApplication.Quit
Set MyDoc = Nothing
Set MyApp = Nothing
9) Delete file
FUNCTION DeleteFile(rFile)
set oFile = createObject("Scripting.FileSystemObject")
currentStatus = oFile.FileExists(rFile)
if currentStatus = true then
oFile.DeleteFile(rFile)
end if
set oFile = Nothing
END FUNCTION
SUB CallExample
DeleteFile ("C:\MyFile.PDF")
END SUB
10) Get reports information
function countReports
set ri = ActiveDocument.GetDocReportInfo
countReports = ri.Count
end function
function getReportInfo
set ri = ActiveDocument.GetDocReportInfo
set r = ri.Item(i)
getReportInfo = r.Id & "," & r.Name & "," & r.PageCount & CHR(10)
end function
11) Send mail using Google Mail
SUB SendMail
Dim objEmail
Const cdoSendUsingPort = 2 ' Send the message using SMTP
Const cdoBasicAuth = 1 ' Clear-text authentication
Const cdoTimeout = 60 ' Timeout for SMTP in seconds
mailServer = "smtp.gmail.com"
SMTPport = 465
mailusername = "[email protected]"
mailpassword = "MyPassword"
mailto = "[email protected]"
mailSubject = "Subject line"
mailBody = "This is the email body"
Set objEmail = CreateObject("CDO.Message")
Set objConf = objEmail.Configuration
Set objFlds = objConf.Fields
With objFlds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPport
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = cdoTimeout
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasicAuth
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailusername
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailpassword
.Update
End With
objEmail.To = mailto
objEmail.From = mailusername
objEmail.Subject = mailSubject
objEmail.TextBody = mailBody
objEmail.AddAttachment "C:\report.pdf"
objEmail.Send
Set objFlds = Nothing
Set objConf = Nothing
Set objEmail = Nothing
END SUB
12) Changing Font setting of an Object
SUB Font()
set obj = ActiveDocument.GetSheetObject("BU01")
set fnt = obj.GetFrameDef.Font
fnt.PointSize1000 = fnt.PointSize1000 + 1000
fnt.FontName = "Calibri"
fnt.Bold = true
fnt.Italic = true
fnt.Underline = true
obj.SetFont fnt
END SUB
13. To Show and Hide Tab row.
Sub ShowTab
rem Hides tabrow in document properties
set docprop = ActiveDocument.GetProperties
docprop.ShowTabRow=true
ActiveDocument.SetProperties docprop
End Sub
Sub HideTab
rem Hides tabrow in document properties
set docprop = ActiveDocument.GetProperties
docprop.ShowTabRow=false
ActiveDocument.SetProperties docprop
End Sub
14. Always One Selected Enable / Disable setting through Macro
Sub AlwaysOneSelected
set obj = ActiveDocument.GetSheetObject("LB02")
set boxfield=obj.GetField
set fprop = boxfield.GetProperties
fprop.OneAndOnlyOne = True
boxfield.SetProperties fprop
End Sub
Sub RemoveAlwaysOneSelected
set obj = ActiveDocument.GetSheetObject("LB02")
set boxfield=obj.GetField
set fprop = boxfield.GetProperties
fprop.OneAndOnlyOne = False
boxfield.SetProperties fprop
ActiveDocument.ClearAll True
End Sub
15. Reading Rows and Columns in a table object
Sub ReadStraightTable
Set Table = ActiveDocument.GetSheetObject("CH01")
For RowIter = 0 to table.GetRowCount-1
For ColIter = 0 to table.GetColumnCount-1
set cell = table.GetCell(RowIter,ColIter)
Msgbox(cell.Text)
Next
Next
End Sub
16. Get number of Rows in a Straight or Pivot tables
function ReadRowsCount
set v = ActiveDocument.GetVariable("variableName")
v.SetContent ActiveDocument.GetSheetObject("CH01").GetRowCount-1, true
end function
17. Get and Set variable values in macros
function setVariable(name, value)
set v = ActiveDocument.GetVariable("variableName")
v.SetContent value,true
end function
function getVariable(name)
set v = ActiveDocument.GetVariable("variableName")
getVariable = v.GetContent.String
end function
18. Export chart data to QVD file, the chart may Bar/Line/StraightTable/Pivot etc.
sub ChartToQVD
set obj = ActiveDocument.GetSheetObject("CH01")
obj.ExportEx "QvdName.qvd", 4
end sub
Hope this helps.
@jay16
Copy link
Author

jay16 commented Jun 17, 2014

set chart = ActiveDocument.GetSheetObject("CH01")
ActiveDocument.Variables("ROWS_VAR").SetContent chart.GetRowCount, True

@jay16
Copy link
Author

jay16 commented Jun 17, 2014

set tx = ActiveDocument.GetSheetObject("TX02")
set txprop = tx.GetProperties
txprop.Layout.Text.v = msg
tx.SetProperties txprop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment